| Author |
Topic |
|
hamdanian
Starting Member
4 Posts |
Posted - 2010-06-23 : 17:33:44
|
| Campus Numberaccts NumberDaysOutstandingAvgDaysC1 C2 C3 Total I'm trying to create the above table in a stored procedure but I'm getting two error messages:1st error message 1st line: Cannot perform an aggregate function on an expression containing an aggregate or a subquery. in 1st line(COUNT(Case B.Descrip When 'Ready to Review' THEN B.Descrip ELSE Null END) AS NumberAccts2nd error message 8th line: Invalid column name 'NumberDaysOutstanding'. Invalid column name 'NumberAccts'.Select A.Descrip as Camp, COUNT(Case B.Descrip When 'Ready to Review' THEN B.Descrip ELSE Null END) AS NumberAccts, MAX(DateDiff(day,(SELECT cast(convert(char(12),max(datelstmod) ,101) as datetime) FROM C where C.RecordID = E.FaStudentAyID and Columnname like 'Package Status'),GetDate())) AS NumberDaysOutstanding, NumberDaysOutstanding/NumberAccts AS Avg_DaysFROM D JOIN A ON D.SyCampusID = A.SyCampusID JOIN E ON D.AdEnrollID = E.AdEnrollID JOIN B ON B.FaPackStatusID = E.FaPackStatusIDJOIN C ON C.RecordID = E.FaStudentAyID Group BY A.Descrip |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-06-24 : 03:06:54
|
| Wow, this query was quite messy. COUNT(Case B.Descrip When 'Ready to Review' THEN B.Descrip ELSE Null END) AS NumberAcctsshould be changed to SUM(Case When B.Descrip = 'Ready to Review' THEN 1 ELSE 0 END) AS NumberAcctsThe rest of the query ill have to be explained forst...I don't get it.- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
hamdanian
Starting Member
4 Posts |
Posted - 2010-06-24 : 16:09:34
|
| This Worked thanks!! but how do I add one extra column which divides column c with column b. In the last selection NumberDaysOutstanding/NumberAccts AS Avg did not work.Select SyCampus.Descrip as Campus, SUM(Case WHEN B.Descrip = 'Ready to Review' THEN 1 Else 0 END) AS NumberAccts, MAX(DateDiff(day,C.datelstmod,GetDate())) AS NumberDaysOutstanding, NumberDaysOutstanding/NumberAccts AS AvgFROM D JOIN A ON D.SyCampusID = A.SyCampusID JOIN E ON D.AdEnrollID = E.AdEnrollID JOIN B ON B.FaPackStatusID = E.FaPackStatusIDJOIN C ON C.RecordID = E.FaStudentAyID Group BY A.Descrip |
 |
|
|
hamdanian
Starting Member
4 Posts |
Posted - 2010-06-24 : 16:37:47
|
| I also want to add a total Row at the bottom that totals all the values in C1, C2, and C3 for each column. The report will look like this:Campus Numberaccts NumberDaysOutstanding AvgDaysC1 C2 C3 Total Does anyone know how to do that? |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-06-25 : 02:31:11
|
Try this:WITH cte AS ( Select SyCampus.Descrip as Campus, SUM(Case WHEN B.Descrip = 'Ready to Review' THEN 1 Else 0 END) AS NumberAccts, MAX(DateDiff(day,C.datelstmod,GetDate())) AS NumberDaysOutstanding, FROM D JOIN A ON D.SyCampusID = A.SyCampusID JOIN E ON D.AdEnrollID = E.AdEnrollID JOIN B ON B.FaPackStatusID = E.FaPackStatusID JOIN C ON C.RecordID = E.FaStudentAyID Group BY A.Descrip )SELECT *, NumberDaysOutstanding/NumberAccts AS [Avg]FROM cte The total at the bottom should be calculated in your presentation layer (where you create the actual report layout).- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-06-25 : 02:32:07
|
| And next time you post TSQL please add [ code ] tags around it...it preserves the formatting and makes it alot easier to read.- LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
hamdanian
Starting Member
4 Posts |
Posted - 2010-06-25 : 18:15:59
|
| I should have mentioned it earlier, but I'm creating a stored procedure and what Lumbago gave me did not work in the stored procedure. This is the error message I received: Incorrect syntax near 'cte'.However, I ran your query without the stored procedure and I got this error message: Divide by zero error encountered. Lumbago I don't know if you could answer both questions, hopefully you can... you seem knowledgeable. |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-06-28 : 03:10:02
|
Some small ajustments then:CREATE PROCEDURE dbo.ProcNameASWITH cte AS ( Select SyCampus.Descrip as Campus, SUM(Case WHEN B.Descrip = 'Ready to Review' THEN 1 Else 0 END) AS NumberAccts, MAX(DateDiff(day,C.datelstmod,GetDate())) AS NumberDaysOutstanding FROM D JOIN A ON D.SyCampusID = A.SyCampusID JOIN E ON D.AdEnrollID = E.AdEnrollID JOIN B ON B.FaPackStatusID = E.FaPackStatusID JOIN C ON C.RecordID = E.FaStudentAyID Group BY A.Descrip )SELECT *, [Avg] = CASE WHEN NumberAccts = 0 THEN 0 ELSE NumberDaysOutstanding/NumberAccts ENDFROM cte - LumbagoMy blog (yes, I have a blog now! just not that much content yet) -> www.thefirstsql.com |
 |
|
|
|