在按另一列分组的列中查找不同的值

  • 本文关键字:查找 一列 sql-server tsql
  • 更新时间 :
  • 英文 :


假设一个表如下摘录所示:

SIZEPOS
ITEID 颜色代码 ISEAN13
4454 7 0
4454 0
4456 0
4456 0
4456 0
4456 0
4462 0
4462 0
4462 0
4462 0
4462 0
4462 0
4462 0
4464 0
4464 0
4464 0
4469 0
4469 0
4469 0
4469 0
4511 0
4511 0
4511 0
4511 0
4511 0

可能吗?(请下次自己做设置工作)

DROP TABLE IF EXISTS YT
CREATE TABLE YT(
ITEID           INT  NOT NULL
,COLORCODE       NVARCHAR(7)

);
INSERT INTO YT(ITEID,COLORCODE) VALUES
(4454,N'ΜΑΥ'),(4454,N'ΜΑΥ'),(4456,N'ΜΑΥ Λ'),(4456,N'ΜΑΥ Λ'),(4456,N'ΜΑΥ Λ')
,(4456,N'ΜΑΥ Λ'),(4462,N'ΜΑΥ'),(4462,N'ΜΑΥ'),(4462,N'ΜΑΥ'),(4462,N'ΜΑΥ')
,(4462,N'ΜΑΥ'),(4462,N'ΜΑΥ'),(4462,N'ΜΑΥ'),(4464,N'ΜΑΥ ΠΡΑ'),(4464,N'ΜΑΥ ΠΡΑ')
,(4464,N'ΜΑΥ ΠΡΑ'),(4469,N'ΜΠΛΕ'),(4469,N'ΜΠΛΕ'),(4469,N'ΜΠΛΕ'),(4469,N'ΜΠΛΕ')
,(4511,N'ΚΟΚ'),(4511,N'ΚΟΚ'),(4511,N'ΚΟΚ'),(4511,N'ΚΟΚ'),(4511,N'ΚΟΚ')
-- ADDED DATA TO DEMONSTRATE YOUR QUESTION
,(4511,N'ΚΟΚ'),(4511,N'BAL'),(4511,N'NIK'),(4511,N'DIK'),(4511,N'ΚIΚ')
;
SELECT   ITEID, COUNT(DISTINCT COLORCODE) AS cnt
FROM      YT
GROUP BY ITEID
HAVING COUNT(DISTINCT COLORCODE) > 1

相关内容

  • 没有找到相关文章

最新更新