Solved

Multiple CASE statements in SELECT

Posted on 2012-03-27
7
361 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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