2

目标是排除或以其他方式标记只有 ItemNumber 记录的客户,例如“9%”。

    CustomerID  ItemNumber
    85610           99
    85611           11
    85611           99
    85612           13
    85612           11
    85612           98
    85613           98
    85613           99

在此示例中,应排除或标记 CustomerID 86510 和 86513,因为与其记录关联的唯一 ItemNumber 类似于“9%”。

我将不胜感激任何帮助。

4

5 回答 5

6
SELECT CustomerID, ItemNumber
FROM dbo.table AS t
WHERE NOT EXISTS
(
    SELECT 1
    FROM dbo.table 
    WHERE CustomerID = t.CustomerID 
    AND ItemNumber NOT LIKE '9%'
);
于 2012-07-25T20:34:10.747 回答
2
SELECT 
    CustomerID
FROM Table T
WHERE NOT EXISTS (SELECT CustomerID FROM Table WHERE CustomerId = T.CustomerId AND ItemNumber NOT LIKE '9%')
于 2012-07-25T20:35:07.203 回答
2

另外一个选项

SELECT CustomerID
FROM YourTable
GROUP BY CustomerID
HAVING MAX(CASE WHEN ItemNumber LIKE '9%' THEN 1 END) = 1
    AND MAX(CASE WHEN ItemNumber NOT LIKE '9%' THEN 1 END) IS NULL
于 2012-07-25T20:40:51.567 回答
0
create table tc(CustomerID int,ItemNumber varchar(10))

INSERT INTO tc
VALUES(85610,99),
    (85611,11),
    (85611,99),
    (85612,13),
    (85612,11),
    (85612,98),
    (85613,98),
    (85613,99)

select CustomerID from tc 
where ItemNumber like '9%'
group by CustomerID 

except

select CustomerID from tc 
where ItemNumber not like '9%'
group by CustomerID 
于 2012-07-26T06:41:49.713 回答
-1

除非我遗漏了什么,否则为什么不只包含一个WHERE具有ItemNumber NOT LIKE '9%'

于 2012-07-25T20:35:35.887 回答