Avatar of Harreni
Harreni
Flag for Saudi Arabia asked on

Filtering T-SQL statement based on 2 combined values in a SUB-QUERY

Hi Experts,

I want to filter my query below based on 2 combined values from a sub-query which is (ConsumerID nvarcha(255) PK &  DatasetID int) using Exists OR IN Operators. So, how to do that?

SELECT 
 C.ConsumerID 
, C.ConsumerName
, S.ServiceArabicName
, DSI.ServiceID
, DSI.DatasetID
, DSI.DatasetActivatedToUsedByService 
,DS.DatasetName
, DS.DatasetAvaiableOnGSB
FROM   Datasets_Services_Intersection DSI INNER JOIN
             Datasets DS ON DSI.DatasetID = DS.DatasetID INNER JOIN
             Services S ON DSI.ServiceID = S.ServiceID INNER JOIN
             Consumers C ON S.ConsumerID = C.ConsumerID

	  WHERE C.ConsumerID & DatasetID in
			 (
			 SELECT DSNA.ConsumerID,DSI.DatasetID  
			 FROM DatasetsNEEDSACTIVATION DSNA
			 )

Open in new window



Thanks a lot in advance,
Harreni
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Harreni

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

Why not JOIN the table with the other, avoiding a subquery?
SELECT 
 C.ConsumerID 
, C.ConsumerName
, S.ServiceArabicName
, DSI.ServiceID
, DSI.DatasetID
, DSI.DatasetActivatedToUsedByService 
,DS.DatasetName
, DS.DatasetAvaiableOnGSB
FROM   Datasets_Services_Intersection DSI 
    INNER JOIN Datasets DS ON DSI.DatasetID = DS.DatasetID 
    INNER JOIN Services S ON DSI.ServiceID = S.ServiceID 
    INNER JOIN Consumers C ON S.ConsumerID = C.ConsumerID
    INNER JOIN DatasetsNEEDSACTIVATION DSNA ON DSNA.ConsumerID=C.ConsumerID 
WHERE DSNA.DatasetID = DSI.DatasetID

Open in new window

Harreni

ASKER
Many thanks Eric, working fine with me
Harreni

ASKER
sorry Mr.vitor;  I just see your reply after I post my reply
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck