Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-19
3
Medium Priority
?
294 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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