【发布时间】:2017-03-25 09:06:09
【问题描述】:
我开始学习 SQL Server,想请教一下这个问题的想法。我有一个库存系统,其中包含以下表格:
Delivery Table (Stock_ID, Stock_Name, Quantity, Delivery_Date)Disposal (Stock_ID, Stock_Name, Quantity, Disposal_Date)
示例Delivery 表:
Stock_ID Stock_Name Quantity Delivery_Date
0001 Plates 500 6/6/2015
0002 Glasses 1000 6/7/2015
0003 Plates 800 9/8/2016
0004 Spoon 200 9/8/2016
0005 Glasses 300 9/8/2016
0006 Plates 1000 10/1/2017
0007 Spoon 800 10/1/2017
0008 Glasses 200 10/1/2017
示例Disposal 表:
Stock_ID Stock_Name Quantity Delivery_Date
0001 Plates 20 9/6/2015
0002 Glasses 100 10/7/2015
0003 Plates 30 10/8/2016
0004 Spoon 20 12/8/2016
0005 Glasses 10 12/8/2016
0006 Plates 100 12/1/2017
0007 Spoon 20 12/1/2017
0008 Glasses 20 12/1/2017
基本上,我想获得每年的交付和处置摘要,以及所有库存的年末余额。
结果表希望如下所示:
Stock_Name: Plates
2015 2016 2017
1. Beginning Balance 0 480 1250
2. Delivery 500 800 1000
3. Disposal 20 30 100
4. Balance 480 1250 2150
我正在尝试PIVOT 和UNPIVOT,但我似乎无法掌握如何让它发挥作用。请,任何帮助将不胜感激。谢谢!
【问题讨论】:
-
告诉我们你有什么尝试?因为应该是一个简单的PIVOT.
标签: sql-server tsql pivot