Click here to Skip to main content
15,913,939 members
Home / Discussions / Database
   

Database

 
GeneralRe: Prioritized joining - Updated Pin
Richard Deeming17-Jun-16 12:29
mveRichard Deeming17-Jun-16 12:29 
GeneralRe: Prioritized joining - Updated Pin
Jörgen Andersson18-Jun-16 4:18
professionalJörgen Andersson18-Jun-16 4:18 
GeneralRe: Prioritized joining - Updated Pin
Jörgen Andersson29-Jun-16 10:44
professionalJörgen Andersson29-Jun-16 10:44 
QuestionPivot Not Working Pin
MadDashCoder11-Jun-16 8:01
MadDashCoder11-Jun-16 8:01 
AnswerRe: Pivot Not Working Pin
Mycroft Holmes11-Jun-16 14:51
professionalMycroft Holmes11-Jun-16 14:51 
AnswerRe: Pivot Not Working Pin
CHill6012-Jun-16 0:33
mveCHill6012-Jun-16 0:33 
GeneralRe: Pivot Not Working Pin
MadDashCoder12-Jun-16 3:56
MadDashCoder12-Jun-16 3:56 
GeneralRe: Pivot Not Working Pin
CHill6012-Jun-16 4:43
mveCHill6012-Jun-16 4:43 
The only solution that comes to mind at the moment (I'll keep thinking!) is to have a separate table listing the courses with an Id field that reflects the order you require e.g.
SQL
DECLARE @courseNames table (id int identity(1,1), course nvarchar(25))
insert into @courseNames values ('English'),('Physics'),('Mathematics'),('Engineering')

It could just as easily be a permanent table as a table variable. You could use such a table to generate dynamic SQL for the PIVOT if more courses are added).
You can then join the previous query to this table and ORDER BY the id on the course table e.g.
SQL
select cn.id, Courses, MAX(ISNULL(Jane,'')) as Jane, MAX(ISNULL(Michelle,'')) as Michelle, MAX(ISNULL(Dan,'')) as Dan
from 
	(Select * from TestScores
	Unpivot(Course for Courses in (English, Physics, Mathematics, Engineering))
	as upv ) UPV
Pivot(MAX(Course) for Student in (Jane, Michelle, Dan)) as pv
inner join @courseNames cn ON pv.Courses=cn.course
GROUP BY cn.id, Courses
ORDER BY cn.id
or if you don't want to include the ID number in the output you could use a CTE ...
SQL
;with q as
(
	select Courses, MAX(ISNULL(Jane,'')) as Jane, MAX(ISNULL(Michelle,'')) as Michelle, MAX(ISNULL(Dan,'')) as Dan
	from 
		(Select * from TestScores
		Unpivot(Course for Courses in (English, Physics, Mathematics, Engineering))
		as upv ) UPV
	Pivot(MAX(Course) for Student in (Jane, Michelle, Dan)) as pv
	GROUP BY Courses
)
select q.Courses, q.Jane, q.Michelle, q.Dan
FROM q
inner join @courseNames cn ON q.Courses=cn.course
ORDER BY cn.id

GeneralRe: Pivot Not Working Pin
MadDashCoder12-Jun-16 8:36
MadDashCoder12-Jun-16 8:36 
GeneralRe: Pivot Not Working Pin
CHill6012-Jun-16 8:54
mveCHill6012-Jun-16 8:54 
GeneralRe: Pivot Not Working Pin
Mycroft Holmes12-Jun-16 12:58
professionalMycroft Holmes12-Jun-16 12:58 
QuestionDatabase backed up type virtual device- not mainteance ? Pin
SreeDBA5-Jun-16 5:28
SreeDBA5-Jun-16 5:28 
AnswerRe: Database backed up type virtual device- not mainteance ? Pin
Richard Deeming6-Jun-16 5:13
mveRichard Deeming6-Jun-16 5:13 
Questionsyntax error for simple IF - THEN Pin
Jassim Rahma1-Jun-16 7:50
Jassim Rahma1-Jun-16 7:50 
AnswerRe: syntax error for simple IF - THEN Pin
thatraja1-Jun-16 9:04
professionalthatraja1-Jun-16 9:04 
GeneralRe: syntax error for simple IF - THEN Pin
Jassim Rahma1-Jun-16 21:01
Jassim Rahma1-Jun-16 21:01 
GeneralRe: syntax error for simple IF - THEN Pin
Peter_in_27801-Jun-16 21:13
professionalPeter_in_27801-Jun-16 21:13 
GeneralRe: syntax error for simple IF - THEN Pin
thatraja3-Jun-16 5:50
professionalthatraja3-Jun-16 5:50 
QuestionCall dll from SQL trigger Pin
srikrishnathanthri31-May-16 19:49
srikrishnathanthri31-May-16 19:49 
AnswerRe: Call dll from SQL trigger Pin
Richard Deeming1-Jun-16 2:39
mveRichard Deeming1-Jun-16 2:39 
GeneralRe: Call dll from SQL trigger Pin
srikrishnathanthri1-Jun-16 4:04
srikrishnathanthri1-Jun-16 4:04 
GeneralRe: Call dll from SQL trigger Pin
Richard Deeming1-Jun-16 4:14
mveRichard Deeming1-Jun-16 4:14 
QuestionJoining three tables Pin
Raabi Anony27-May-16 2:27
Raabi Anony27-May-16 2:27 
AnswerRe: Joining three tables Pin
Eddy Vluggen27-May-16 2:53
professionalEddy Vluggen27-May-16 2:53 
AnswerRe: Joining three tables Pin
Richard Deeming27-May-16 3:21
mveRichard Deeming27-May-16 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.