我想返回一个唯一ID的计数以及自创建记录以来的分钟数。
我有这个查询:
SELECT DISTINCT
batchid AS 'ID'
, COUNT(batchid) AS 'Count'
, DATEDIFF (MINUTE,CONVERT(char(10),MIN([createdOn]), 108),CONVERT(char(10),GETDATE(), 108))
FROM [UpdateRecords]
WHERE [status] = 'Pending'
GROUP BY batchId,createdOn
ORDER BY batchId
它返回ID,计数和分钟数。
ID Count Minutes 171836 1 28 171836 4 28 171836 10 28 171836 21 28
出现相同的ID会出现多次的问题。 当加在一起时,该ID的计数是准确的。
我想通过ID和分钟数来进行计数
ID Count Minutes 171836 36 28
如何用几分钟来做到这一点?可以显示小时和分钟吗?
答案 0 :(得分:1)
您需要按“选择”中的相同项目分组:
SELECT
batchid AS 'ID'
, COUNT(batchid) AS 'Count'
, DATEDIFF (MINUTE,CONVERT(char(10),MIN([createdOn]), 108),CONVERT(char(10),GETDATE(), 108))
FROM [UpdateRecords]
WHERE [status] = 'Pending'
GROUP BY batchId, DATEDIFF (MINUTE,CONVERT(char(10),MIN([createdOn]), 108),CONVERT(char(10),GETDATE(), 108))
ORDER BY batchId
答案 1 :(得分:1)
我认为您想要这样的东西:
SELECT batchid AS ID,
COUNT(*) AS Count,
SUM(DATEDIFF(MINUTE, CONVERT(time, createdOn), CONVERT(time, GETDATE()))
FROM UpdateRecords
WHERE status = 'Pending'
GROUP BY batchId
ORDER BY batchId