简体   繁体   中英

How to substract exact timestamp minutes 00:15 in SQL for every day in MS SQL server?

I am substracting daily values. For example, I got:

       timestamp           value
2020-03-23 00:00:00.000    3000
2020-03-23 00:15:00.000    3100
2020-03-24 00:00:00.000    3500
2020-03-24 00:15:00.000    4000

and with:

SELECT tagtimeutc,description,tagname,unit,
    (   (LEAD (TagValue) OVER (order by tag_id,TagTimeUTC) -
            TagValue)),prik_sifr,tag_id
    FROM vwDMData
    where TagTimeUTC=DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) and unit='m3'
       or TagTimeUTC=DATEADD(DAY, DATEDIFF(DAY, 1, GETDATE()), 0) and unit='m3'

I get:

    date      value
2020-03-23    500

but I want

    date      value
2020-03-23    900

(So that substraction is between 1d 00:15 values) like:

value(2020-03-24 00:15:00.000) - value(2020-03-23 00:15:00.000)

Can you help me?

Just fix the where :

SELECT tagtimeutc, description, tagname, unit,
       (LEAD(TagValue) OVER (order by tag_id,TagTimeUTC) -
        TagValue)
       ),
       prik_sifr, tag_id
FROM vwDMData
WHERE unit = 'm3' AND
      TagTimeUTC >= DATEADD(minute, -15, CAST(CAST(GETDATE() AS DATE) as DATETIME)) AND
      TagTimeUTC < DATEADD(minute, 24*60-15, CAST(CAST(GETDATE() AS DATE) as DATETIME)) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM