gpt4 book ai didi

sql-server - TSQL:如何确保一个ID有多行?

转载 作者:行者123 更新时间:2023-12-03 02:28:05 25 4
gpt4 key购买 nike

我需要确保每个 IncidentID 都有 2 个特定的 ResponseCodes(201 和 204)。如何识别不同时具有 201 和 204 的 IncidentID(如下图所示)?

数据集

enter image description here

我试图在数据集中识别什么

enter image description here

期望的结果集:

enter image description here

SQL Fiddle 对我不起作用,但测试代码如下:

CREATE TABLE DIS (
IncidentID NUMERIC,
ResponseCode NUMERIC,
ResponseCodeDescription varchar(30)
)

INSERT INTO DIS (IncidentID, ResponseCode, ResponseCodeDescription)
VALUES
( '999888', '201', 'OSS'),
( '999888', '204', 'Long Term OSS'),
( '888777', '201', 'OSS'),
( '888777', '204', 'Long Term OSS'),
( '777666', '204', 'Long Term OSS'),
( '555444', '201', 'OSS'),
( '555444', '204', 'Long Term OSS')

SELECT
IncidentID
, ResponseCode
, ResponseCodeDescription
FROM DIS

谢谢!

最佳答案

试试这个:

SELECT IncidentID
FROM DIS
WHERE ResponseCode IN (201,204)
GROUP BY IncidentID
HAVING COUNT(DISTINCT ResponseCode) < 2

此查询将选择 ResponseCode 值等于 201204 但不能同时等于两者的 IncidentID 值.

关于sql-server - TSQL:如何确保一个ID有多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36285391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com