Element Present in One group must present with oth

2019-08-06 03:53发布

问题:

Seeking help on this complicated scenario.

I have three table called as Interchange, Driver and Application

ALL SKU from APPLICATION table which are present in Interchange table with Checks as 'REP' and available with column called as 'Baseline' and Correspondance NewSKU which present with Newline present In Driver Table

Tables:-

DECLARE @Interchange TABLE
(
SKU VARCHAR (50),
CHECKS VARCHAR (50),
NewSKU VARCHAR (50)
)


INSERT @Interchange
SELECT 'AEM41-1408DS',  'Bridgestone',  '41-1408DS' UNION ALL
SELECT 'AEM41-1408DS',  'REP',  'AEM21-203DK'


DECLARE @Driver TABLE
(
BaseLine VARCHAR (50),
NewLine VARCHAR (50)
)



INSERT @Driver

SELECT 'Cold Air Intake',   'Air Filter%'


DECLARE @APPLICATION TABLE
(   
SKU VARCHAR (50),
line VARCHAR (50)
)

INSERT @APPLICATION


SELECT 'AEM41-1408DS',  'Cold Air Intake' UNION ALL
SELECT 'AEM41-1408DS',  'Cold Air Intake' UNION ALL
SELECT 'AEM41-1408DS',  'Cold Air Intake' UNION ALL
SELECT 'AEM41-1408DS',  'Cold Air Intake' UNION ALL
SELECT 'AEM21-203DK',   'Air Filter High'

Can any one please suggest t-sql query for this complicated scenario?

回答1:

As I understand with you comments, I have written below query for you, if it is not fulfill your requirement, then please share sample output.

select A.* from @APPLICATION  A
where exists (select sku from @Interchange where A.SKU = SKU)
AND EXISTS (SELECT baseLine from  @Driver where BaseLine = A.line)