我有4张桌子,如下所示。下面给出的四个表的概要。
InventoryTransferHeader
TrasferCode | TransferDate | FromOutlet| T0Outlet
------------- |------------- |---------- |-----------
SYS000022 | 2016-03-08 | 710 | 707
InventoryTransferLine
TransferCode |ItemCode |ItemDescription| Qty
-------------|---------- |---------------|----
SYS000022 | 7000000064| ABCD | 4
SYS000022 | 5000000087| PQRS | 4
InventoryTransferSerials
TransferCode |ItemCode |SerialNo
-------------|---------- |-------------
SYS000022 | 7000000064| 777777777798829
SYS000022 | 7000000064| 777777777798830
SYS000022 | 7000000064| 777777777798831
SYS000022 | 7000000064| 777777777798832
SYS000022 | 5000000087| 124536850888
SYS000022 | 5000000087| 124536850889
SYS000022 | 5000000087| 124536850890
SYS000022 | 5000000087| 124536850891
插座
OutletCode | OutletDesc
-----------|-------------
707 | ABC
710 | XYZ
我想要输出如下。
TransferCode | TransferDate | FromOutlet | ToOutlet | ItemCode | ItemDescription |SerialNo | Qty
-------------|--------------|------------|----------|----------|----------------|-------------|--------
SYS000022 | 2016-03-08 | XYZ | ABC |5000000087|PQRS |124536850888,124536850889,124536850890,124536850891|4
SYS000022 | 2016-03-08 | XYZ | ABC |7000000064|ABCD |777777777798829 ,777777777798830 ,777777777798831 ,777777777798832 |4
而不是获得两行,我得到八行。这是我使用的代码。
SELECT
ISNULL(InventoryTransferHeader.TransferCode,'') AS 'TransferId',
ISNULL(InventoryTransferHeader.TransferDate,'') AS 'TransferDate',
ISNULL(InventoryTransferLine.ItemCode,'') AS 'ItemCode',
ISNULL(InventoryTransferLine.ItemDescription,'') AS 'ItemDescription',
ISNULL(InventoryTransferLine.Qty,'') AS 'Quantity',
STUFF((SELECT '/' + InventoryTransferSerials.SerialNo
FROM InventoryTransferSerials
WHERE InventoryTransferSerials.ItemCode = InventoryTransferLine.ItemCode AND
InventoryTransferSerials.TransferCode = InventoryTransferLine.TransferCode
FOR XML PATH('')),1,1,'') AS 'SN',
ISNULL(InventoryTransferHeader.FromOutlet,'') AS 'outletFrom' ,
ISNULL(F.OutletDesc,'') AS 'Outlets',
ISNULL(InventoryTransferHeader.TOOutlet,'') AS 'outletTO' ,
ISNULL(T.OutletDesc,'') AS 'Outlets'
FROM InventoryTransferHeader
INNER JOIN
Outlets F ON InventoryTransferHeader.FromOutlet = F.OutletCode
INNER JOIN
Outlets T ON InventoryTransferHeader.ToOutlet = T.OutletCode
LEFT OUTER JOIN
InventoryTransferLine ON InventoryTransferLine.TransferCode = InventoryTransferHeader.TransferCode
LEFT OUTER JOIN
InventoryTransferSerials ON InventoryTransferSerials.TransferCode = InventoryTransferLine.TransferCode
AND InventoryTransferSerials.ItemCode = InventoryTransferLine.ItemCode
WHERE (InventoryTransferHeader.TransferCode = 'SYS000022')
答案 0 :(得分:0)
删除
LEFT OUTER JOIN
InventoryTransferSerials ON InventoryTransferSerials.TransferCode = InventoryTransferLine.TransferCode
AND InventoryTransferSerials.ItemCode = InventoryTransferLine.ItemCode
您已经在查询中获得序列号为SN
您加入InventoryTransferSerials,因此表中有4个序列号InventoryTransferSerials foreach InventoryTransferLine行