当前位置:网站首页>SQL query: subtract the previous row from the next row and make corresponding calculations

SQL query: subtract the previous row from the next row and make corresponding calculations

2022-07-07 05:39:00 MonkeyKing_ sunyuhua

In the project , There is a task of getting a data in an hour , After obtaining the specific value , It needs to be stored in the database . But after long-term accumulation , It is necessary to check whether it is recorded once an hour .

Data like this :
 Insert picture description here
We need statistics , Is there any missing legacy , Is there something missing for an hour ,

select julianday(b.UsageStartDate)-julianday(a.UsageStartDate),a.UsageStartDate,a.UsageEndDate from (

select * from (select Row_Number() over ( order by UsageStartDate ) as RN , * from  "north1-nx-billing" where   LinkedAccountId = '621933488636' and ResourceId = 'i-0f2bf6bb9eab43dd5' and UsageType like '%HeavyUsage:m5.large' ORDER BY UsageStartDate

) where rn%2=0 ) a

inner join

(

select * from (select Row_Number() over ( order by UsageStartDate ) as RN , * from  "north1-nx-billing" where   LinkedAccountId = '621933488636' and ResourceId = 'i-0f2bf6bb9eab43dd5' and UsageType like '%HeavyUsage:m5.large'  ORDER BY UsageStartDate

) where rn%2<>0 ) b on a.rn=b.rn-1 



Query results :

 Insert picture description here

This makes it easy to find , Those places are not once an hour .

Knowledge point : Use sqlite The window function of

select  Fields to query  from (

select * from (select Row_Number() over ( order by UsageStartDate ) as RN , +  My own query sql

) where rn%2=0 ) a

inner join

(

select * from (select Row_Number() over ( order by UsageStartDate ) as RN , +  The same query as above SQL


) where rn%2<>0 ) b on a.rn=b.rn-1 
原网站

版权声明
本文为[MonkeyKing_ sunyuhua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062342339278.html