SQL Server中当天最后6行的总和

时间:2018-03-06 12:08:22

标签: sql sql-server

我需要计算给定id的同一天过去六行的总和。

查看预期的样本数据和结果。

create table #t (id int, a datetime, dy varchar(10), t int);

insert into #t values 
(1,'2017-01-03','Tuesday',7),
(1,'2017-01-10','Tuesday',5),
(1,'2017-01-17','Tuesday',5),
(1,'2017-01-24','Tuesday',2),
(1,'2017-01-31','Tuesday',6),
(1,'2017-02-07','Tuesday',4),
(1,'2017-02-14','Tuesday',5),
(1,'2017-02-21','Monday',2),
(1,'2017-02-28','Monday',4),
(1,'2017-03-07','Monday',4),
(1,'2017-03-17','Monday',4),
(1,'2017-03-21','Monday',4);
(1,'2017-03-2','Monday',4);

create table #t_result (id int, a datetime, dy varchar(10),t int);

insert into #t_result values 
(1,'2017-01-03','Tuesday',29),
(1,'2017-02-14','Monday',22)


select * from #t
select * from #t_result

提前致谢

3 个答案:

答案 0 :(得分:0)

    --Try Using While Loop 


BEGIN TRAN

CREATE TABLE #t (id INT, a DATETIME ,dy VARCHAR(10), t INT);
CREATE TABLE #t_result (id INT, a DATETIME,dy VARCHAR(10),t INT);
CREATE TABLE #Temp (id INT, a DATETIME, t INT, dy VARCHAR(10),temp_count INT);

INSERT INTO #T VALUES 
(1,'2017-01-03','Thursday',7),
(1,'2017-01-10','Thursday',5),
(1,'2017-01-17','Thursday',5),
(1,'2017-01-24','Thursday',2),
(1,'2017-01-31','Thursday',6),
(1,'2017-02-07','Thursday',4),
(1,'2017-02-14','Thursday',5),
(1,'2017-02-21','Monday',2),
(1,'2017-02-28','Monday',4),
(1,'2017-03-07','Monday',4),
(1,'2017-03-17','Monday',4),
(1,'2017-03-21','Monday',4),
(1,'2017-03-2' ,'Monday',4)


DECLARE @Strt INT,@End INT
SELECT *, ROW_NUMBER()OVER(ORDER BY ID)rownum INTO #Temp_data FROM #t

SET @Strt=1
SELECT @End= MAX(rownum) FROM #Temp_data

WHILE @Strt<= @End BEGIN

   DECLARE  @Id INT , @T INT , @Date DATETIME, @total INT, @count INT, @Temp_count INT, @D DATETIME, @Dy VARCHAR(10)

   SELECT @Id= Id, @T=t, @Dy=dy, @Date= a FROM  #Temp_data WHERE rownum= @Strt

   INSERT INTO #Temp
   SELECT @Id, @Date, @T, @Dy,1

   SELECT @Temp_count= COUNT(*) FROM #Temp

       IF @Temp_count=6 BEGIN

                SELECT *,ROW_NUMBER()OVER(Order by ID)rownum INTO #tt FROM #Temp 
                SELECT @D=a FROM #tt WHERE rownum=1 

                INSERT INTO #t_result
                SELECT ID, @D,dy,SUM(t) 
                FROM #Temp 
                WHERE DATEPART(yyyy, a) = YEAR(DATEADD(year,-1,GETDATE()))AND dy = DATENAME(DW,GETDATE())                       
                GROUP BY id,dy
                HAVING COUNT(*)=6

                DELETE FROM #Temp
                DROP TABLE #tt
       END

  SET @Strt= @Strt +1

END
SELECT * FROM #t_result
ROLLBACK TRAN

答案 1 :(得分:0)

你可以做一些窗口函数操作并输出每第6行(ROW_NUMBER上的模运算符)。

SELECT
    id, a, [sum]
FROM
    (
        SELECT
            id
        ,   FIRST_VALUE( a ) OVER (ORDER BY a ROWS 5 PRECEDING) AS a
        ,   SUM( t ) OVER (ORDER BY a ROWS 5 PRECEDING) AS [sum]
        ,   ROW_NUMBER() OVER (ORDER BY a) AS rownum
        FROM
            #t
    ) AS data
WHERE
    rownum % 6 = 0

答案 2 :(得分:-1)

试试这个:

SELECT MIN(D.id)Id,MIN(D.a)a,D.dy,SUM(D.t)T
FROM(
    select *,ROW_NUMBER() OVER(PARTITION BY id,dy ORDER BY a)RN 
    from #t
    )D
WHERE D.RN<=6
GROUP BY D.dy
ORDER BY a

<强>输出:

Id  a                       dy      T
1   2017-01-03 00:00:00.000 Tuesday 29
1   2017-02-21 00:00:00.000 Monday  22