这是我的 SQL 语句:
SELECT repnr, DATEDIFF(day, MIN(start), MAX(start) +1) AS DaysTotal
FROM TBLstatus
WHERE status IN (1, 2, 3, 4, 8, 11, 20, 7, 23)
GROUP BY repnr HAVING COUNT(*) = 2 AND MAX(Start) > GETDATE()-30
Select repnr as repnr, CONVERT(float, SUM(DATEDIFF(day, start, slut))) as extern
From TBLstatus
where status IN (5, 15, 17)
group by repnr
第一个将返回此数据:
repnr daystotal
1000 100
1001 90
1003 91
第二个声明:
repnr extern
900 100
901 102
1000 21
1003 23
我想要的是 repnr (这是一个 ID)匹配的地方,我想减去 to 值,所以我看起来像这样:
repnr result
1000 79
1003 68
我如何为 MSSQL 执行此操作。