0

好的,问题来了:我有一张带有日期标记的交易表。假设日期已经从日期戳中删除,并且表格看起来像这样:

;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        

任何帮助将不胜感激!

4

1 回答 1

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

于 2013-02-12T08:36:02.350 回答