好的,问题出在这里:我有一张带有日期标记交易的表格。假设日期已从日期戳中删除,表格如下所示:
;WITH TRX(TRX_Id, TRX_SalesTotal, TRX_TimeStamp) AS
(
SELECT 1, 100, '09:00:00' UNION ALL
SELECT 2, 100, '09:10:00' UNION ALL
SELECT 3, 200, '17:00:00' UNION ALL
SELECT 4, 999, '11:50:00' UNION ALL
SELECT 5, 999, '12:10:00' UNION ALL
SELECT 6, 999, '12:15:00' UNION ALL
SELECT 7, 100, '08:50:00' UNION ALL
SELECT 8, 600, '17:00:00' UNION ALL
SELECT 9, 700, '17:00:00' UNION ALL
SELECT 10, 100, '09:45:00' UNION ALL
SELECT 11, 100, '17:00:00' UNION ALL
SELECT 12, 777, '09:00:00' UNION ALL
SELECT 13, 777, '09:10:00' UNION ALL
SELECT 14, 777, '10:10:00'
)
我必须报告一小时内销售价值超过2次的所有交易。在任何给定的时间。 (因此根据数据,一个销售总额可以是12:30 - 13:30,另一个销售总额可以是12:05 - 13:05。)
上述数据的结果应为:
TRX_ID TRX_SalesTotal TRX_TimeStamp
1 100 09:00:00
2 100 09:10:00
7 100 08:50:00
10 100 09:45:00
4 999 11:50:00
5 999 12:10:00
6 999 12:15:00
非常感谢任何帮助!
答案 0 :(得分:0)
SELECT TRX_Id, TRX_SalesTotal, TRX_TimeStamp
FROM TRX TRX1
WHERE (SELECT COUNT(*)
FROM TRX TRX2
WHERE TRX1.TRX_SalesTotal=TRX2.TRX_SalesTotal
AND
TRX1.TRX_TimeStamp BETWEEN DATEADD(hour, -1, TRX2.TRX_TimeStamp)
AND
DATEADD(hour, 1, TRX2.TRX_TimeStamp))
>1
请参阅此SQLFiddle