- /*MSSQL 2005 列转行应用案例 By claro(陈亮) 2008-12-2 转载请包含此信息*/
- --test table KuCunMX
- If object_id ('KuCunMX') is not NULL
- Drop Table KuCunMX
- else
- Create Table KuCunMX (ShangPinBH varchar(20),ShiJian datetime,KuWeiBH varchar(20),ShuLiang int)
- Go
- --新建库存表并插入数据
- Insert Into KuCunMX
- Select '0000000000001',getdate(),'A999990000',1 Union All
- Select '0000000000002',getdate(),'A999990000',2 Union All
- Select '0000000000003',getdate(),'A999990000',3 Union All
- Select '0000000000004',getdate(),'A999990000',4 Union All
- Select '0000000000005',getdate(),'A999990000',5 Union All
- Select '0000000000006',getdate(),'E050000000',6 Union All
- Select '0000000000007',getdate(),'E050000000',7 Union All
- Select '0000000000008',getdate(),'E050000000',8 Union All
- Select '0000000000009',getdate(),'E050000000',9 Union All
- Select '0000000000010',getdate(),'E050000000',10
- --Select * From KuCunMX
- /*ShangPinBH ShiJian KuWeiBH ShuLiang
- 0000000000001 2008-12-02 23:42:11.543 A999990000 1
- 0000000000002 2008-12-02 23:42:11.543 A999990000 2
- 0000000000003 2008-12-02 23:42:11.543 A999990000 3
- 0000000000004 2008-12-02 23:42:11.543 A999990000 4
- 0000000000005 2008-12-02 23:42:11.543 A999990000 5
- 0000000000006 2008-12-02 23:42:11.543 E050000000 6
- 0000000000007 2008-12-02 23:42:11.543 E050000000 7
- 0000000000008 2008-12-02 23:42:11.543 E050000000 8
- 0000000000009 2008-12-02 23:42:11.543 E050000000 9
- 0000000000010 2008-12-02 23:42:11.543 E050000000 10
- */
- --预订储位的库存信息列转行
- Select ShangPinBH,ISNULL(A999990000,0) A999990000,ISNULL(E050000000,0) E050000000
- from (Select ShangPinBH,KuWeiBH,ShuLiang from KuCunMX ) a pivot (max(ShuLiang) for KuWeiBH
- in (A999990000,E050000000)) b
- /*
- ShangPinBH A999990000 E050000000
- 0000000000001 1 0
- 0000000000002 2 0
- 0000000000003 3 0
- 0000000000004 4 0
- 0000000000005 5 0
- 0000000000006 0 6
- 0000000000007 0 7
- 0000000000008 0 8
- 0000000000009 0 9
- 0000000000010 0 10
- */
- --test table ShangPin
- If object_id('ShangPin') is not NULL
- Drop Table ShangPin
- Else
- Create table ShangPin (ShangPinBH varchar(20),ShiJian datetime,ShangPinMC varchar(20),ISBN varchar(20),DingJia float)
- Go
- --新建商品表并插入数据
- Insert into ShangPin
- Select '0000000000001',getdate(),'SQL2000入门经典','7-1111-1111-1',10 Union All
- Select '0000000000002',getdate(),'SQL2005入门经典','7-1111-1111-2',20 Union All
- Select '0000000000003',getdate(),'SQL2008入门经典','7-1111-1111-3',30 Union All
- Select '0000000000004',getdate(),'OFFICE97入门经典','7-1111-1111-4',40 Union All
- Select '0000000000005',getdate(),'OFFICE2000入门经典','7-1111-1111-5',50 Union All
- Select '0000000000006',getdate(),'OFFICE2003入门经典','7-1111-1111-6',60 Union All
- Select '0000000000007',getdate(),'OFFICE2007入门经典','7-1111-1111-7',70 Union All
- Select '0000000000008',getdate(),'C#.NET入门经典','7-1111-1111-8',70 Union All
- Select '0000000000009',getdate(),'Java入门经典','7-1111-1111-9',90 Union All
- Select '0000000000010',getdate(),'正则表达式必知必会','7-1111-1111-0',100
- --select * from shangpin
- /*
- ShangPinBH ShiJian ShangPinMC ISBN DingJia
- 0000000000001 2008-12-02 23:47:30.107 SQL2000入门经典 7-1111-1111-1 10
- 0000000000002 2008-12-02 23:47:30.107 SQL2005入门经典 7-1111-1111-2 20
- 0000000000003 2008-12-02 23:47:30.107 SQL2008入门经典 7-1111-1111-3 30
- 0000000000004 2008-12-02 23:47:30.107 OFFICE97入门经典 7-1111-1111-4 40
- 0000000000005 2008-12-02 23:47:30.107 OFFICE2000入门经典 7-1111-1111-5 50
- 0000000000006 2008-12-02 23:47:30.107 OFFICE2003入门经典 7-1111-1111-6 60
- 0000000000007 2008-12-02 23:47:30.107 OFFICE2007入门经典 7-1111-1111-7 70
- 0000000000008 2008-12-02 23:47:30.107 C#.NET入门经典 7-1111-1111-8 70
- 0000000000009 2008-12-02 23:47:30.107 Java入门经典 7-1111-1111-9 90
- 0000000000010 2008-12-02 23:47:30.107 正则表达式必知必会 7-1111-1111-0 100
- */
- --(行列转换)显示预订储位的库存信息和商品信息
- Select sp.ShangPinBH,sp.ShangPinMC,mx11.特殊储位A999990000,mx11.特殊储位E050000000
- from ShangPin sp
- INNER JOIN (Select ShangPinBH,ISNULL(A999990000,0) 特殊储位A999990000,ISNULL(E050000000,0) 特殊储位E050000000
- from (Select ShangPinBH,KuWeiBH,ShuLiang from KuCunMX ) a pivot (max(ShuLiang) For KuWeiBH in (A999990000,E050000000)) b) mx11
- on sp.ShangPinBH=mx11.ShangPinBH
- /*
- ShangPinBH ShangPinMC 特殊储位A999990000 特殊储位E050000000
- 0000000000001 SQL2000入门经典 1 0
- 0000000000002 SQL2005入门经典 2 0
- 0000000000003 SQL2008入门经典 3 0
- 0000000000004 OFFICE97入门经典 4 0
- 0000000000005 OFFICE2000入门经典 5 0
- 0000000000006 OFFICE2003入门经典 0 6
- 0000000000007 OFFICE2007入门经典 0 7
- 0000000000008 C#.NET入门经典 0 8
- 0000000000009 Java入门经典 0 9
- 0000000000010 正则表达式必知必会 0 10
- */
- --END--go sleeping
- --test table JinHuoD
- If object_id ('JinHuoD') is not NULL
- Drop Table JinHuoD
- else
- Create Table JinHuoD (JinHuoDBH varchar(20),ShiJian datetime,caozuoy varchar(20))
- Go
- --新建进货单表并插入数据
- Insert Into JinHuoD
- Select 'JH2008120300001',getdate(),'admin' Union All
- Select 'JH2008120200002',getdate()-1,'admin' Union All
- Select 'JH2008120100003',getdate()-2,'admin'
- --select * from JinHuoD
- /*
- JinHuoDBH ShiJian caozuoy
- JH2008120300001 2008-12-03 10:23:57.513 admin
- JH2008120200002 2008-12-02 10:23:57.513 admin
- JH2008120100003 2008-12-01 10:23:57.513 admin
- */
- --test table JinHuoDMX
- If object_id ('JinHuoDMX') is not NULL
- Drop Table JinHuoDMX
- else
- Create Table JinHuoDMX (JinHuoDBH varchar(20),JinHuoDMXBH varchar(40),ShangPinBH varchar(20),FuHeSL int)
- Go
- --新建进货单明细表并插入数据
- Insert Into JinHuoDMX
- Select 'JH2008120300001','JH200812030000101','0000000000001',1 Union All
- Select 'JH2008120300001','JH200812030000102','0000000000002',2 Union All
- Select 'JH2008120300001','JH200812030000103','0000000000005',12 Union All
- Select 'JH2008120300001','JH200812030000104','0000000000004',4 Union All
- Select 'JH2008120300001','JH200812030000105','0000000000005',5 Union All
- Select 'JH2008120300001','JH200812030000106','0000000000006',6 Union All
- Select 'JH2008120300001','JH200812030000108','0000000000008',8 Union All
- Select 'JH2008120300001','JH200812030000109','0000000000008',11 Union All
- Select 'JH2008120300001','JH200812030000110','0000000000010',10 Union All
- Select 'JH2008120200002','JH200812020000201','0000000000003',1 Union All
- Select 'JH2008120200002','JH200812020000202','0000000000001',2 Union All
- Select 'JH2008120100003','JH200812010000301','0000000000009',111
- --select * from JinHuoDMX
- /*
- JinHuoDBH JinHuoDMXBH ShangPinBH FuHeSL
- JH2008120300001 JH200812030000101 0000000000001 1
- JH2008120300001 JH200812030000102 0000000000002 2
- JH2008120300001 JH200812030000103 0000000000005 12
- JH2008120300001 JH200812030000104 0000000000004 4
- JH2008120300001 JH200812030000105 0000000000005 5
- JH2008120300001 JH200812030000106 0000000000006 6
- JH2008120300001 JH200812030000108 0000000000008 8
- JH2008120300001 JH200812030000109 0000000000008 11
- JH2008120300001 JH200812030000110 0000000000010 10
- JH2008120200002 JH200812020000201 0000000000003 1
- JH2008120200002 JH200812020000202 0000000000001 2
- JH2008120100003 JH200812010000301 0000000000009 111
- */
- --显示预订储位的库存信息,显示第一次进货时间
- Select sp.ShangPinBH,sp.ShangPinMC,mx11.特殊储位A999990000,mx11.特殊储位E050000000,
- ISNULL(CONVERT(VARCHAR(100),ys1.DaoHuoRQ,23),'无进货史') 第一次进货日期
- from ShangPin sp
- INNER JOIN (Select ShangPinBH,ISNULL(A999990000,0) 特殊储位A999990000,ISNULL(E050000000,0) 特殊储位E050000000
- from (Select ShangPinBH,KuWeiBH,ShuLiang from KuCunMX ) a pivot (max(ShuLiang)
- For KuWeiBH in (A999990000,E050000000)) b) mx11
- on sp.ShangPinBH=mx11.ShangPinBH
- LeFT JOIN (Select mx21.ShangPinBH,MIN(ys.ShiJian) DaoHuoRQ
- from JinHuoD ys
- INNER JOIN JinHuoDMX mx21 on mx21.JinHuoDBH=ys.JinHuoDBH
- Group By mx21.ShangPinBH) ys1
- on ys1.ShangPinBH=sp.ShangPinBH
- /*
- ShangPinBH ShangPinMC 特殊储位A999990000 特殊储位E050000000 第一次进货日期
- 0000000000001 SQL2000入门经典 1 0 2008-12-02
- 0000000000002 SQL2005入门经典 2 0 2008-12-03
- 0000000000003 SQL2008入门经典 3 0 2008-12-02
- 0000000000004 OFFICE97入门经典 4 0 2008-12-03
- 0000000000005 OFFICE2000入门经典 5 0 2008-12-03
- 0000000000006 OFFICE2003入门经典 0 6 2008-12-03
- 0000000000007 OFFICE2007入门经典 0 7 无进货史
- 0000000000008 C#.NET入门经典 0 8 2008-12-03
- 0000000000009 Java入门经典 0 9 2008-12-01
- 0000000000010 正则表达式必知必会 0 10 2008-12-03
- */
- --显示预订储位的库存信息和商品信息以及第一次进货时间和累计进货数量
- Select sp.ShangPinBH,sp.ShangPinMC,mx11.特殊储位A999990000,mx11.特殊储位E050000000,
- ISNULL(CONVERT(VARCHAR(100),ys1.DaoHuoRQ,23),'无进货史') 第一次进货日期,
- ISNULL(ys2.yanshoul,0) 累计进货数量
- from ShangPin sp
- INNER JOIN (Select ShangPinBH,ISNULL(A999990000,0) 特殊储位A999990000,ISNULL(E050000000,0) 特殊储位E050000000
- from (Select ShangPinBH,KuWeiBH,ShuLiang from KuCunMX ) a pivot (max(ShuLiang)
- For KuWeiBH in (A999990000,E050000000)) b) mx11
- on sp.ShangPinBH=mx11.ShangPinBH
- LeFT JOIN (Select mx21.ShangPinBH,MIN(ys.ShiJian) DaoHuoRQ
- from JinHuoD ys
- INNER JOIN JinHuoDMX mx21 on mx21.JinHuoDBH=ys.JinHuoDBH
- Group By mx21.ShangPinBH) ys1
- on ys1.ShangPinBH=sp.ShangPinBH
- LeFT JOIN (Select mx31.ShangPinBH,SUM(mx31.FuHeSL) yanshoul
- from JinHuoDMX mx31
- INNER JOIN JinHuoD ys on mx31.JinHuoDBH=ys.JinHuoDBH
- Group By mx31.ShangPinBH) ys2
- on ys2.ShangPinBH=sp.ShangPinBH
- /*
- ShangPinBH ShangPinMC 特殊储位A999990000 特殊储位E050000000 第一次进货日期 累计进货数量
- 0000000000001 SQL2000入门经典 1 0 2008-12-02 3
- 0000000000002 SQL2005入门经典 2 0 2008-12-03 2
- 0000000000003 SQL2008入门经典 3 0 2008-12-02 1
- 0000000000004 OFFICE97入门经典 4 0 2008-12-03 4
- 0000000000005 OFFICE2000入门经典 5 0 2008-12-03 17
- 0000000000006 OFFICE2003入门经典 0 6 2008-12-03 6
- 0000000000007 OFFICE2007入门经典 0 7 无进货史 0
- 0000000000008 C#.NET入门经典 0 8 2008-12-03 19
- 0000000000009 Java入门经典 0 9 2008-12-01 111
- 0000000000010 正则表达式必知必会 0 10 2008-12-03 10
- */
- --END
MSSQL 2005 列转行应用案例的更多相关文章
-
sqlserver 2005列转行
isnull(stuff((select ',' + d.comname from projemp a left outer join emps c on a.empid = c.empidleft ...
-
mssql 数据库表行转列,列转行 比较经典
--行列互转 /******************************************************************************************** ...
-
转:MSSQL SERVER行转列 列转行
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
-
Oracle行转列、列转行的Sql语句总结
多行转字符串 这个比较简单,用||或concat函数可以实现 SQL Code 12 select concat(id,username) str from app_userselect i ...
-
做图表统计你需要掌握SQL Server 行转列和列转行
说在前面 做一个数据统计和分析的项目,每天面对着各种数据,经过存储过程从源表计算汇总后需要写入中间结果表以提高数据使用效率,那么此时就需要用到行转列和列转行. 1.列转行 数据经过计算加工后会直接生成 ...
-
Oracle行转列、列转行的Sql语句总结(转)
多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...
-
SQL 行转列和列转行
SQL 行转列和列转行 行列互转,是一个经常遇到的需求.实现的方法,有case when方式和2005之后的内置pivot和unpivot方法来实现. 在读了技术内幕那一节后,虽说这些解决方案早就用过 ...
-
SQL2005语句实现行转列,列转行
在做报表时,经常需要将数据表中的行转列,或者列转行,如果不知道方法,你会觉得通过SQL语句来实现非常难.这里,我将使用pivot和unpivot来实现看似复杂的功能.这个功能在sql2005及以上版本 ...
-
sql 行转 列, 列转行
行列互转 复制代码 create table test(id ),quarter int,profile int) insert into test values(,,) insert into te ...
随机推荐
-
javascript的escape()方法
escape() 方法:采用ISO Latin字符集对指定的字符串进行编码.所有的空格符.标点符号.特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码 ...
-
python简易爬虫来实现自动图片下载
菜鸟新人刚刚入住博客园,先发个之前写的简易爬虫的实现吧,水平有限请轻喷. 估计利用python实现爬虫的程序网上已经有太多了,不过新人用来练手学习python确实是个不错的选择.本人借鉴网上的部分实现 ...
-
UI设计趋势:渐变设计风格重新回归主流
在扁平化设计刚刚兴起之时,渐变是设计师们避之不及的设计手法,然而今天它已经正式回归.几乎是在一夜之间,无数的网站开始使用渐变色. 从背景到图片上的色彩叠加,包括UI元素上所遮盖的色彩,所有的这一切都表 ...
-
Spring.Net+Nhibernate
Spring.net+Nhibernate系列优秀文章导航 冬哥的Spring.Net+Nhibernate Spring.Net+NHibenate+Asp.Net mvc +ExtJs 系列 NH ...
-
Java中int与Integer
一般小写字母开头的是数据类型(如int double),大写字母开头的一般是封装为类(如Double),里面有很多方法,比如实行转换Integer.parseInt(arg0),可以把其他类型的数据转 ...
-
Matlab spline
请记住,,平稳 早期project图时,把富有弹性的细长木条(所谓样条)用压铁固定在样点上,在其它地方让它*弯曲,然后沿木条画下曲线. 成为样条曲线 三次样条插值(简称Spline插值)是通过一系列 ...
-
MarkdownPad2之安装破解
MarkdownPad2之安装破解 一.下载破解版 1.地址链接:http://pan.baidu.com/s/1i5JzG13 密码: 4jgw 2.按步骤安装后,进行汉化:[Tool]--> ...
-
.net到Java那些事儿--structs做了那些事(二)
一.跟着项目先来看下structs怎么执行的 首先看下web.xml配置文件,下面有如下代码 <filter> <filter-name>struts2</fi ...
-
jQuery遍历-祖先
祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...
-
RSA非对称加密简析-java
1 非对称加密算法 1.1 概述 1976年,美国学者Dime和Henman为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这 ...