Solved

Using IF THEN ELSE In SQL Views

Posted on 2009-07-06
10
234 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

9 Experts available now in Live!

Get 1:1 Help Now