需要sql select min(date),max(date)更改每个代码的stat 我尝试这个查询它适用于哪里条件但他给我错误的数据 没有条件
select x.code,x.stat,x.grp, min(date) as min_date, max(date) as max_date
from (
select code,stat,date ,
row_number() over (order by date)- row_number() over (partition by stat order by date) as grp
from c
where code='1') X
group by code,stat,grp
order by min(date)
我的桌子
code stat date
1 s 01/01/2012
1 s 02/01/2012
1 i 03/01/2012
1 s 04/01/2012
2 a 01/01/2012
2 a 02/01/2012
需要此结果
code stat min date max date
1 s 01/01/2012 02/01/2012
1 i 03/01/2012 03/01/2012
1 s 04/01/2012 04/01/2012
2 a 01/01/2012 02/01/2012
答案 0 :(得分:1)
我认为您的代码基本上是正确的,除了缺少的partition by
:
select c.code, c.stat, c.grp,
min(c.date) as min_date, max(c.date) as max_date
from (select c.*
row_number() over (partition by c.code order by c.date) as seqnum,
row_number() over (partition by c.code, c.stat order by date) as seqnum_d
from c
) c
group by c.code, c.stat, c.grp
order by min(c.date);