本文共 9235 字,大约阅读时间需要 30 分钟。
- --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
- --预订储位的库存信息列转行
- 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
-
- --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
- --(行列转换)显示预订储位的库存信息和商品信息
- 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
-
- --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
- --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
- --显示预订储位的库存信息,显示第一次进货时间
- 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
-
- --显示预订储位的库存信息和商品信息以及第一次进货时间和累计进货数量
- 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
-
- --END
转载于:https://www.cnblogs.com/accumulater/p/6101152.html