当前位置: 代码迷 >> ASP.NET >> SQL sever 查询一段时间的数据,该如何解决
  详细解决方案

SQL sever 查询一段时间的数据,该如何解决

热度:1994   发布时间:2013-02-25 00:00:00.0
SQL sever 查询一段时间的数据
如 我的数据库【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页面上

------解决方案--------------------------------------------------------
declare @sql varchar(8000)
set @sql = 'select iqcid as 质检编码, prodid as 编码,product as 名称 '
select @sql = @sql + ' , max(case chname when ''' + chname + ''' then cast(testvalue as decimal(18,2)) else 0 end) [' + chname + ']'
from (select distinct chname from iqctestvalue where Prodid='LT' ) as a
set @sql = @sql + ' from iqctestvalue where Prodid=''LT'' group by iqcid, prodid,product'
exec(@sql) 
这是我以前写的,你可以参考一下。
------解决方案--------------------------------------------------------
参考
http://blog.csdn.net/htl258/article/details/3947993

可以用Ado.NET操作数据库,将数据读取出来,绑定到数据控件(比如Gridview,datalist,repeater等),显示到页面
------解决方案--------------------------------------------------------
SQL code
--1、行互列--> --> (Roy)生成測試數據if not object_id('Class') is null    drop table ClassGoCreate table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)Insert Classselect N'张三',N'语文',78 union allselect N'张三',N'数学',87 union allselect N'张三',N'英语',82 union allselect N'张三',N'物理',90 union allselect N'李四',N'语文',65 union allselect N'李四',N'数学',77 union allselect N'李四',N'英语',65 union allselect N'李四',N'物理',85 Go--2000方法:动态:declare @s nvarchar(4000)set @s=''Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'from Class group by[Course]exec('select [Student]'+@s+' from Class group by [Student]')生成静态:select     [Student],    [数学]=max(case when [Course]='数学' then [Score] else 0 end),    [物理]=max(case when [Course]='物理' then [Score] else 0 end),    [英语]=max(case when [Course]='英语' then [Score] else 0 end),    [语文]=max(case when [Course]='语文' then [Score] else 0 end) from     Class group by [Student]GO动态:declare @s nvarchar(4000)Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')生成静态:select * from     Class pivot     (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b生成格式:/*Student 数学          物理          英语          语文------- ----------- ----------- ----------- -----------李四      77          85          65          65张三      87          90          82          78(2 行受影响)*/
  相关解决方案