项目中,有一个小时获取一个数据的任务,获取具体的值后,需要存在数据库中。但是长期累计后,需要检查有没有做到一个小时记录一次。
比如这样的数据:
我们需要统计,有没有遗漏的遗留,是不是有遗漏了一个小时的,
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
select 自己要查询的字段 from (
select * from (select Row_Number() over ( order by UsageStartDate ) as RN , + 自己的查询sql
) where rn%2=0 ) a
inner join
select * from (select Row_Number() over ( order by UsageStartDate ) as RN , + 和上面一样的查询SQL
) where rn%2<>0 ) b on a.rn=b.rn-1
项目中,有一个小时获取一个数据的任务,获取具体的值后,需要存在数据库中。但是长期累计后,需要检查有没有做到一个小时记录一次。比如这样的数据:我们需要统计,有没有遗漏的遗留,是不是有遗漏了一个小时的,查询结果:这样就和容易查询出来,那些地方不是一个小时一次了。知识点: 使用sqlite的窗口函数...
mysql数据库中,查询一个表的下一条数据减上一条数据的值的写法:select
a.nodeId,
a.cpuCharge-b.cpuCharge cpuCharge,
a.chargeTime from
(select @arownum:=@arownum+1 rownum,nodeId,cpuCharge,chargeTime from rn_ext_vir_instance_cha...
-->Date :2009-09-30 08:52:38
set nocount on
if object_id('tb','U')is not null drop table tb
create table tb(ID int, ParentID int)
有时会有这样一种查询场景,需要对查询结果前后行操作,比如后一行减前一行。
比如有一张用户登录表,有登入和登出两行,需要相减简单计算在线时长。或者一张订单表,需要计算后一行的订单id和前一行的订单id差多少(假设订单号是单调递增的)。
针对这类场景,SQL语句改如何写呢?
表结构和数据构造
以统计前后行的订单号是否连续为例,
CREATE TABLE `t_test3` (
`i...
以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的argument1 相加 存放到当前行的 argument2 中,比较简单。
--drop table #temptable
create table #temptable
argument1 int,
argument2 int,
argument3 datetime
declare @rowcount int,@argument1 int,@argument2 nvarchar(50),@argument3 datetime
set @rowcount=1
set @argument1=1
-- 对于CET通用表达式增加自定义列的问题?-- 附带说明
-- 自定义列必须满足下面二者中的一种,否则报错:[在递归查询 "cet" 的列 "CommType" 中,定位点类型和递归部分的类型不匹配。]
-- a.自定义列CommType显示转换
-- b.或者在使用自定义列时,前面列的长度必须比后面列的长度大
admit_dt,
discharge_dt,
LAG(discharge_dt) OVER (PARTITION BY id ORDER BY admit_dt) AS prev_discharge_dt
FROM admissions
SELECT
admit_dt - prev_discharge_dt AS time_between_stays
FROM base_data
WHERE prev_discharge_dt IS NOT NULL;
在这段代码中,我们首先创建了一个名为 `base_data` 的临时表,它选取了 admissions 表中的 ID、入院日期和出院日期。接下来,我们使用 LAG 函数获取每一行的前一行的出院日期,并将它们存储在名为 `prev_discharge_dt` 的列中。最后,我们选取 `base_data` 表中的 ID 和入院日期减去前一行的出院日期,并将其存储在名为 `time_between_stays` 的列中。
java.time.format.DateTimeParseException: Text ‘2022-03-18 14:13:38‘ could not be parsed at index 10
19260