1

这是在 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         |

我想知道是否有可能写一些东西,以便对于每个 shipping_id,它返回 shipping_header 详细信息,下面列出了 shipping_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
4

2 回答 2

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
于 2017-04-25T09:01:17.947 回答
1

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

编辑:使用 CTE 仅考虑其中的记录SHIPMENT_HEADERSHIPMENT_DETAIL两者。

with 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。我们正在硬编码HEADERnull这里的项目。现在 union 的第二部分是您当前的查询货件详细信息,除了一个额外的列DETAIL。它是匹配列UNION

最后,只需将其封装在一个 select 子句中order by

于 2017-04-25T08:58:51.240 回答