日期:2014-05-17  浏览次数:20754 次

大哥大姐帮忙写个sql语句,嘿嘿!



表结构如图,例如:现在我想查询2012年8月到2013年6月每个月的得分数,按照年月排列。该怎么写sql语句呢? 

------解决方案--------------------
select score from table where Asses_Year = '2012' and Asses_Month > 8 or Asses_Year = '2013' and Asses_Month < 6 order by Asses_Month;
------解决方案--------------------

with t as
(select 2013 y,1 m,0.51 s from dual
union all
select 2013 y,6 m,0.54 s from dual
union all
select 2013 y,7 m,0.53 s from dual
union all
select 2013 y,8 m,0.54 s from dual
union all
select 2013 y,9 m,0.5 s from dual
)
select to_date(y
------解决方案--------------------
'-'
------解决方案--------------------
m,'YYYY-MM'),sum(s) from t 
where to_date(y
------解决方案--------------------
'-'
------解决方案--------------------
m,'YYYY-MM') between to_date('2013-06','YYYY-MM') and to_date('2013-08','YYYY-MM')
group by to_date(y
------解决方案--------------------
'-'
------解决方案--------------------
m,'YYYY-MM') 
order by to_date(y
------解决方案--------------------
'-'
------解决方案--------------------
m,'YYYY-MM') 



------解决方案--------------------

select ASSESS_YEAR,ASSESS_MONTH,score
from t
where ASSESS_YEAR=2012 and (ASSESS_MONTH between 8 and 12)
union all
select ASSESS_YEAR,ASSESS_MONTH,score
from t
where ASSESS_YEAR=2013 and (ASSESS_MONTH between 1 and 6)
--没有排序,影响性能
--如果要排序,加一个嵌套做ORDER ASSESS_YEAR,ASSESS_MONTH