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

x
?
Solved

Multiple CASE statements in SELECT

Posted on 2012-03-27
7
Medium Priority
?
368 Views
Last Modified: 2012-03-27
Hi Experts,

Can I use the result from one CASE statment in another? my SELECT statement is...

SELECT DISTINCT
                      dbo.tblMestecData.idMESTEC, dbo.tblMestecData.MESDATE, dbo.tblMestecData.LOCATION, dbo.tblMestecData.ALLOCATION, dbo.tblMestecData.OWNERSHIPTYPE,
                      dbo.tblMestecData.PARTNBR, dbo.tblMestecData.VERSION, dbo.tblMestecData.PARTDESCRIPTION, dbo.tblMestecData.QTY, dbo.tblMestecData.TRANSTYPE,
                      dbo.tblMestecData.REASONCODE, dbo.tblMestecData.REASONDESC, dbo.tblMestecData.TRANSCOMMENT, dbo.tblMestecData.GRNNBR,
                      dbo.tblMestecData.SERIALNBR, dbo.tblMestecData.CURRENCY, dbo.tblMestecData.UNITCOST, dbo.tblMestecData.TOTALBASELINECOST,
                      dbo.tblMestecData.COMPARTNBR, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Price ELSE V1Price END AS DEFV1PRICE,
                      CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END AS V1_CURRENCY,
                      CASE WHEN V1_CURRENCY = 'GBP' THEN 0 ELSE USD END AS EX_RATE

Im getting the error 'Invalid column name V1_CURRENCY'

Thanks,
Dean.
0
Comment
Question by:deanlee17
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 2000 total points
ID: 37770270
That is because V1_CURRENCY is an alias. An alias is only applied to the final result. What you have to do is replace the V1_CURRENCY with the line that originated it:
CASE WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END= 'GBP' THEN 0 ELSE USD END AS EX_RATE
0
 
LVL 6

Expert Comment

by:ingriT
ID: 37770274
You can't use VI_CURRENCY column that you have just created above, you should do something like this:

SELECT DISTINCT 
	dbo.tblMestecData.idMESTEC, dbo.tblMestecData.MESDATE, dbo.tblMestecData.LOCATION, dbo.tblMestecData.ALLOCATION, dbo.tblMestecData.OWNERSHIPTYPE, 
	dbo.tblMestecData.PARTNBR, dbo.tblMestecData.VERSION, dbo.tblMestecData.PARTDESCRIPTION, dbo.tblMestecData.QTY, dbo.tblMestecData.TRANSTYPE, 
	dbo.tblMestecData.REASONCODE, dbo.tblMestecData.REASONDESC, dbo.tblMestecData.TRANSCOMMENT, dbo.tblMestecData.GRNNBR, 
	dbo.tblMestecData.SERIALNBR, dbo.tblMestecData.CURRENCY, dbo.tblMestecData.UNITCOST, dbo.tblMestecData.TOTALBASELINECOST,
	dbo.tblMestecData.COMPARTNBR, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Price ELSE V1Price END AS DEFV1PRICE, 
	CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END AS V1_CURRENCY, 
		CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 AND Currency = 'GBP' THEN 0
		CASE WHEN NewCurrency = 'GBP' THEN 0
		ELSE USD END AS EX_RATE

Open in new window

0
 

Author Comment

by:deanlee17
ID: 37770311
Ok so in a case statment how could I have multiple else statements....

CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND  UNITCOST > 0 THEN Currency ELSE V1Currency END
= 'GBP' THEN 0 ELSEIF
='EUR' then EURO ELSE
0


The values for USD and EURO are stored in their own table.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 18

Expert Comment

by:Cluskitt
ID: 37770319
You would have to use them on each statement:
CASE WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END= 'GBP' THEN 0
WHEN CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END='EUR' then EURO
ELSE 0 END
0
 
LVL 6

Expert Comment

by:ingriT
ID: 37770321
To have multiple case statements you can add case when ... then ... for every line until you set an else ..., that is the last statement in the case.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37770328
Another option would be to construct a smaller subquery as a table and use that as the field. Something like:
SELECT DISTINCT
                      dbo.tblMestecData.idMESTEC, dbo.tblMestecData.MESDATE, dbo.tblMestecData.LOCATION, dbo.tblMestecData.ALLOCATION, dbo.tblMestecData.OWNERSHIPTYPE,
                      dbo.tblMestecData.PARTNBR, dbo.tblMestecData.VERSION, dbo.tblMestecData.PARTDESCRIPTION, dbo.tblMestecData.QTY, dbo.tblMestecData.TRANSTYPE,
                      dbo.tblMestecData.REASONCODE, dbo.tblMestecData.REASONDESC, dbo.tblMestecData.TRANSCOMMENT, dbo.tblMestecData.GRNNBR,
                      dbo.tblMestecData.SERIALNBR, dbo.tblMestecData.CURRENCY, dbo.tblMestecData.UNITCOST, dbo.tblMestecData.TOTALBASELINECOST,
                      dbo.tblMestecData.COMPARTNBR, CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Price ELSE V1Price END AS DEFV1PRICE,
                      V1_CURRENCY,
                      CASE WHEN V1_CURRENCY = 'GBP' THEN 0 ELSE USD END AS EX_RATE
FROM MyTable INNER JOIN
(SELECT DISTINCT CASE WHEN OWNERSHIPTYPE = 'VMI Thales' AND UNITCOST > 0 THEN Currency ELSE NewCurrency END AS V!_CURRENCY, MyIDField ID2 FROM MyTable ON MyIDField=ID2

Open in new window


You would have to adapt it to your complete query, but that is the theory. However, it would be more efficient to work with multiple cases, as those would require just a simple logical computation, instead of the creation and joining of a temporary table.
0
 

Author Comment

by:deanlee17
ID: 37770507
Excellent info guys. I will award the points to Cluskitt's first answer.
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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

783 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