数据库表a:电表记录,NUM为表号
NUM D_DATE D_VALUE ADDR
10001 2021-05-01 00:04:26.000 1459.3 302室
10001 2021-05-01 00:05:32.000 1459.3 302室
10001 2021-05-01 00:30:00.000 1459.3 302室
10002 2021-05-01 00:04:26.000 237.44 301室
10002 2021-05-01 00:05:32.000 237.45 301室
10003 2021-05-01 00:04:32.000 1525.25 303室
10003 2021-05-01 00:44:39.000 1525.35 303室
......
现在想要将每个电表在0时,8时,11时,13时,19时,21时,22时,23时的第一条记录存放在表b。
这样写就好呀:select num,d_date,d_value,addr into b from a where d_date in
(select d_date from (select number() over(group by year(d_date),month(d_date,day(d_date),hour(d_date) order by d_date) as myseqnum,* from a) as tmptable1 where myseqnum=1) and hour(d_date) in (0,8,11,13,19,21,22,23)
刚才写错了,现在重写如下:
select * from
(select row_number() over(partition by year(d_date),month(d_date),day(d_date),datepart(hour,d_date) order by d_date) as myseqnum, * from a)
as tmptable1
where myseqnum=1 and datepart(hour,d_date) in (0,8,11,13,19,21,22,23)