Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.sqlserver.programming > #31332

Re: How to add a datetime column for a 24 hour period

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups microsoft.public.sqlserver.programming
Subject Re: How to add a datetime column for a 24 hour period
Date Tue, 24 Oct 2017 20:49:55 +0200
Organization Erland Sommarskog
Lines 38
Message-ID <XnsA818D3EA567D3Yazorman@127.0.0.1> (permalink)
References <d0c629ca-5826-4cd4-baba-9848a915df19@googlegroups.com> <XnsA817F2C2824FCYazorman@127.0.0.1> <9ebb76da-7445-4b19-a786-49e159c79594@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 8bit
Injection-Info reader02.eternal-september.org; posting-host="e5809fca7dae36379545db2cbc13f242"; logging-data="10030"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6tD+1EUUv6gOPzLr2I4qG"
User-Agent Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32)
Cancel-Lock sha1:05ewUCtN9DD/XJTX7q7AW+R8Yyc=
Xref csiph.com microsoft.public.sqlserver.programming:31332

Show key headers only | View raw


Mike Arney (mikespcnet.net@gmail.com) writes:
> I need to SUM all Timestamp (in seconds) with a Topic of
> 'STG/A/CycleStart', a Payload of 1. 
> 
> Payload of 1 = Machine On 
> Payload of 0 = Machine Off 
> 
> I am trying to get amount of time that a Payload is = 1 over the last 24
> hours. 
> 

Here is a query. However, it is clear to how you want to handle the case
that the last row has Payload = 1. Should we count from that time until
"now", or is there something else? In the example I am assuming "now",
but you can change that as you see fit.

LEAD returns the next row in the result set as defined by the OVER clause.

; WITH ontimes AS (
    SELECT Topic, 
           CASE WHEN Payload = 1
                THEN datediff(ss, Timestamp,
                                  LEAD(Timestamp, 1, sysutcdatetime()) 
                                     OVER (PARTITION BY Topic ORDER BY Timestamp))
                ELSE 0
           END AS ontime
    FROM  MQTTData
)
SELECT Topic, SUM(ontime) 
FROM   ontimes
GROUP  BY Topic





-- 
Erland Sommarskog, Stockholm, esquel@sommarskog.se

Back to microsoft.public.sqlserver.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-10-23 11:42 -0700
  Re: How to add a datetime column for a 24 hour period Erland Sommarskog <esquel@sommarskog.se> - 2017-10-23 23:51 +0200
    Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-10-24 05:10 -0700
      Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-10-24 05:12 -0700
    Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-10-24 05:38 -0700
      Re: How to add a datetime column for a 24 hour period Erland Sommarskog <esquel@sommarskog.se> - 2017-10-24 20:49 +0200
        Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-10-25 04:57 -0700
          Re: How to add a datetime column for a 24 hour period Erland Sommarskog <esquel@sommarskog.se> - 2017-10-26 20:58 +0200
            Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2017-11-02 09:54 -0700
              Re: How to add a datetime column for a 24 hour period mikespcnet.net@gmail.com - 2018-05-08 08:21 -0700
                Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2018-05-08 08:35 -0700
                Re: How to add a datetime column for a 24 hour period Erland Sommarskog <esquel@sommarskog.se> - 2018-05-08 19:48 +0200
                Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2018-05-09 06:31 -0700
                Re: How to add a datetime column for a 24 hour period Mike Arney <mikespcnet.net@gmail.com> - 2018-05-09 06:34 -0700
                Re: How to add a datetime column for a 24 hour period Erland Sommarskog <esquel@sommarskog.se> - 2018-05-09 23:08 +0200

csiph-web