我创建了一些代码,允许我在特定的子视觉后显示我的数据中的每条记录
SELECT
ID,
Received,
Closed,
Subsymptom,
DATEDIFF(HOUR, received, COALESCE(closed, GETDATE())) AS 'HoursOpen'
FROM
vw_UserView_OpenAndClosed A
WHERE closed >
(SELECT MAX(closed)
FROM
vw_UserView_OpenAndClosed SUB
WHERE
Subsymptom = 'INSTALL'
AND received >= '2017-06-01'
AND SUB.name = A.name)
数据看起来像这样
ID received closed Subsymptom HoursOpen
1306438 2017-06-20 2017-06-21 ISSUES 19
1305393 2017-06-18 2017-06-19 REQUEST 12
1299203 2017-06-05 2017-06-05 REQUEST 0
1299415 2017-06-05 2017-06-06 SPECIFIC FAILURE 2
我希望能够做的是创建一个新列,告诉我自原始子症状命名为' INSTALL'以来已经过了多少时间。被提出来了。
ID可能有差距,我会有多个名称'
我已经尝试过选择Max(已关闭)加入,但我无法让它工作,我认为我在完全错误的轨道上并且有一些头脑模糊。
示例数据
ID Received Closed Subsymptom Name
1306438 2017-06-20 2017-06-21 ISSUES BROOM
1299203 2017-06-05 2017-06-05 REQUEST BROOM
1305393 2017-06-18 2017-06-19 REQUEST CHIVE
1299415 2017-06-06 2017-06-06 SPECIFIC FAILURE BROOM
1299222 2017-06-05 2017-06-05 ISSUES BROOM
1299112 2017-06-05 2017-06-05 CONTACTLESS BROOM
1279168 2017-06-01 2017-06-03 INSTALL BROOM
1287965 2017-06-10 2017-06-10 INSTALL CHIVE
所需结果
ID Received Closed Subsymptom Name HoursOpen Days Received since Install Closed
1306438 2017-06-20 2017-06-21 ISSUES BROOM 19 17
1299203 2017-06-05 2017-06-05 REQUEST BROOM 0 2
1305393 2017-06-18 2017-06-19 REQUEST CHIVE 12 21
1299415 2017-06-06 2017-06-06 SPECIFIC FAILURE BROOM 2 3
1299222 2017-06-05 2017-06-05 ISSUES BROOM 0 2
1299112 2017-06-05 2017-06-05 ISSUES BROOM 0 2
自安装关闭以来收到的天数 - 计算从安装关闭日期到RECEIVED日期的天数。
(稍后将绘制图表,表明在第0天,第1天,第2天等收到INSTALL新记录之后)
答案 0 :(得分:1)
编辑:感谢您澄清您的意见并添加所请求的样本数据。一旦我拥有它,这是轻而易举的事。因此,将来发帖时请务必添加,以确保最快的响应!
以下是代码:
DECLARE @Table TABLE (ID bigint, Received DATETIME, Closed DATETIME, Subsymptom varchar(50), [Name] varchar(50))
DECLARE @Now DateTime = GETDATE()
INSERT INTO @Table
VALUES
(1306438 , '2017-06-20', '2017-06-21', 'ISSUES', 'BROOM'),
(1299203 , '2017-06-05', '2017-06-05', 'REQUEST', 'BROOM'),
(1305393 , '2017-06-18', '2017-06-19', 'REQUEST', 'CHIVE'),
(1299415 , '2017-06-06', '2017-06-06', 'SPECIFIC FAILURE', 'BROOM'),
(1299222 , '2017-06-05', '2017-06-05', 'ISSUES', 'BROOM'),
(1299112, '2017-06-05', '2017-06-05', 'CONTACTLESS', 'BROOM'),
(1279168 , '2017-06-01', '2017-06-03', 'INSTALL', 'BROOM'),
(1287965 , '2017-06-10', '2017-06-10', 'INSTALL', 'CHIVE')
SELECT T.ID,
T.Received,
T.Closed,
T.Subsymptom,
T.[Name],
DATEDIFF(HH, T.Received, COALESCE(T.Closed, @Now)) AS 'HoursOpen',
DATEDIFF(DD, T2.Closed, T.Received) AS [Days Received since Install Closed]
FROM @Table T
JOIN @Table T2
ON T.[Name] = T2.[Name]
AND T2.Subsymptom = 'INSTALL'
WHERE T.Closed > T2.Closed
结果如下:
(6 row(s) affected)
ID Received Closed Subsymptom Name HoursOpen Days Received since Install Closed
-------------------- ----------------------- ----------------------- -------------------------------------------------- -------------------------------------------------- ----------- ----------------------------------
1306438 2017-06-20 00:00:00.000 2017-06-21 00:00:00.000 ISSUES BROOM 24 17
1299203 2017-06-05 00:00:00.000 2017-06-05 00:00:00.000 REQUEST BROOM 0 2
1305393 2017-06-18 00:00:00.000 2017-06-19 00:00:00.000 REQUEST CHIVE 24 8
1299415 2017-06-06 00:00:00.000 2017-06-06 00:00:00.000 SPECIFIC FAILURE BROOM 0 3
1299222 2017-06-05 00:00:00.000 2017-06-05 00:00:00.000 ISSUES BROOM 0 2
1299112 2017-06-05 00:00:00.000 2017-06-05 00:00:00.000 CONTACTLESS BROOM 0 2
(6 row(s) affected)
您要保证的是每个名字只有一个“安装”子症状的实例。如果是这种情况,此查询将始终有效。我的结果与您的结果不同的原因是因为您只提供了日期(而不是DateTimes),因此计算结果略有偏差。但是,基本上,你只是将每个记录加入其原始安装 - 抓住原始记录的关闭时间 - 并将其与当前记录的接收时间进行比较。非常简单 - 而且直截了当。
如果这不是您想要的,请告诉我。