Solved

Can I use complex CASE conditioning in SQL 2005 with the SUM funtion?

Posted on 2014-10-19
3
280 Views
Last Modified: 2014-10-19
Hi All,

I'm trying to run the following sql query but I get an error msg. What am I doing wrong?


SELECT Owner.OwnerID, Owner.OwnerNumber,

sum
(case
when SoldInventory.RoomType='Superior' and SoldInventory.Week>=15 and SoldInventory.Week<=50 then 22
else
when SoldInventory.RoomType='Deluxe' and SoldInventory.Week>=15 and SoldInventory.Week<=50 then 26
else
when SoldInventory.RoomType='Royal' and SoldInventory.Week>=15 and SoldInventory.Week<=50 then 32
else
when SoldInventory.RoomType='Superior' then 35
else
when SoldInventory.RoomType='Deluxe' then 39
else  
when SoldInventory.RoomType='Royal' then 45
else 0
end) as VotesABC

FROM   Owner AS Owner INNER JOIN Contract AS Contract
ON Owner.OwnerID = Contract.OwnerID
INNER JOIN SoldInventory AS SoldInventory
ON Contract.ContractID = SoldInventory.ContractID

WHERE     (Contract.ContractStatus = 'Active')

group BY Owner.OwnerID, Owner.OwnerNumber

Thanks for your help.

Errol
0
Comment
Question by:Errol Farro
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 40391013
Telling us the error message would have been nice. However, you might try leaving out all but one of the "else" lines:
select	Owner.OwnerID, 
		Owner.OwnerNumber, 
		sum	(	case 
					when si.RoomType='Superior'	and si.Week>=15 and si.Week<=50 	then 22 
					when si.RoomType='Deluxe' 	and si.Week>=15 and si.Week<=50 	then 26
					when si.RoomType='Royal' 	and si.Week>=15 and si.Week<=50 	then 32
					when si.RoomType='Superior' 	then 35
					when si.RoomType='Deluxe' 	then 39
					when si.RoomType='Royal' 	then 45
					else 0 
				end) as 'VotesABC'
from   dbo.Owner
inner joinContract			on Owner.OwnerID = Contract.OwnerID 
inner join SoldInventory si		on Contract.ContractID = si.ContractID
where ( Contract.ContractStatus = 'Active'	)
group by Owner.OwnerID, Owner.OwnerNumber

Open in new window


hth

Mike
0
 

Author Closing Comment

by:Errol Farro
ID: 40391024
Thanks a lot Mike. That took care of the errors. Next time I will add the actual errors. Best regards. Errol
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 40391209
Good to hear!

Mike

P.S. sorry about the missing space :)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question