当前位置: 代码迷 >> ASP >> 怎么求本月、上月、去年同期、今年累计(用一条SQL语句)
  详细解决方案

怎么求本月、上月、去年同期、今年累计(用一条SQL语句)

热度:173   发布时间:2012-04-09 13:41:25.0
如何求本月、上月、去年同期、今年累计(用一条SQL语句)?
有如下access表:
ny   sj  mc
--------------------
2006-1-1 11 a
2006-2-1 39 a
2006-3-1 40 a
2007-1-1 12 a
2007-2-1 35 a
2007-3-1 18 a
2006-1-1 13 b
2006-2-1 35 b
2006-3-1 46 b
2007-1-1 72 b
2007-2-1 8 b
2007-3-1 17 b

用一条SQL语句求今年3月的SJ、2月的SJ、1-3月的累计SJ以及去年3月的SJ、2月的SJ、1-3月的累计SJ,请各位高手帮忙!

------解决方案--------------------
SQL code
select case conert(varchar(6),cast(ny as datetime) when '200703' then sum(sj) end '今年3月',
case conert(varchar(6),cast(ny as datetime) when '200702' then sum(sj) end '今年2月',
case when conert(varchar(6),cast(ny as datetime) IN ('200703','200702','200701') then sum(sj) end '今年1-3月',
case conert(varchar(6),cast(ny as datetime) when '200603' then sum(sj) end '去年3月',
case conert(varchar(6),cast(ny as datetime) when '200602' then sum(sj) end '去年2月',
case when conert(varchar(6),cast(ny as datetime) IN ('200603','200602','200601') then sum(sj) end '去年1-3月'
from ny