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
HarreniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
something like this:

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 exists 
			 (
			 SELECT *  
			 FROM DatasetsNEEDSACTIVATION DSNA
                         WHERE DSNA.ConsumerID = C.ConsumerID  and DSI.DatasetID = DatasetID
			 )

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
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

HarreniAuthor Commented:
Many thanks Eric, working fine with me
HarreniAuthor Commented:
sorry Mr.vitor;  I just see your reply after I post my reply
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.