| Author |
Topic |
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-20 : 15:13:33
|
| Hi,I'm just starting to learn SQL, just on the querying side and have what I think is quite a tricky function. It may be simple for you guys.I want to make a report based on three tables. Enrolment, Course and Student. Its pretty easy until I need to crosstab it.It's more complex than this (more fields but this gives the gist) Basically I want AcademicYearId, StudentID, coursecode, completionstatusid which would give the following.AcademicYearId StudentId coursecode completionstatusid 05/06 912345 FWFT1 CONTINUING05/06 914587 FWFT1 CONTINUING05/06 987654 FWFT1 WITHDRAWN05/06 954376 FWFT1 CONTINUING05/06 934512 FWFT1 TRANSFERRED05/06 923000 FWFT1 TRANSFERRED04/05 945109 FWFT1 COMPLETEDI have managed to group it by academicyearid, coursecode and completionstatusid and done the counts ok but I want to twist the completionstatusid so each type of status has its own column like thisYear CourseCode Students Continuing Withdrawn Transferred Completed05/06 FWFT1 6 3 1 2 004/05 FWFT1 1 0 0 0 1Can anyone help me with the the extra columns please. |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-20 : 15:14:45
|
| Oops. Formatting went a bit wrong there. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-20 : 19:23:54
|
[code]select AcademicYearId, CourseCode, Stidents = count(*), Continuing = count(case when completionstatusid = 'CONTINUING' then 1 end), Withdrawn = count(case when completionstatusid = 'WITHDRAWN' then 1 end), Transferred= count(case when completionstatusid = 'TRANSFERRED' then 1 end), Completed = count(case when completionstatusid = 'COMPLETED' then 1 end)from tablegroup by AcademicYearId, CourseCode[/code] KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-21 : 00:31:39
|
| Read about Cross-tab Reports in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-21 : 05:35:03
|
quote: Originally posted by khtan
select AcademicYearId, CourseCode, Stidents = count(*), Continuing = count(case when completionstatusid = 'CONTINUING' then 1 end), Withdrawn = count(case when completionstatusid = 'WITHDRAWN' then 1 end), Transferred= count(case when completionstatusid = 'TRANSFERRED' then 1 end), Completed = count(case when completionstatusid = 'COMPLETED' then 1 end)from tablegroup by AcademicYearId, CourseCode KH
Thankyou. Will try this on Monday.Dan |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-21 : 05:35:37
|
quote: Originally posted by madhivanan Read about Cross-tab Reports in sql server help fileMadhivananFailing to plan is Planning to fail
HiI saw that after I'd posted it but being a newbie couldnt wokr it out. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-21 : 06:11:04
|
There you can find examples similar to Tan posted. Here is other methodselect AcademicYearId, CourseCode, Stidents = count(*), Continuing = SUM(case when completionstatusid = 'CONTINUING' then 1 else 0 end), Withdrawn = SUM(case when completionstatusid = 'WITHDRAWN' then 1 else 0 end), Transferred= SUM(case when completionstatusid = 'TRANSFERRED' then 1 else 0 end), Completed = SUM(case when completionstatusid = 'COMPLETED' then 1 else 0 end)from tablegroup by AcademicYearId, CourseCode MadhivananFailing to plan is Planning to fail |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-21 : 11:07:04
|
quote: Originally posted by madhivanan There you can find examples similar to Tan posted. Here is other methodselect AcademicYearId, CourseCode, Stidents = count(*), Continuing = SUM(case when completionstatusid = 'CONTINUING' then 1 else 0 end), Withdrawn = SUM(case when completionstatusid = 'WITHDRAWN' then 1 else 0 end), Transferred= SUM(case when completionstatusid = 'TRANSFERRED' then 1 else 0 end), Completed = SUM(case when completionstatusid = 'COMPLETED' then 1 else 0 end)from tablegroup by AcademicYearId, CourseCode MadhivananFailing to plan is Planning to fail
Thankyou. it may turn out to be more complex than this as I have other fields and joins to include but thabnks for your help so far.BTW, I just realised you didnt mean to check the crosstab/pivot table article on here (which i noticed after i'd posted). I will have a look at the SQL server help file.Daniel |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-23 : 14:37:56
|
| I managed to get my query working pretty well using the code you supplied. Thanks. Adding the extra fields I mentioned created a bit of a problem as I havent used the SUM correctly on the extra fields but i'm going to have another look at it tomorrw. |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-24 : 04:24:06
|
| This is what I have now (I need to change how the part in red views)SELECT TOP 100000 SD.AcademicYearID, O.Code, O.Name, O.TargetStudents, COUNT(SD.StudentDetailID) AS [Total Students], Continuing = SUM(CASE WHEN E.CompletionstatusID = '1' THEN 1 ELSE 0 END), Completed = SUM(CASE WHEN E.CompletionstatusID = '2' THEN 1 ELSE 0 END), Withdrawn = SUM(CASE WHEN E.CompletionstatusID = '3' THEN 1 ELSE 0 END), Transferred = SUM(CASE WHEN E.CompletionstatusID = '4' THEN 1 ELSE 0 END), Cancelled = SUM(CASE WHEN E.CompletionstatusID = '7' THEN 1 ELSE 0 END), [Exempt Under Law] = SUM(CASE WHEN E.FeeExemptionReasonID = '1' THEN 1 ELSE 0 END), [Waived On Benefit] = SUM(CASE WHEN E.FeeExemptionReasonID = '4' THEN 1 ELSE 0 END), [Paid In Full] = SUM(CASE WHEN E.FeeExemptionReasonID = '99' THEN 1 ELSE 0 END), [Other Funding] = SUM(CASE WHEN E.FeeExemptionReasonID = '13' THEN 1 ELSE 0 END), [Waived By College] = SUM(CASE WHEN E.FeeExemptionReasonID = '10' THEN 1 ELSE 0 END), [Waived Level 2 Entitlement] = SUM(CASE WHEN E.FeeExemptionReasonID = '22' THEN 1 ELSE 0 END), [Fee Is Zero] = SUM(CASE WHEN E.FeeExemptionReasonID = '19' THEN 1 ELSE 0 END)FROM prosolution.dbo.Enrolment E INNER JOINprosolution.dbo.Offering O ON E.OfferingID = O.OfferingID INNER JOINprosolution.dbo.StudentDetail SD ON E.StudentDetailID = SD.StudentDetailIDGROUP BY O.Code, O.Name, O.TargetStudents, E.CompletionStatusID, E.FeeExemptionReasonID, SD.AcademicYearID ORDER BY O.Code, SD.AcademicYearIDIf I dont include the part in red italics It looks ok in that it has one line per year as follows (cut down for space)Year Course Target TotalStudents Cont Comp Wdrawn05/06 ATBWK1 15 15 8 6 1Once I start including the red italics part I get extra lines like the following exampleYear Course Target TotalStudents Cont Comp Wdrawn PIF Exempt05/06 ATBWK1 15 7 0 7 0 7 005/06 ATBWK1 15 5 5 0 0 5 005/06 ATBWK1 15 3 0 0 3 0 3 What I'd like it to show is thisYear Course Target TotalStudents Cont Comp Wdrawn PIF Exempt05/06 ATBWK1 15 15 5 7 3 12 3Is this possible? I bet the formatting is out again. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-24 : 04:35:47
|
| TrySelect Year, Course, Sum(Target), sum(TotalStudents), sum(Cont), sum(Comp), sum(Wdrawn), sum(PIF), sum(Exempt)from(Your full query) TGroup by Year, CourseMadhivananFailing to plan is Planning to fail |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-10-24 : 08:08:16
|
| Thankyou for your help Madhivanan. I managed to sort it. I had a rogue extra GROUP BY in there that was messing up my results.Dan |
 |
|
|
|