使用动态SQL语句实现简单的行列转置(动态产生列)
原始数据如下图所示:(商品的销售明细)
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语句实现简单的行列转置(动态产生列)的更多相关文章
- 动态sql语句基本语法--Exec与Exec sp_executesql 的区别
http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1 :普通SQL语句可以用Exec执行 ...
- mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- IBatis.net动态SQL语句
在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...
- 存储过程中执行动态Sql语句
MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...
- 【转】mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
转自:除非申明,文章均为一号门原创,转载请注明本文地址,谢谢! 转载地址:http://blog.csdn.net/kutejava/article/details/9164353#t5 1. if ...
- IBatis.net动态SQL语句(六)
在学习动态SQL语句之前,首先必须对条件查询有一定了解,先来学习如何向IBatis.Net的映射文件里传入参数. 一.条件查询 1.传递单个参数 如根据Id查询: <select id=&quo ...
- Java下拼接运行动态SQL语句
mod=viewthread&tid=3039" target="_blank">Java拼接动态SQL的一般做法有 1.使用动态语句 非常多数 ...
- Java下拼接执行动态SQL语句(转)
在实际业务中经常需要拼接动态SQL来完成复杂数据计算,网上各类技术论坛都有讨论,比如下面这些问题: http://bbs.csdn.net/topics/390876591 http://bbs.cs ...
- 动态SQL语句:定义(一)
文章系列 动态SQL语句:定义(一) 静态SQL与动态SQL 静态SQL:程序运行前,具有固定的形式和结构的SQL. 动态SQL:程序运行时,能够动态改变形式或结构的SQL. 一些思考和想法 在实际的 ...
随机推荐
- HTML5的placeholder属性如何实现换行
在HTML5中,placeholder是一个非常有用的属性,当控件中无内容时可以代替UI控件的提示功能,而不需要写额外的代码.但如果有一个textarea控件,我们需要多行的文本提示信息时,使用”\n ...
- iOS学习之UI自定义cell
一.自定义Cell 为什么需要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.如下图所示的这些Cell ...
- shell 函数
1 shell函数的定义及其调用 shell函数有两种格式: function name { commands } name() { commands } 其中,name为函数名,commands为函 ...
- 从一个Activity返回上一个Activity
从一个Activity返回上一个Activity 要求:保留上一个Activity的数据 方法: 第一步:从Activity1转向Activity2时,用startActivityForResult而 ...
- 2015最新百度搜索引擎(seo优化)排名算法
多少年来,对于弄清百度排名算法成为了一代又一代站长的最高目标.随着百度推出了搜索引擎网页质量**,直接揭开了神秘的百度排名算法,这是作为站长福音啊.现在小编就来为大家介绍一下. 首先想要得到直接需要的 ...
- 使用python远程登录
最近要使用python做一个在web上管理交换机的程序,需要远程登录,就查了点资料,由于还没有搞到交换机,就先用自己的机器测试一下. 首先python的标准库中包含telnet,用起来也很方便,查看一 ...
- c++ dirname() basename()
http://linux.about.com/library/cmd/blcmdl3_dirname.htm #include <iostream> #include <libgen ...
- ptype_base和ptype_all学习笔记
"linux-2.6.32/include/linux/netdevice.h" struct packet_type { __be16 type; /* This is real ...
- 【BZOJ】【2038】小Z的袜子
填个坑吧,学习了莫队算法.我也忘记是看的哪位大牛的博客&代码学习的了T_T,如果您发现了的话请私信我,我会注明学自您的代码. 另外感谢@PoPoQQQ大神 好,进入正文,莫队算法,也算是一种暴 ...
- Leetcode#90 Subsets II
原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...