Author |
Topic |
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 07:21:44
|
Hi,I have this query, SELECT t.student_id, t.week_noFROM dbo.StudAtt TCROSS APPLY (SELECT TOP 3 week_no FROM dbo.StudAtt GROUP BY student_id, acad_period, week_no HAVING student_id = t.student_id AND week_no < t.week_no AND (acad_period = '08/09') AND (CASE WHEN (SUM(t.Actual)) > 0 AND (SUM(t.Meets) > 0) THEN ((SUM(t.Actual) * 1.0) / (SUM(t.Meets) * 1.0) * 100) ELSE '0' END = 0) ORDER BY week_no DESC) t1OUTER APPLY (SELECT COUNT(week_no) AS PresentCount FROM dbo.StudAtt GROUP BY student_id, acad_period HAVING student_id = t.student_id AND week_no >= t1.week_no AND week_no <= t.week_no AND (acad_period = '08/09') AND (CASE WHEN (SUM(t.Actual)) > 0 AND (SUM(t.Meets) > 0) THEN ((SUM(t.Actual) * 1.0) / (SUM(t.Meets) * 1.0) * 100) ELSE '0' END > 0) ) t2GROUP BY t.student_id, t.acad_period, t.week_noHAVING (t.acad_period = '08/09') AND (CASE WHEN (SUM(t.Actual)) > 0 AND (SUM(t.Meets) > 0) THEN ((SUM(t.Actual) * 1.0) / (SUM(t.Meets) * 1.0) * 100) ELSE '0' END = 0) AND ISNULL(t2.PresentCount, 0) = 0ORDER BY t.student_id, t.week_noAnd i get the following error: Aggregates on the right side of an APPLY cannot reference columns from the left side.Any help/advice on what that means? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-04 : 07:27:24
|
You need to first agrregate the columns on left part and form a dervied table out of them. Then take CROSS APPLY with this derived table to get the reqd result. |
 |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 08:31:03
|
any chance of an example?quote: Originally posted by visakh16 You need to first agrregate the columns on left part and form a dervied table out of them. Then take CROSS APPLY with this derived table to get the reqd result.
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-04 : 11:08:03
|
Do you know what your HAVING part is doing?HAVING CASE WHEN SUM(t.Actual) > 0 AND SUM(t.Meets) > 0 THEN 100.0 * SUM(t.Actual) / SUM(t.Meets) ELSE 0 END = 0 If SUM(t.Actual) and SUM(t.Meets) both are positive then you divide those two and you only want the record back if the division is equal to 0. Then only division that is zero is when the upper value is zero.In all other cases, you use zero to check against zero.This can be simplified asHAVING CASE WHEN SUM(t.Actual) > 0 THEN SUM(t.Actual) ELSE 0 END = 0 But then again, if SUM(t.Actual) is GREATER than zero then you check for SUM(t.Actual) is EQUAL to zero, which will NEVER happen! First CASE will never evaluate to true.So this is what you can use instead of original CASEHAVING SUM(t.Actual) <= 0 E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-04 : 11:15:47
|
I think this rewrite does the same thing as your original monster querySELECT studentID, weekNoFROM ( SELECT t.studentID, t.weekNo, t.Actual ROW_NUMBER() OVER (PARTITION BY t.studentID ORDER BY t.weekNo DESC) AS recID, COUNT(*) OVER (PARTITION BY t.studentID) AS pCount FROM dbo.studAtt AS t WHERE t.acad_period = '08/09' ) AS wWHERE recID <= 3GROUP BY student_id, week_noHAVING SUM(Actual) <= 0 AND MAX(pCount) = 0 E 12°55'05.63"N 56°04'39.26" |
 |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 11:17:19
|
Ok, ive changed the query to:SELECT t.student_id, t.week_noFROM dbo.StudAtt TCROSS APPLY (SELECT TOP 3 week_noFROM dbo.StudAttGROUP BY student_id, acad_period, week_noHAVING student_id = t.student_idAND week_no < t.week_noAND (acad_period = '08/09')AND SUM(t.Actual) <= 0ORDER BY week_no DESC) t1OUTER APPLY (SELECT COUNT(week_no) AS PresentCountFROM dbo.StudAttGROUP BY student_id, acad_periodHAVING student_id = t.student_idAND week_no >= t1.week_noAND week_no <= t.week_noAND (acad_period = '08/09')AND SUM(t.Actual) > 0 ) t2GROUP BY t.student_id, t.acad_period, t.week_noHAVING (t.acad_period = '08/09') AND SUM(t.Actual) <= 0 AND ISNULL(t2.PresentCount, 0) = 0ORDER BY t.student_id, t.week_noand still get aggregates error;Aggregates on the right side of an APPLY cannot reference columns from the left side.quote: Originally posted by Peso Do you know what your HAVING part is doing?HAVING CASE WHEN SUM(t.Actual) > 0 AND SUM(t.Meets) > 0 THEN 100.0 * SUM(t.Actual) / SUM(t.Meets) ELSE 0 END = 0 If SUM(t.Actual) and SUM(t.Meets) both are positive then you divide those two and you only want the record back if the division is equal to 0. Then only division that is zero is when the upper value is zero.In all other cases, you use zero to check against zero.This can be simplified asHAVING CASE WHEN SUM(t.Actual) > 0 THEN SUM(t.Actual) ELSE 0 END = 0 But then again, if SUM(t.Actual) is GREATER than zero then you check for SUM(t.Actual) is EQUAL to zero, which will NEVER happen! First CASE will never evaluate to true.So this is what you can use instead of original CASEHAVING SUM(t.Actual) <= 0 E 12°55'05.63"N 56°04'39.26"
|
 |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 11:25:19
|
I get very funny error with this query, line 7 '('quote: Originally posted by Peso I think this rewrite does the same thing as your original monster querySELECT studentID, weekNoFROM ( SELECT t.studentID, t.weekNo, t.Actual ROW_NUMBER() OVER (PARTITION BY t.studentID ORDER BY t.weekNo DESC) AS recID, COUNT(*) OVER (PARTITION BY t.studentID) AS pCount FROM dbo.studAtt AS t WHERE t.acad_period = '08/09' ) AS wWHERE recID <= 3GROUP BY student_id, week_noHAVING SUM(Actual) <= 0 AND MAX(pCount) = 0 E 12°55'05.63"N 56°04'39.26"
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-04 : 11:29:16
|
Put a comma after t.Actual in the derived table w.And try again. E 12°55'05.63"N 56°04'39.26" |
 |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 11:32:41
|
thanks,the aim of my query was to get the students who have been absent for more than 3 weeks in a row.quote: Originally posted by Peso Put a comma after t.Actual in the derived table w.And try again. E 12°55'05.63"N 56°04'39.26"
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-04 : 11:44:39
|
Yup but i kept getting no more help :(Is there any way to apply some logic thatll identify students that have 0 SUM(Actual) = 0 for 4 consecutive weeks?quote: Originally posted by Peso Oh, so this is really a continuation of this topic?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113717 E 12°55'05.63"N 56°04'39.26"
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-07 : 09:00:53
|
Ive changed your query to the following below:But I get a operand type char is invalid sum operator, ive played around with the grouping and stil get same error, any advice?DECLARE @Sample TABLE(student_id CHAR(15),week_no TINYINT,Actual CHAR(1))INSERT @Sample(student_id,week_no,Actual)SELECT dbo.stmbiogr.student_id, dbo.StudAtt.week_no, SUM(StudAtt.Actual) AS ActualFROM dbo.stmbiogr INNER JOIN dbo.StudAtt ON dbo.stmbiogr.student_id = dbo.StudAtt.student_idWHERE (dbo.StudAtt.acad_period = '08/09')GROUP BY dbo.stmbiogr.student_id, dbo.StudAtt.week_noHAVING (SUM(StudAtt.Actual) = 0)SELECT DISTINCT s1.student_idFROM @Sample AS s1INNER JOIN @Sample AS s2 ON s2.student_id = s1.student_idAND s2.week_no = s1.week_no + 1AND (SUM(s2.Actual) = 0)INNER JOIN @Sample AS s3 ON s3.student_id = s2.student_idAND s3.week_no = s2.week_no + 1AND (SUM(s3.Actual) = 0)WHERE (SUM(s1.Actual) = 0)quote: Originally posted by Peso See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113717I have posted two different solutions there. E 12°55'05.63"N 56°04'39.26"
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 09:11:35
|
you cant sum on char datatype. use MAX() or MIN() depending on your requirement instead of SUM() |
 |
|
cipriani1984
Constraint Violating Yak Guru
304 Posts |
Posted - 2008-11-07 : 09:14:49
|
But max and min doesnt give the same value as sum do they?quote: Originally posted by visakh16 you cant sum on char datatype. use MAX() or MIN() depending on your requirement instead of SUM()
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 09:29:08
|
just posted a suggestion here. is this what you want?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113717 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-07 : 09:31:16
|
We have no idea that ACTUAL represents. E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 09:37:22
|
quote: Originally posted by Peso We have no idea that ACTUAL represents. E 12°55'05.63"N 56°04'39.26"
Seeing the datatype (CHAR(1)) i assume its Absent bit, though guarantee on it |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-07 : 09:41:05
|
In other topic there were a reference to an "absent" column, which was "y" or "n". E 12°55'05.63"N 56°04'39.26" |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 09:45:19
|
quote: Originally posted by Peso In other topic there were a reference to an "absent" column, which was "y" or "n". E 12°55'05.63"N 56°04'39.26"
yeah...seems like the original column name was Actual rather than absent |
 |
|
Next Page
|