两个日期之间的SQL差异

时间:2017-10-26 13:15:58

标签: sql sql-server

我有一张如下表:

ID,  MachineID     Customer   TimeStamp                     Counter   type
1    A                ABC       2017-10-25 3:08PM              1952      1
2    A                ABC       2017-10-25 3:00PM              1940      1
3    A                ABC      2017-10-25 12:05PM              1920      1
4    A                ABC       2017-10-25 9:00AM              1900      1
5    B                BCD       2017-10-25 3:11PM              1452      1
6    B                BCD       2017-10-25 3:10PM              1440      1
7    B                BCD      2017-10-25 12:15PM              1420      1
8    B                BCD       2017-10-25 9:30AM              1400      1
9    A                ABC       2017-10-23 3:08PM              1900      1
10   A                ABC       2017-10-23 3:00PM              1840      1
11   A                ABC      2017-10-23 12:05PM              1820      1
12   A                ABC       2017-10-23 9:00AM              1800      1
13   B                BCD       2017-10-23 3:11PM              1399      1
14   B                BCD       2017-10-23 3:10PM              1340      1
15   B                BCD      2017-10-23 12:15PM              1320      1
16   B                BCD       2017-10-23 9:30AM              1300      1

只要有点击,计数器值就会增加。我试图通过在一天结束时获取最大计数器值并减去前一天的最大计数器值来计算每天的点击次数,依此类推。

如何在SQL Server中执行此操作。必须为每个客户和机器重复此操作

2 个答案:

答案 0 :(得分:0)

试试这个。我正在使用LAG函数来实现这一目标。您可以使用where子句过滤掉您想要的特定日期:

Create  table #counter(ID int, timeStamp datetime, Counter int,  type int)
insert into #counter values
(1,   '20171024 3:08PM' ,1952, 1),
(1,   '20171025 3:00PM' ,1964, 1)

Select iq.*, (iq."counter" - iq.yesterday_counter) as today_count
from
(select id, 
        cast("timestamp" as date) as today_date, 
        "counter", 
        LAG("counter") over (order by cast("timestamp" as date)) yesterday_counter 
from #counter
) iq

输出:

id          today_date counter     yesterday_counter today_count
----------- ---------- ----------- ----------------- -----------
1           2017-10-24 1952        NULL              NULL
1           2017-10-25 1964        1952              12

答案 1 :(得分:0)

获取每天最大计数器的SQL查询是:

SELECT CAST(timeStamp as date) AS [dateval]
      ,MAX(Counter) AS [maxCounter]
 FROM YOURDATASET
GROUP BY CAST(timeStamp as date)

这是将日期时间转换为日期 - 缩短时间,然后取最大值(计数器)。

获得差异的一种方法是将结果保存在临时数据结构中,然后查询它以获得差异。

问题在于您的上一个日期是否恰好是前一天,或者您是否在计算之间跳过了几天,或者周末休假等等。在这种情况下,您必须选择最近的日期到被检查的日期

离。

DECLARE @temp TABLE (dateval date, maxCounter int)

INSERT INTO @temp(dateval, maxCounter)
SELECT CAST(timeStamp as date) AS [dateval]
      ,MAX(Counter)
  FROM YOURDATASET
GROUP BY CAST(timeStamp as date)

SELECT T.dateval
      ,T.dateval
       -
       (SELECT maxCounter
          FROM @temp T2
         WHERE T2.dateVal = (SELECT MAX(dateVal)
                               FROM @temp T3
                              WHERE T3.dateVal < T1.dateVal
                            )
       ) AS [Difference]
  FROM @temp T
ORDER BY T.dateval