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

SQL server查询一段时间的数据
如 我的数据库【HI】是这样 
 
  ID Type(int) Dtime(datetime)
  1 haapy 2012-7-21 。。。。
  2 not haapy 2012-7-21 。。。。
  3 just so so 2012-7-22.。。。  

我想输出的是  
    | happy |just so so | not happy
Jul-12(7月12号)|30(次数) | 30 | 30
Jul-13 |18 | 30 | 30
Jul-14 | 30 | 30 | 30


还有这种格式
  |happy |just so so | not happy
Jan(月份)| 120(次数)| 30 | 30
Feb | 30 | 30 | 30
Mar | 30 | 30 | 30


大概就是这样 求怎么写SQL server 语句 还有连接好数据库后 怎么把这些数据显示在aspx页面上

------解决方案--------------------
SQL code
select  Dtime,sum(case when Type='happy' then 1 else 0 end ) as 'happy',
        sum(case when Type='just so so' then 1 else 0 end ) as 'just so so',
        sum(case when Type='not happy' then 1 else 0 end ) as 'not happy'
from TB
group by  Dtime

------解决方案--------------------
from Flow 2:
SQL code

select convert(varchar(7), Dtime, 121) as month, 
    sum([happy]) as [happy], 
    sum([just so so]) as [just so so], 
    sum([not happy]) as [not happy]
from (
    select  Dtime,sum(case when Type='happy' then 1 else 0 end ) as [happy],
        sum(case when Type='just so so' then 1 else 0 end ) as [just so so],
        sum(case when Type='not happy' then 1 else 0 end ) as [not happy]
    from TB
    group by  Dtime
) as a
group by convert(varchar(7), Dtime, 121)