表说明:
1) 出货单:Delivery, DeliveryGoods,DeliveryDetail
2) 核销单:Check, CheckGoods, CheckDetail
出货单主表(Delivery)
字段名 | 中文描述 | 备注 |
DeliveryID | 出货单 | 主键 |
DeliveryDate | 出货日期 |
|
ManualId | 手工单号 |
|
出货单货品明细表(DeliveryGoods)
字段名 | 中文描述 | 备注 |
DeliveryGoodsID |
|
|
DeliveryID | 出货单 | 外键 |
Goods_No | 货号 |
|
UnitPric | 单价 |
|
出货单货品规格明细表(DeliveryDetail)
字段名 | 中文描述 | 备注 |
ID |
| 主键 |
DeliveryGoodsID |
| 外键 |
ColorID | 颜色 |
|
Long | 内长 |
|
Size | 尺码 |
|
Quantity | 数量 |
|
核销单主表(Check)
字段名 | 中文描述 | 备注 |
CheckID | 核销单 | 主键 |
CheckDate | 核销日期 |
|
ManualId | 手工单号 |
|
核销单货品明细表(CheckGoods)
字段名 | 中文描述 | 备注 |
CheckGoodsID |
|
|
CheckID |
| 外键 |
Goods_No | 货号 |
|
UnitPric | 单价 |
|
核销单货品规格明细表(CheckDetail)
字段名 | 中文描述 | 备注 |
ID |
| 主键 |
CheckGoodsID |
| 外键 |
ColorID | 颜色 |
|
Long | 内长 |
|
Size | 尺码 |
|
Quantity | 数量 |
|
要求实现以下查询结果:
项目 | 单据日期 | 货号 | 颜色 | 数量 | 金额 |
出货单 | 2007-04-09 | 021234 | 01 | 2 | 20 |
出货单 | 2007-04-09 | 011234 | 01 | 7 | 350 |
核销单 | 2007-05-10 | 011234 | 02 | 6 | 300 |
SQL代码:
ALTER PROCEDURE [dbo].[test]
@type char(200)=''
AS
BEGIN
if(@type='test_main')
begin
SELECT (CASE WHEN Checkd.CheckID>0 THEN '核销单' else '不存在' end) as 项目, Checkd.CheckDate as 单据日期, CheckGoods.Goods_No as 货号,
CheckDetail.ColorID as 颜色, CheckDetail.Quantity as 数量, CheckGoods.UnitPric*CheckDetail.Quantity as 金额
FROM Checkd
INNER JOIN CheckGoods ON Checkd.CheckID = CheckGoods.CheckID
INNER JOIN CheckDetail ON CheckDetail.CheckGoodsID=CheckGoods.CheckGoodsID
union -- union合并表
SELECT (CASE WHEN Delivery.DeliveryID>0 THEN '出货单' end) as 项目, Delivery.DeliveryDate as 单据日期, DeliveryGoods.Goods_No as 货号,
DeliveryDetail.ColorID as 颜色, DeliveryDetail.Quantity as 数量, DeliveryGoods.UnitPric*DeliveryDetail.Quantity as 金额
FROM Delivery
INNER JOIN DeliveryGoods ON Delivery.DeliveryID = DeliveryGoods.DeliveryID
INNER JOIN DeliveryDetail ON DeliveryDetail.DeliveryGoodsID= DeliveryGoods.DeliveryGoodsID
order by 项目 asc, 单据日期 desc,货号 desc,数量 desc --多条件排序
end
END
查询结果: