2个表 - 当列值更改时,从不同的表中选择一行

时间:2017-04-25 08:50:43

标签: sql sql-server ssms ssms-2016

这是在MS SSMS 2016中。我有两个表 - SHIPMENT_HEADER和SHIPMENT_DETAIL。

SHIPMENT_HEADER

shipment_id |  customer  | 
------------|------------|
SH001       |    cust1   |
SH002       |    cust2   |

Shipment_detail有类似的列

SHIPMENT_DETAIL

shipment_id | customer | item   | requested_qty|
------------|----------|--------|--------------|
SH001       |   cust1  |  item1 |    2         |
SH001       |   cust1  |  item2 |    2         |
SH001       |   cust1  |  item3 |    1         |
SH002       |   cust2  |  item1 |    2         |
SH002       |   cust2  |  item2 |    2         |

我想知道是否有可能写出一些内容,以便每次出货时都会返回出货订单的详细信息,下面列出了shipment_details,所以:

shipment_id | instruction  |customer | item   | requested_qty|
------------|--------------|---------|--------|--------------|
SH001       |    HEADER    |  cust1  |  NULL  |    5         |
SH001       |    DETAIL    |  cust1  |  item1 |    2         |
SH001       |    DETAIL    |  cust1  |  item2 |    2         |
SH001       |    DETAIL    |  cust1  |  item3 |    1         |
SH002       |    HEADER    |  cust2  |  NULL  |    4         |
SH002       |    DETAIL    |  cust2  |  item1 |    2         |
SH002       |    DETAIL    |  cust2  |  item2 |    2         |

我认为可能是UNION选择标题然后选择详细信息,但如果我一次查询多个货件,它会选择所有标题然后选择所有详细信息,而不是标题详细信息,标题详细信息。

到目前为止,基本上只是单独选择每个位。这是用UNION编写的,因此如果使用另一种方法,可能不需要一些NULL列

SELECT
SH.SHIPMENT_ID 'SHIPMENT_ID', 
'HEADER' AS 'INSTRUCTION_TYPE',
SH.CUSTOMER 'CUSTOMER', 
NULL 'ITEM',
B.TOTAL_QTY
FROM SHIPMENT_HEADER SH
    INNER JOIN (
    SELECT SHIPMENT_ID, SUM(REQUESTED_QTY) 'TOTAL_QTY'
    FROM SHIPMENT_DETAIL
    GROUP BY SHIPMENT_ID
    ) AS B
ON SH.SHIPMENT_ID = B.SHIPMENT_ID
WHERE SH.SHIPMENT_ID IN ('SH001','SH002')
UNION
SELECT 
SD.SHIPMENT_ID,
'SHIPMENT_DETAIL' AS 'INSTRUCTION_TYPE',
SD.CUSTOMER,
SD.ITEM,
SD.REQUESTED_QTY
FROM SHIPMENT_DETAIL SD
WHERE SD.SHIPMENT_ID IN (
'SH001', 'SH002'
)
ORDER BY 1, 2

2 个答案:

答案 0 :(得分:1)

试试这个。在这里,我假设shipment_id根据您的示例,customer具有相同的customer。如果不是这种情况,您必须告诉HEADER要显示SHIPMENT_HEADER条记录。

编辑:使用CTE仅考虑SHIPMENT_DETAILwith t_SHIPMENT_DETAIL as (SELECT d.shipment_id, d.customer, d.item, d.requested_qty FROM SHIPMENT_DETAIL d inner join SHIPMENT_HEADER h on d.shipment_id=h.shipment_id ) SELECT * FROM ( SELECT shipment_id, 'HEADER' AS instruction, customer, NULL AS item, sum(requested_qty) AS requested_qty FROM t_SHIPMENT_DETAIL GROUP BY shipment_id, customer UNION ALL SELECT shipment_id, 'DETAIL' AS instruction, customer, item, requested_qty FROM t_SHIPMENT_DETAIL ) ORDER BY shipment_id, instruction DESC 两者中的记录。

sum(requested_qty)

说明:联合中的第一个查询是通过shipment_id, customer获取HEADER组。我们在这里为项目硬编码nullDETAIL。现在联合的第二部分是您当前的查询货件详细信息,但额外的列UNION除外。它是为了匹配order by

中的列

最后,将其封装在 override func viewDidAppear(_ animated: Bool) { setResetNavigation() self.navigationController?.navigationBar.backItem?.title = "" } 的select子句中。

答案 1 :(得分:1)

您可以使用以下声明:

(
SELECT header.shipment_id, 'HEADER' as 'instruction', header.customer, NULL as 'item', SUM(requested_qty) as 'requested_qty'
FROM SHIPMENT_HEADER header
LEFT JOIN SHIPMENT_DETAIL detail
ON header.shipment_id = detail.shipment_id
AND header.customer = detail.customer
GROUP BY header.shipment_id, header.customer
)
UNION
(
SELECT shipment_id, 'DETAIL', customer, item, requested_qty
FROM SHIPMENT_DETAIL
)
ORDER BY 1, 2 DESC, 3, 4

它给出了以下结果

shipment_id instruction customer   item       requested_qty
----------- ----------- ---------- ---------- -------------
SH001       HEADER      cust1      NULL       5
SH001       DETAIL      cust1      item1      2
SH001       DETAIL      cust1      item2      2
SH001       DETAIL      cust1      item3      1
SH002       HEADER      cust2      NULL       4
SH002       DETAIL      cust2      item1      2
SH002       DETAIL      cust2      item2      2