Solved

Multiple CASE statements in SELECT

Posted on 2012-03-27
7
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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