Solved

MSSQL SELECT WITH CASE STATEMENT IN WHERE CLAUSE

Posted on 2014-02-27
5
568 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hello John,
I can't make a left outer join between table1 (EDITEUR) and table2 (RE_ED_BLOCK).
0
 

Author Comment

by:bruno_boccara
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.​
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

771 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

16 Experts available now in Live!

Get 1:1 Help Now