Solved

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

Posted on 2014-10-19
3
279 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now