SQL Server总查询每06:30 AM

时间:2018-09-13 01:31:39

标签: sql-server

真的需要一些帮助:我有一个像这样的SQL Server表:

enter image description here

我想从今天的06:30到明天的06:29,每天进行一次价值计数查询。

问题是,该值不是每隔06:30从0开始。

我需要这样的查询:首先选择每个代码的最大值,例如:

(code : value)
M12 : 108, 
M77 : 26

那么M77值不能从06:30开始。

我需要做:26-12(06:30:00之前的值),所以M77现在是14。

最后将M77和M12求和= 14 + 108 = 122。

我的预期输出仅是每个日期的总价值。

根据我的数据库(图片),总值为26-12 = 14。

  • 26是最新值。
  • 12是06:30之前的值

如何在SQL Server中做到这一点?请帮助我

2 个答案:

答案 0 :(得分:0)

如果我确实正确理解您的要求。

查询说明在代码注释中

; with 
-- for getting the max value by code, rn = 1 is the max value
cte1 as
(
    select  *, rn = row_number() over (partition by code order by value desc)
    from    sample_table
),
-- for getting rows before 6:30, rn = 1 is the last record before 6:30
cte2 as
(
    select  *, rn = row_number() over (partition by code order by [date] desc)
    from    sample_table
    where   convert(time, [date])   < '06:30'
)
select  total = sum(case when convert(time, c1.[date]) < '06:30' 
                         then c1.value 
                         else c1.value - c2.value 
                         end)
from    cte1 c1
        left join cte2 c2   on  c1.code     = c2.code
                           and  c1.rn       = c2.rn
where   c1.rn   = 1

答案 1 :(得分:0)

尝试这个难题:

CREATE TABLE TestTable
(
[date]  datetime,
value   int,
code    varchar(10)
)
GO
INSERT INTO [dbo].[TestTable]
       ([date]
       ,[value]
       ,[code])
 VALUES
    ('2018-09-13 06:20:52.803'   
               ,100
               ,'M12'),
    ('2018-09-13 06:21:52.803'   
       ,102
       ,'M12')
    ,  ('2018-09-13 06:22:52.803'    
               ,104
               ,'M12')
    ,  ('2018-09-13 06:23:52.803'    
               ,106
               ,'M12')
    ,  ('2018-09-13 06:24:52.803'    
               ,108
               ,'M12')
    ,  ('2018-09-13 06:25:52.803'    
               ,2
               ,'M77')
    ,  ('2018-09-13 06:29:14.803'    
               ,4
               ,'M77')
    ,  ('2018-09-13 06:29:16.803'    
               ,6
               ,'M77')
    ,  ('2018-09-13 06:29:18.803'    
               ,8
               ,'M77')
    ,  ('2018-09-13 06:29:45.803'    
               ,10
               ,'M77')
    ,  ('2018-09-13 06:29:55.803'    
               ,12
               ,'M77')
    ,  ('2018-09-13 06:30:18.803'    
               ,14
               ,'M77')
    ,  ('2018-09-13 06:31:18.803'    
               ,26
               ,'M77')

;WITH RESULT AS (
SELECT 
  TT.code   
, MaxValueThatday          = max(maxval.MAXVALUE )              
, MaxValueBefore630NextDay = max(MAXValBefore630.MAXVALUE)
, ResultSubstraction = 
 CASE WHEN max(maxval.MAXVALUE ) <>  max(MAXValBefore630.MAXVALUE)
        THEN max(maxval.MAXVALUE )  - max(MAXValBefore630.MAXVALUE)
    ELSE max(maxval.MAXVALUE ) 
END
FROM  [dbo].[TestTable] TT
 OUTER APPLY(   
            SELECT max(VALUE) MAXVALUE
                , code 
             FROM [dbo].[TestTable] aa
                WHERE Aa.code = tt.code
            group by code
        )maxval
 OUTER APPLY(   
            SELECT max(A.VALUE) MAXVALUE
                , code
             FROM [dbo].[TestTable] A
                 WHERE DATEPART(HOUR,[DATE]) <= 6 AND DATEPART(MINUTE,[DATE]) < 30 
                 and A.code = tt.code
            group by code
        )MAXValBefore630
where    ( [DATE] > DATEADD(MINUTE,390,CAST({ fn CURDATE()} AS DATETIME) ) ) --6:30 today
group by tt.code

)
SELECT SUM(ResultSubstraction)
 FROM RESULT