查找错误记录(SQL)
谢谢了,下面是postgresql 的SQL写法。
-- p1 昨天的日期-- p2 当月统计数据范围内,上一条月累加值-- p3 当年统计数据范围内,上一条年累加值-- p4 当月统计数据范围内,第一条月累加值-- p5 当月统计数据范围内,第一条日值-- p6 当年统计数据范围内,第一条年累加值-- p7 当年统计数据范围内,第一条日值
select * from (
select v_d, data, month, year,
lag(v_d, 1) over (order by v_d) p1,
lag(month, 1) over (partition by extract(month from v_d) order by v_d) p2,
lag(year, 1) over (partition by extract(year from v_d) order by v_d) p3,
first_value(data) over(partition by extract(month from v_d) order by v_d) p4,
first_value(month) over(partition by extract(month from v_d) order by v_d) p5,
first_value(data) over(partition by extract(year from v_d) order by v_d) p6,
first_value(year) over(partition by extract(year from v_d) order by v_d) p7
from tbl)
twhere v_d - p1 <>1orp4 <> p5orp6 <> p7orp2+data <> monthorp3+data <> year;
不知道这种写法对不对。在这个简单例子里可以得到期望结果。
sum(data) over (partition by extract(month from v_d) order by v_d) as 计算月累,如果记录里年不都是一年,结果就不对。希望大拿帮助一下,解释partition by extract(month from v_d),谢谢了。select * from(
select v_d, data,
month,
sum(data) over (partition by extract(month from v_d) order by v_d) as 计算月累,
year,
sum(data) over (partition by extract(year from v_d) order by v_d) as 计算年累,
lag(v_d, 1) over (order by v_d) p1
from tbl
) twhere v_d - p1 <>1ormonth <> 计算月累oryear <> 计算年累
赞0
踩0