Calculating Total Time in Seconds

If I group this query by

format(dateadd(hh,-8, t_stamp), 'yyyy-MMM-dd') 

how do I join it to to another query that will be grouped by

format(dateadd(hh,-8, t_stamp), 'yyyy-MMM-dd') 

both queries have subqueries

still join on mytable from that inner most subquery?
and if I join, I think my t_stamps would be messed up?


it turned out that I could join a subquery on a subquery

left join ( the entire subquery) sub1
on on format(dateadd(hh,-8, t.t_stamp), 'yyyy-MMM-dd') = sub1.day

I apparently need someone to post or I can’t post on this thread because 3 self replies is the limit.
I think I will have more things to do with this duration query though.


sept 2022

I am using a union per machine to get more than one machine in the query because my table has all machines in one table.
I haven’t figured out a way to get the t_stamps of one machine, then the next, and then the next.
I think it must be some kind of partition by over combination.


I think thought these were the same, with isnull being faster.
Seems to be throwing index errors.

,isnull(lead(t_stamp) OVER (ORDER BY t_stamp),current_timestamp) as test2
,coalesce(lead(t_stamp) OVER (ORDER BY t_stamp),	CASE WHEN current_timestamp < :endLead THEN current_timestamp ELSE :endLead END) AS lead,t_stamp