原始数据如下图所示:(商品的销售明细)
date=业务日期;Item=商品名称;saleqty=销售数量;

-- 建立测试数据(表)
create table test (Date varchar(10), item char(10),saleqty int)
insert test values('2010-01-01','AAA',8)
insert test values('2010-01-02','AAA',4)
insert test values('2010-01-03','AAA',5)
insert test values('2010-01-01','BBB',1)
insert test values('2010-01-02','CCC',2)
insert test values('2010-01-03','DDD',6)

需要实现的报表样式:每一行既每一天,显示所有商品(列)该天的销售数量;

实现的方法和思路如下:

-- 实现结果的静态SQL语句写法
-- 整理报表需要的格式
select date,
case item when 'AAA' then saleqty when null then 0 end as AAA,
case item when 'BBB' then saleqty when null then 0 end as BBB,
case item when 'CCC' then saleqty when null then 0 end as CCC,
case item when 'DDD' then saleqty when null then 0 end as DDD
from test

-- 按日期汇总行
select date,
sum(case item when 'AAA' then saleqty when null then 0 end) as AAA,
sum(case item when 'BBB' then saleqty when null then 0 end) as BBB,
sum(case item when 'CCC' then saleqty when null then 0 end) as CCC,
sum(case item when 'DDD' then saleqty when null then 0 end) as DDD
from test 
group by date

-- 处理数据:将空值的栏位填入数字0;
select date,
isnull (sum(case item when 'AAA' then saleqty end),0) as AAA,
isnull (sum(case item when 'BBB' then saleqty end),0) as BBB,
isnull (sum(case item when 'CCC' then saleqty end),0) as CCC,
isnull (sum(case item when 'DDD' then saleqty end),0) as DDD
from test 
group by date

静态SQL语句编写完成!

-- 需要动态实现的SQL部分
isnull (sum(case item when 'AAA' then saleqty end),0) as AAA,
isnull (sum(case item when 'BBB' then saleqty end),0) as BBB,
isnull (sum(case item when 'CCC' then saleqty end),0) as CCC,
isnull (sum(case item when 'DDD' then saleqty end),0) as DDD

-- 动态语句的实现
select 'isnull (sum(case item when '''+item+''' then saleqty end),0) as ['+item+']' 
from (select distinct item from test) as a
-- 这一步很关键:利用结果集给变量赋值;

-- 完成!
declare @sql varchar(8000)
set @sql = 'select Date'
select @sql = @sql + ',isnull (sum(case item when '''+item+''' then saleqty end),0) as ['+item+']' 
from (select distinct item from test) as a
select @sql = @sql+' from test group by date'
exec(@sql)

-- 删除测试数据(表)
drop table test

使用动态SQL语句实现简单的行列转置(动态产生列)的更多相关文章

  1. 动态sql语句基本语法--Exec与Exec sp_executesql 的区别

    http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1   :普通SQL语句可以用Exec执行   ...

  2. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  3. IBatis.net动态SQL语句

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...

  4. 存储过程中执行动态Sql语句

    MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...

  5. 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...

  6. IBatis.net动态SQL语句(六)

    在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...

  7. Java下拼接运行动态SQL语句

    mod=viewthread&tid=3039" target="_blank">Java拼接动态SQL的一般做法有       1.使用动态语句 非常多数 ...

  8. Java下拼接执行动态SQL语句(转)

    在实际业务中经常需要拼接动态SQL来完成复杂数据计算,网上各类技术论坛都有讨论,比如下面这些问题: http://bbs.csdn.net/topics/390876591 http://bbs.cs ...

  9. 动态SQL语句:定义(一)

    文章系列 动态SQL语句:定义(一) 静态SQL与动态SQL 静态SQL:程序运行前,具有固定的形式和结构的SQL. 动态SQL:程序运行时,能够动态改变形式或结构的SQL. 一些思考和想法 在实际的 ...

随机推荐

  1. C#(HTML)_小技巧_关于textbox框中不能输入HTML标签的解决方法(如输入“<p>”后,在提交表单时系统会崩溃)

    主要修改文件是config文件(Web.config): 1.在<pages>标签中添加属性:validateRequest="false" <pages val ...

  2. hdu 1873 看病要排队

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1873 看病要排队 Description 看病要排队这个是地球人都知道的常识.不过经过细心的0068的 ...

  3. 九度oj 1554 区间问题

    原题链接:http://ac.jobdu.com/problem.php?pid=1554 由数列的前缀和:$\begin{align*}\Large{} S_n &=\Large{}\sum ...

  4. URI与URL的区别

    (原网址:http://zhidao.baidu.com/question/38764759.html) Web上可用的每种资源 - HTML文档.图像.视频片段.程序等 - 由一个通过通用资源标志符 ...

  5. shell 函数

    1 shell函数的定义及其调用 shell函数有两种格式: function name { commands } name() { commands } 其中,name为函数名,commands为函 ...

  6. [shell基础]——split命令

    测试文本 # cat name1.txt name1 alvin1 name2 alvin2 name3 alvin3 name4 alvin4 此时目录下就只有这个文件 # ls name1.txt ...

  7. Android -- Webview自适应屏幕

    第一种                                                                                          WebSett ...

  8. C#如何设置Listview的行高-高度

    Winform窗口中,控件listview是无法设置行高的. 以加入一个imagelist(图片列表控件)实现行高的设置. ImageList imageList = new ImageList(); ...

  9. Portion of class Throwable’s inheritance hierarchy

  10. django构建blog--页面部分(eclipse+pydev)

    本文介绍的是在eclipse+pydev 平台下,利用django 搭建blog的第2部分:页面部分(主要涉及3个部分:模板.视图.URL模式) 篇幅1:创建模板 blog目录下新建一个文件夹:tem ...