# 问个算去年同期值的sql

charlesxu 2013-05-21 11:26:05

2012-1 85
2012-2 98
2012-3 56
2012-4 66
2012-5 34
。。。
。。。
2013-1 82
2013-2 78
2013-3 29
2013-4 77

sql查4个值，日期、得分、去年同期得分、历史最高得分，希望取到的值如下：

2012-1 85
2012-2 98
2012-3 56
2012-4 66
2012-5 34
。。。
。。。
2013-1 82 85 85
2013-2 78 98 98
2013-3 29 56 29
2013-4 66 77 77

...全文
420 点赞 收藏 19

19 条回复

run-and-debug 2013-05-22

``````SELECT t1.dt,t1.score,
(SELECT t2.score FROM VALUE t2 WHERE SUBSTR(t2.dt,6,2)= SUBSTR(t1.dt,6,2)
AND SUBSTR(t2.dt,1,4)= (SUBSTR(t1.dt,1,4)-1)
) AS lastYear,
(SELECT MAX(t3.score) FROM VALUE t3 WHERE SUBSTR(t3.dt,6,2)= SUBSTR(t1.dt,6,2)
AND SUBSTR(t3.dt,1,4) < SUBSTR(t1.dt,1,4)
) AS lastYear1
FROM VALUE t1``````

``````
with t1 as
(
select '2011-01' dt,85 sc from dual union all
select '2011-02' dt,98 sc from dual union all
select '2011-03' dt,56 sc from dual union all
select '2011-04' dt,66 sc from dual union all
select '2012-01' dt,33 sc from dual union all
select '2012-02' dt,34 sc from dual union all
select '2012-03' dt,54 sc from dual union all
select '2012-04' dt,69 sc from dual union all
select '2013-01' dt,97 sc from dual union all
select '2013-02' dt,43 sc from dual union all
select '2013-03' dt,44 sc from dual union all
select '2013-04' dt,34 sc from dual
)

select dt,sc,s_sc,
(select max(sc) from t1 where t1.dt <= t.dt and substr(t1.dt,-2)=substr(t.dt,-2)) m_sc
from
(
select dt,sc,
case row_number() over(partition by substr(dt,-2) order by rownum)
when 1 then null
else lag(sc) over(order by substr(dt,-2),substr(dt,1,4)) end s_sc
from t1
) t
order by dt

dt      sc    s_sc    m_sc
------------------------------------------
1	2011-01	85		85
2	2011-02	98		98
3	2011-03	56		56
4	2011-04	66		66
5	2012-01	33	85	85
6	2012-02	34	98	98
7	2012-03	54	56	56
8	2012-04	69	66	69
9	2013-01	97	33	97
10	2013-02	43	34	98
11	2013-03	44	54	56
12	2013-04	34	69	69

``````

hh7yx 2013-05-21

``````
(select sum(sc)
from t1 t
where t.dt =
``````
where t.dt —→ where to_char(to_date(t.dt,'yyyy-mm'),'yyyy-mm') 或者where substr(t.dt,1,5)||lpad(substr(t.dt,6,2),2,'0')

hh7yx 2013-05-21

charlesxu 2013-05-21

[quote=引用 12 楼 charlesxu 的回复:] [quote=引用 6 楼 u010412956 的回复:] 借下2#的数据：
``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sc
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sum(sc)
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````
[/quote] 相同的年月，但是地区不同，能改改吗？我只要北京地区的得分、去年同期得分、历史最高得分 日期 得分 地区 2012-1 85 北京 2012-2 98 天津 2012-3 56 上海 2012-4 66 广州 2012-5 34 深圳

``````
select dt,sc,
case row_number() over(partition by substr(dt,6,2) order by rownum)
when 1 then null
else lag(sc) over(order by substr(dt,6,2)) end s_sc,
(select max(sc)
from t1
where t1.dt <= t.dt
and substr(t1.dt,6,2)=substr(t.dt,6,2)
) m_sc
from t1 t
order by dt
``````

hh7yx 2013-05-21

[quote=引用 6 楼 u010412956 的回复:] 借下2#的数据：
``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sc
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sum(sc)
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

charlesxu 2013-05-21

``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sc
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

``````
select dt,sc,
case row_number() over(partition by substr(dt,instr(dt,'-')+1,length(dt)) order by rownum)
when 1 then null
else lag(sc,1,null) over(order by substr(dt,instr(dt,'-')+1,length(dt))) end s_sc,
(select max(sc)
from t1
where t1.dt <= t.dt
and substr(t1.dt,instr(t1.dt,'-')+1,length(t1.dt))=substr(t.dt,instr(t.dt,'-')+1,length(t.dt))
) m_sc
from t1 t
order by dt
``````

rabitsky 2013-05-21

charlesxu 2013-05-21

rabitsky 2013-05-21

``````
with t1 as (
select '2011-01' dt,85 sc from dual union all
select '2011-02' dt,98 sc from dual union all
select '2011-03' dt,56 sc from dual union all
select '2011-04' dt,66 sc from dual union all
select '2012-01' dt,33 sc from dual union all
select '2012-02' dt,34 sc from dual union all
select '2012-03' dt,54 sc from dual union all
select '2012-04' dt,69 sc from dual union all
select '2013-01' dt,97 sc from dual union all
select '2013-02' dt,43 sc from dual union all
select '2013-03' dt,44 sc from dual union all
select '2013-04' dt,34 sc from dual )
select dt, sc, lag(sc,1,null) over(partition by substr(dt,-2) order by dt) last_sc,
(select max(sc)
from t b
where b.dt <= a.dt
and substr(b.dt,-2) = substr(a.dt,-2)) max_sc
from t1 a
order by 1;
``````

vanjayhsu 2013-05-21

``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sc
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

hh7yx 2013-05-21

``````with t1 as
(select '2011-01' dt, 85 sc
from dual
union all
select '2011-02' dt, 98 sc
from dual
union all
select '2011-03' dt, 56 sc
from dual
union all
select '2011-04' dt, 66 sc
from dual
union all
select '2012-01' dt, 33 sc
from dual
union all
select '2012-02' dt, 34 sc
from dual
union all
select '2012-03' dt, 54 sc
from dual
union all
select '2012-04' dt, 69 sc
from dual
union all
select '2013-01' dt, 97 sc
from dual
union all
select '2013-02' dt, 43 sc
from dual
union all
select '2013-03' dt, 44 sc
from dual
union all
select '2013-04' dt, 34 sc from dual)
select t1.dt 日期,
t1.sc 得分,
(select sc
from t1 t
where t.dt =
(select max(sc)
from t1 t
where t.dt < t1.dt
and substr(t1. dt, 6, 2) = substr(t.dt, 6, 2)) 历史最高得分
from t1;
``````

``````
select dt,sc,
case row_number() over(partition by substr(dt,-2) order by rownum)
when 1 then null
else lag(sc) over(order by substr(dt,-2),substr(dt,1,4)) end s_sc,
(select max(sc) from t1 where t1.dt <= t.dt and substr(t1.dt,-2)=substr(t.dt,-2)) m_sc
from t1 t
order by dt
``````

charlesxu 2013-05-21

Oracle

1.6w+

Oracle开发相关技术讨论

2013-05-21 11:26