首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL server查询一段时间的数据,该如何处理

2012-09-04 
SQL server查询一段时间的数据如 我的数据库【HI】是这样IDType(int)Dtime(datetime)1haapy2012-7-21 。。。。2n

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 TBgroup 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 agroup by convert(varchar(7), Dtime, 121) 

热点排行