Solved

Using IF THEN ELSE In SQL Views

Posted on 2009-07-06
10
240 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
[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
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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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
 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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.​
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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