[MSSQL]SQL server 2005中竖排变横排显示 收藏
2009-12-28 19:19
507 查看
SQL server 2005中竖排变横排显示 收藏 create table r(num varchar(4),[time] varchar(10),rc int,dc int) go insert r select 1,'2009-07-26',100,200 union all select 3,'2009-07-25',87,352 union all select 3,'2009-07-26',121,45 union all select 2,'2009-07-26',100,76 union all select 1,'2009-07-01',87,68 union all select 1,'2009-07-02',121,89 union all select 1,'2009-07-03',10,102 union all select 4,'2009-07-23',50,32 go CREATE PROCEDURE [dbo].[up_Test] @StartDate DATETIME, --查询开始日期 @EndDate DATETIME --查询结束日期 AS DECLARE @tmpCondition VARCHAR(200) DECLARE @tmpsql VARCHAR(MAX) DECLARE @CurrDate DATETIME DECLARE @tmpDate VARCHAR(10) SET @EndDate=DateAdd(Day,1,@EndDate) --如果数据量较大建议使用临时表 SET @tmpCondition ='WHERE T1.[time]>='''+ CONVERT(VARCHAR(10),@StartDate,120) +''' AND T1.[time]<'''+ CONVERT(VARCHAR(10),@EndDate,120) +'''' SET @tmpsql='' SET @CurrDate = DateAdd(Day,-30, Convert(Varchar(10), Getdate(), 120)) WHILE @CurrDate <= DateAdd(Day,-1, Convert(Varchar(10), Getdate(), 120)) BEGIN SET @tmpDate = CONVERT(VARCHAR(10),@CurrDate,120) SET @tmpsql = @tmpsql + ', CAST(SUM(CASE WHEN [time] = '''+@tmpDate+''' THEN T1.dc ELSE 0 END) AS INT) AS ''['+CONVERT(VARCHAR(10),@CurrDate,120)+']配发''' SET @CurrDate = @CurrDate +1 END SET @CurrDate = DateAdd(Day,-30, Convert(Varchar(10), Getdate(), 120)) WHILE @CurrDate <= DateAdd(Day,-1, Convert(Varchar(10), Getdate(), 120)) BEGIN SET @tmpDate = CONVERT(VARCHAR(10),@CurrDate,120) SET @tmpsql = @tmpsql + ', CAST(SUM(CASE WHEN [time] = '''+@tmpDate+''' THEN T1.rc ELSE 0 END) AS INT) AS ''['+CONVERT(VARCHAR(10),@CurrDate,120)+']销售''' SET @CurrDate = @CurrDate +1 END SET @tmpsql= 'SELECT ISNULL(T1.num,''合计'') AS ID,sum(dc) as dc ,sum(rc) as rc '+@tmpsql +' FROM r T1 GROUP BY T1.num WITH ROLLUP ' PRINT @tmpsql EXEC (@tmpsql) go exec up_Test N'2009-01-01','2009-06-01' drop table r drop proc up_Test
相关文章推荐
- SQL server 2005中竖排变横排显示
- Sql Server 2005 行转列的实现(横排)
- [MSSQL]维护Sql Server中表的索引等常用命令收藏
- SQL SERVER 2005 显示行号
- SQL SERVER 2005 无法显示中文问题
- 【BUG】Sql Server 2005 输入中文无法显示
- sql查询 竖排变横排及横排变竖排显示 行列转换问题
- SQL Server 2005远程连接连不上的解决办法收藏 Microsoft给的方法
- VS 2005中winForm开发(C#)—图片上传到数据库与显示(sql server 2005)
- SQL Server 2005 创建索引显示灰色,不可用??
- 汉字显示乱码(中文OS,英文SQL Server 2005)
- [MSSQL]SQL SERVER 2005-OVER (PARTITION BY…)的用法
- SQL Server 2005/2008/2012 数据库一直显示(正在恢复)无法使用
- 解决SQL Server 2005数据库中datetime时间字段在前端显示时分秒的问题
- MSSQL2005:SQL Server 2005安装图解
- 竖排显示变横排显示的方法
- SQL SERVER 2005 windows身份验证模式转为sql server验证,以及连接显示TCP/IP问题解决
- SQL Server 重建索引|索引重组|索引的碎片检查 (MSSQL个人笔记之数据库优化之路 六<SQL2005以上>)
- java 程序 连接sql server 2000能显示中文 但是连接 sql server 2005 显示乱码
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 用户 '' 登录失败。该用户与可信 SQL Server 连接无关联