Solved

MSSQL SELECT WITH CASE STATEMENT IN WHERE CLAUSE

Posted on 2014-02-27
5
571 Views
Last Modified: 2014-02-27
Hello,
I'm trying to do something like this :

select table1.A

from table1 , (select X , Y , Z  from table2 WHERE clause condition ) SUB

where table1.B = 0 and table1.C = 1 and

if  sub.X = 1 => table1.Y = SUB.Y

elseif sub.X = 0 => table1.Y not in (select Y  from table2 WHERE clause condition)

else if there is no result in the sub query , no join with sub, return all the result from table1....)

/*******/

in fact I have a table of product. (table1)

and I have another table (table2)  where I managed the right access for a customer.

if there is no entry in table2, no restriction for the customer in table1 (last else)

if there is an entry in table2, there is a restriction for the customer for  specific product.

there is 2 type of restriction, in the X column (bit).

0 = no access for this product (but yes access to other product)
1 = only access for this product  (so no access for other product)

Many thanks for your help.
0
Comment
Question by:bruno_boccara
  • 4
5 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39892282
Would've been easier with real table/field names:
SELECT	a.A
from	table1	a
LEFT
JOIN	table2	b	ON	a.Y = b.Y
			AND	b.X = 1
WHERE	a.B = 0
AND	a.C = 1

UNION

SELECT	a.A
from	table1	a
LEFT
JOIN	table2	b	ON	a.Y = b.Y
			AND	b.X = 0
WHERE	a.B = 0
AND	a.C = 1
AND	b.Y IS NULL

Open in new window

0
 

Author Comment

by:bruno_boccara
ID: 39892295
In fact, I made this query:

SELECT DISTINCT E.COL_ED 

FROM EDITEUR E , (SELECT COL_ED , BLOCK_TYPE , CTNUM , PLATFORME FROM RE_ED_BLOCK REB WHERE CTNUM = '0000008909' AND PLATFORME = 'INTRANET') SUB

WHERE Interne = 0 AND ED_ACTUEL = 1

AND (

(SUB.BLOCK_TYPE = 1 AND E.COL_ED = SUB.COL_ED)   /* IT WORKS */

OR

(SUB.BLOCK_TYPE = 0 AND E.COL_ED NOT IN (SELECT COL_ED FROM RE_ED_BLOCK REB WHERE CTNUM = SUB.CTNUM AND PLATFORME = SUB.PLATFORME ))  /* IT WORKS */

 OR (NOT EXISTS(SELECT COL_ED FROM RE_ED_BLOCK REB WHERE CTNUM = SUB.CTNUM AND PLATFORME = SUB.PLATFORME) AND E.COL_ED = E.COL_ED)  /* IT DOESN'T WORKS */
)

Open in new window


the third OR condition do not work.  I want to avoid a join with SUB table if there is no entry and to return all entries from EDITEUR  (with only condition Interne = 0 AND ED_ACTUEL = 1 )

Many thanks for any help.
0
 

Author Comment

by:bruno_boccara
ID: 39892319
Hello John,
I can't make a left outer join between table1 (EDITEUR) and table2 (RE_ED_BLOCK).
0
 

Author Comment

by:bruno_boccara
ID: 39892463
One way is to do this with INTERSERCT or EXCEPT, but I have a problem when there is no entry in the second table (RE_ED_BLOCK ). I can't return in this case entries from table1 (EDITEUR )
0
 

Author Closing Comment

by:bruno_boccara
ID: 39892547
this could be a solution if I could make a left outer join between the two table.

anyway, I found a different way , using fucntion, intersect and except....
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

861 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

26 Experts available now in Live!

Get 1:1 Help Now