Solved

Using IF THEN ELSE In SQL Views

Posted on 2009-07-06
10
235 Views
Last Modified: 2012-05-07
I have three different database fields (from different tables) that depending upon the scenario need to be the field that is used to be picked up by reports as a selection criteria.

I need to intgegrate an IF THEN ELSE into a SQL view so that it shows this value regardless. How do i do this? The concept of the query is as follows:

IF order.custA = 'ABC' then order.custB
Else if stock.cust is null then sales.cust
Else stock.cust

In every case it is a string value. Ideally i want to be able to pass that result into Crystal as a parameter. I have tried doing this in Crystal and it kills the performance, so really need to pass this directly from SQL.

Any ideas would be appreciated.

Thanks in advance.
0
Comment
Question by:delkent
10 Comments
 
LVL 19

Expert Comment

by:Melih SARICA
ID: 24791794
select case when order.custa ='ABC' then order.custb when stock.cust is null then sales.cust else stock.cust end column_name from order,sales etc
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24791821
I am guessing you have all three tables joined in in a single query. In that case you can do something like this in your select statement:
 

SELECT *,

       CASE WHEN order.custA = 'ABC' THEN order.custB

            WHEN stock.cust is null THEN sales.cust

            ELSE stock.cust

       END as Customer

From  Yourtable INNER JOIN OtherTable....

Open in new window

0
 
LVL 14

Expert Comment

by:shru_0409
ID: 24791945
select custA, case when order.custA = 'ABC' THEN order.custB when stock.cust is null then sales.cust when stock.cust = 'xyz' then custC ELSE abc end "order_name"
from order;


try this
0
 

Author Comment

by:delkent
ID: 24792018
Thanks, looks promising however it doesn't accept the alias for the Case statement - just says invalid
column name 'hocode'. This is what I have put in:

SELECT CASE WHEN order.custcode = 'WASTE' AND order.job <> 0 THEN order_job.custcode WHEN stock.job IS NULL
                      THEN cust.hocode ELSE stock_job.custcode END AS hocode, dbo.order.ordnum
FROM         dbo.order INNER JOIN
                      dbo.orderdetail ON dbo.order.orderdid = dbo.orderdetail.orderdid INNER JOIN
                      dbo.stock ON dbo.orderdetail.stockid = dbo.stock.stockid INNER JOIN
                      dbo.cust ON dbo.order.custcode = dbo.cust.custcode LEFT OUTER JOIN
                      dbo.job AS order_job ON dbo.ordhed.job = order_job.job LEFT OUTER JOIN
                      dbo.job AS stock_job ON dbo.stock.job = lot_job.job
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24792046
Try it this way:


SELECT CASE WHEN [order].[custcode] = 'WASTE' AND [order].[job] <> 0 THEN [order_job].[custcode] 

			WHEN [stock].[job] IS NULL THEN [cust].[hocode] 

            ELSE [stock_job].[custcode] 

       END AS 'HOCODE', 

	   [dbo].[order].[ordnum]

FROM   [dbo].[order] INNER JOIN

                      [dbo].[orderdetail] ON [dbo].[order].[orderdid] = [dbo].[orderdetail].[orderdid] INNER JOIN

                      [dbo].[stock] ON [dbo].[orderdetail].[stockid] = [dbo].[stock].[stockid] INNER JOIN

                      [dbo].[cust] ON [dbo].[order].[custcode] = [dbo].[cust].[custcode] LEFT OUTER JOIN

                      [dbo].[job] AS order_job ON [dbo].[ordhed].[job] = [order_job].[job] LEFT OUTER JOIN

                      [dbo].[job] AS stock_job ON [dbo].[stock].[job] = [lot_job].[job]

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:delkent
ID: 24792076
Thanks but still says invalid column name 'hocode' on save
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24792093
Can you try changing the name to see if it works with a different alias? Also, I see that you get an error when saving. You meant when running the query right?
P.
0
 

Author Comment

by:delkent
ID: 24792099
Have changed name and it still errors.
It occurs either when running the query or try to save the query as a view
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 500 total points
ID: 24792128
Try running it without an alias to see if it still errors out. Between the syntax looks fine not sure whats going on. I'll try to do some testing as well.
P.
0
 

Author Closing Comment

by:delkent
ID: 31600482
Has now worked fine. Thank you for your help on this
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

19 Experts available now in Live!

Get 1:1 Help Now