我试图根据一个值是否存在来排除特定值。所以我的数据类似于下面的数据。
Client StoreType
------- ---------
1 1
1 2
1 2
2 2
3 1
3 2
4 2
4 2
因此,在此示例中,StoreType = 1存在时,我希望在该Client的StoreType = 2时排除。但是,如果该Client不存在StoreType = 1,我希望在StoreType = 2时包含。所以我想得到的结果是:
Client StoreType
------- ---------
1 1
2 2
3 1
4 2
4 2
有谁知道你怎么能这样做?
答案 0 :(得分:1)
SELECT Client, StoreType
FROM YourTable AS t1
WHERE StoreType = 1
OR (NOT EXISTS (SELECT 1 FROM YourTable AS t2
WHERE t1.Client = t2.Client
AND t2.StoreType = 1)
AND StoreType = 2)
答案 1 :(得分:0)
IF EXISTS(SELECT Client, StoreType
FROM tableFoo
WHERE StoreType = 1)
SELECT Client, StoreType
FROM tableFoo
WHERE StoreType = 1
ELSE
IF NOT EXISTS (SELECT Client, StoreType
FROM tableFoo
WHERE StoreType = 1)
SELECT Client, StoreType
FROM tableFoo
WHERE StoreType = 2