Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-11-03 : 13:16:26
|
| Hello,I have the following select statement which works fine:select o.prefix as Prefix,o.organizationName as Organization, SUM(a.amount) as TotalSpent, o.AwardBucketMoney as Allocation,o.AwardBucketMoney-Sum(a.amount) as Remainder, SUM(a.amount)/o.AwardBucketMoney AS PercentSpent from AwardOrganizations oinner join LU_CostCenters c on o.Prefix = SUBSTRING(c.OrgCode,1,2)INNER JOIN Position R ON R.Pos_LU_CostCenters_ID=c.LU_CostCenters_IDinner join Person p on p.Person_ID=R.Pos_Person_IDinner join Awards a on a.AWA_Person_ID = p.Person_ID where o.PartOfMedCtr = 1group by o.OrganizationName, o.AwardBucketMoney,o.PrefixI would like to return one more column, which is the SUM(a.amount) where a.AwardType = 4.I'm not sure how to put a WHERE clause for just one of the columns, like this. What I tried (but gave an error) was:select o.prefix as Prefix,o.organizationName as Organization, SUM(a.amount) as TotalSpent, SUM(a.Amount) where a.AwardType=4 as TotalPerfSpento.AwardBucketMoney as Allocation,o.AwardBucketMoney-Sum(a.amount) as Remainder, SUM(a.amount)/o.AwardBucketMoney AS PercentSpent from AwardOrganizations oinner join LU_CostCenters c on o.Prefix = SUBSTRING(c.OrgCode,1,2)INNER JOIN Position R ON R.Pos_LU_CostCenters_ID=c.LU_CostCenters_IDinner join Person p on p.Person_ID=R.Pos_Person_IDinner join Awards a on a.AWA_Person_ID = p.Person_ID where o.PartOfMedCtr = 1group by o.OrganizationName, o.AwardBucketMoney,o.Prefix |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-11-03 : 13:28:02
|
This???? SELECT o.prefix as Prefix , o.organizationName as Organization , SUM(a.amount) as TotalSpent , o.AwardBucketMoney as Allocation , o.AwardBucketMoney-Sum(a.amount) as Remainder , SUM(a.amount)/o.AwardBucketMoney AS PercentSpent from AwardOrganizations oINNER JOIN LU_CostCenters c ON o.Prefix = SUBSTRING(c.OrgCode,1,2) -- should cause a scanINNER JOIN PositiON R ON R.Pos_LU_CostCenters_ID=c.LU_CostCenters_IDINNER JOIN PersON p ON p.Person_ID=R.Pos_Person_IDINNER JOIN Awards a ON a.AWA_Person_ID = p.Person_ID INNER JOIN ( AWA_Person_ID, SUM(amount) AS SUM_amount FROM Awards where AwardType = 4) AS XXX ON XXX.AWA_Person_ID = p.Person_ID WHERE o.PartOfMedCtr = 1 GROUP BY o.OrganizationName, o.AwardBucketMoney,o.Prefix you really should format your code betterBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
DaveBF
Yak Posting Veteran
89 Posts |
Posted - 2010-11-03 : 13:50:00
|
| The statement you suggested is giving Invalid Object Name on AWA_Person_ID. (4th line from bottom) I'd try to fix it but I'm not sure what you're trying to do. It also gives an error on the same line, on SUM(amount), saying "an expression of non-boolean type, where a condition is expected".I'll format better in the future. Thanks. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-03 : 15:34:44
|
Try:SUM(CASE WHEN a.AwardType=4 THEN a.Amount ELSE 0 END) AS TotalPerfSpentin your SELECT list. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|