Solved

Multiple CASE statements in SELECT

Posted on 2012-03-27
7
360 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 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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