| Author |
Topic |
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 05:06:48
|
Hi. We have to create an export from our system to be imported into another system. To get the data out we need to create some SQl but we're struggling a bit.I presently have the following codeSELECT ProSolution.dbo.StudentDetail.RefNo, ProSolution.dbo.StudentDetail.FirstForename, ProSolution.dbo.StudentDetail.Surname, ProSolution.dbo.StudentDetail.MobileTel, ProSolution.dbo.StudentDetail.RestrictedUseIndicatorID, ProSolution.dbo.Enrolment.CompletionStatusID, ProSolution.dbo.Offering.CodeFROM ProSolution.dbo.StudentDetail INNER JOIN ProSolution.dbo.Enrolment ON ProSolution.dbo.StudentDetail.StudentDetailID = ProSolution.dbo.Enrolment.StudentDetailID INNER JOIN ProSolution.dbo.Offering ON ProSolution.dbo.Enrolment.OfferingID = ProSolution.dbo.Offering.OfferingIDWHERE (ProSolution.dbo.StudentDetail.AcademicYearID = '06/07') AND (ProSolution.dbo.StudentDetail.RestrictedUseIndicatorID = '9') AND (ProSolution.dbo.Enrolment.CompletionStatusID = '1') The above code returns the data one line per course but we need it to be one line per student with all their courses on one line too, like follows.567897 Tom Smith 07111 111111 TCFT1 CKSAN1 DHICSCan anyone give us any guidance please? ThanksChip |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 05:15:47
|
Nicer to read with aliasesSELECT sd.RefNo, sd.FirstForename, sd.Surname, sd.MobileTel, sd.RestrictedUseIndicatorID, e.CompletionStatusID, o.CodeFROM ProSolution.dbo.StudentDetail AS sdINNER JOIN ProSolution.dbo.Enrolment AS e ON e.StudentDetailID = sd.StudentDetailIDINNER JOIN ProSolution.dbo.Offering AS o ON o.OfferingID = e.OfferingIDWHERE sd.AcademicYearID = '06/07' AND sd.RestrictedUseIndicatorID = '9' AND e.CompletionStatusID = '1' Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 05:23:29
|
| For the output you describe above, you must do a CROSSTAB or PIVOT report.Peter LarssonHelsingborg, Sweden |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 05:33:07
|
quote: Originally posted by Peso For the output you describe above, you must do a CROSSTAB or PIVOT report.Peter LarssonHelsingborg, Sweden
I've tried that but it's not like we have code1, code2, code3, code4 to create the columns on. I've used crosstab case before but this seems different. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 05:37:27
|
| Do you always have the same columns in the output?What is the headers in the output you posted?Peter LarssonHelsingborg, Sweden |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 06:12:17
|
quote: Originally posted by Peso Do you always have the same columns in the output?What is the headers in the output you posted?Peter LarssonHelsingborg, Sweden
normally it would beStudent Ref First Name Surname Code Ideally it should beStudent Ref First Name Surname Code1 Code2 Code3 Code 4I'm thinking subqueries but to be honest i'm at a loss. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 07:42:49
|
| How many codes at most do you expect?Peter LarssonHelsingborg, Sweden |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 08:34:31
|
quote: Originally posted by Peso How many codes at most do you expect?Peter LarssonHelsingborg, Sweden
At most it would be 9 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 08:36:58
|
| Would it be ok to always show nine columns for codes?If there are no codes, NULL is displayed.Peter LarssonHelsingborg, Sweden |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 08:59:42
|
quote: Originally posted by Peso Would it be ok to always show nine columns for codes?If there are no codes, NULL is displayed.Peter LarssonHelsingborg, Sweden
Yes, that would be fine. I did it through a subquery before and it had nulls which werent a problem |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 10:00:28
|
Something like this?SELECT sd.RefNo, sd.FirstForename, sd.Surname, sd.MobileTel, sd.RestrictedUseIndicatorID, e.CompletionStatusID, o.Code, 0 AS CodeNumINTO #TempFROM ProSolution.dbo.StudentDetail AS sdINNER JOIN ProSolution.dbo.Enrolment AS e ON e.StudentDetailID = sd.StudentDetailIDINNER JOIN ProSolution.dbo.Offering AS o ON o.OfferingID = e.OfferingIDWHERE sd.AcademicYearID = '06/07' AND sd.RestrictedUseIndicatorID = '9' AND e.CompletionStatusID = '1'UPDATE t1SET t1.CodeNum = (SELECT COUNT(*) FROM #Temp t2 WHERE t2.RefNo = t1.RefNo AND t2.Code <= t1.Code)FROM #Temp t1SELECT RefNo, FirstForename, Surname, MobileTel, RestrictedUseIndicatorID, CompletionStatusID, MAX(CASE WHEN CodeNum = 1 THEN Code END) AS Code1, MAX(CASE WHEN CodeNum = 2 THEN Code END) AS Code2, MAX(CASE WHEN CodeNum = 3 THEN Code END) AS Code3, MAX(CASE WHEN CodeNum = 4 THEN Code END) AS Code4, MAX(CASE WHEN CodeNum = 5 THEN Code END) AS Code5, MAX(CASE WHEN CodeNum = 6 THEN Code END) AS Code6, MAX(CASE WHEN CodeNum = 7 THEN Code END) AS Code7, MAX(CASE WHEN CodeNum = 8 THEN Code END) AS Code8, MAX(CASE WHEN CodeNum = 9 THEN Code END) AS Code9FROM #TempGROUP BY RefNo, FirstForename, Surname, MobileTel, RestrictedUseIndicatorID, CompletionStatusIDORDER BY RefNo, FirstForename, SurnameDROP TABLE #Temp Peter LarssonHelsingborg, SwedenEDIT: Numbers in red |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 10:10:10
|
| Excellent. Certainly looks like how it should. Only problem is each code 1-9 has the same course in it so theres 9 of the same course rather than the different ones they would have at each position. |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-12 : 10:15:25
|
| Actually, i've just edited it a bit. not sure if i've done right but it seems to be right now. This is what i've done MAX(CASE WHEN CodeNum = 1 THEN Code END) AS Code1, MAX(CASE WHEN CodeNum = 2 THEN Code END) AS Code2, MAX(CASE WHEN CodeNum = 3 THEN Code END) AS Code3, MAX(CASE WHEN CodeNum = 4 THEN Code END) AS Code4, MAX(CASE WHEN CodeNum = 5 THEN Code END) AS Code5, MAX(CASE WHEN CodeNum = 6 THEN Code END) AS Code6, MAX(CASE WHEN CodeNum = 7 THEN Code END) AS Code7, MAX(CASE WHEN CodeNum = 8 THEN Code END) AS Code8, MAX(CASE WHEN CodeNum = 9 THEN Code END) AS Code9If what I've done is correct then our problem appears sorted.Thanks so much for your help. Very much appreciated. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 10:29:35
|
| My bad with Copy&Paste.Well done!Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-12 : 10:33:43
|
| Is sorted codes a problem?Peter LarssonHelsingborg, Sweden |
 |
|
|
chipembele
Posting Yak Master
106 Posts |
Posted - 2006-12-13 : 02:50:39
|
quote: Originally posted by Peso Is sorted codes a problem?Peter LarssonHelsingborg, Sweden
not sure what you mean Peter? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-13 : 03:06:47
|
| Ah!Now I realize you mean "problem sorted out"... My badPeter LarssonHelsingborg, Sweden |
 |
|
|
|