Solved

Multiple CASE statements in SELECT

Posted on 2012-03-27
7
356 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 …
In this article I will describe the Copy Database Wizard 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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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