这适用于MSQL2012实例
我目前在将客户端数据表加入层次结构时遇到问题,我的目标是生成一个输出表,显示它们在第一级不拥有的内容。
我遇到了客户端ID问题,因为比较表时输出重叠并为所有客户端ID提供例外,而不是每个客户端ID
有没有人可以帮助我为这类问题生成语法?
product_data
Level 1, Level 1 Name, Level 2, Level 2 Name
1 Fruit 2 Apple
1 Fruit 3 Banana
1 Fruit 4 Orange
5 Vegetable 6 Potato
5 Vegetable 7 Celery
client data
Client ID, Level 1, Level 1 Name, Level 2, Level 2 Name
1 1 Fruit 2 Apple
1 1 Fruit 3 Banana
2 5 Vegetable 5 Potato
2 1 Fruit 4 Orange
Desired Output
Client ID, Level 1, Level 1 Name, Level 2, Level 2 Name
1 1 Fruit 4 Orange
2 1 Fruit 2 Apple
2 1 Fruit 3 Banana
2 5 Vegetable 7 Celery
谢谢!
答案 0 :(得分:0)
试试这个
SELECT cln1.ClientID, cln1.Level1, cln1.Level1Name, prd.Level2, prd.Level2Name
FROM product_data prd
INNER JOIN client_data cln1
ON prd.Level1 = cln1.Level1
LEFT JOIN client_data cln2
ON cln1.ClientId = cln2.ClientId
AND prd.Level1 = cln2.Level1
AND prd.Level2 = cln2.Level2
WHERE cln2.Level2 IS NULL