gpt4 book ai didi

sql - 如何删除重复的组键值并显示详细信息项(多组)

转载 作者:行者123 更新时间:2023-12-02 07:18:13 25 4
gpt4 key购买 nike

源数据和 DDL

CREATE TABLE T
([Order_NO] varchar(10), [MO_NO] varchar(10), [DEP] varchar(3), [PRD_NO] varchar(12), [QTY] int, [STD_QTY] numeric, [QTY_DIF] numeric);

INSERT INTO T
([Order_NO], [MO_NO], [DEP], [PRD_NO], [QTY], [STD_QTY], [QTY_DIF])
VALUES
('SO17110026', 'MO17110824', 'MO2', '3CA002-00120', 40681, 40380.48, 300.52),
('SO17110026', 'MO17110824', 'MO2', '3CA002A00110', 41125, 40380.48, 744.52),
('SO17110026', 'MO17110825', 'MO1', '403010060002', 40381, 40380.48, 0.52),
('SO17110026', 'MO17110825', 'MO1', '601001000734', 69, 68.102, 0.898),
('SO17110026', 'MO17110826', 'MO1', '601002000004', 2862, 2861.4858, 0.5142),
('SO17110026', 'MO17110826', 'MO1', '601005000127', 1499, 1498.6446, 0.3554),
('SO17110026', 'MO17110826', 'MO1', '601007000001', 69, 68.102, 0.898),
('SO17110029', 'MO17110825', 'MO1', '40107008CM01', 238010, 237658.3374, 351.6626),
('SO17110029', 'MO17110825', 'MO1', '40107008CM33', 110000, 109149.843, 850.157)
;
Order_NO   | MO_NO      | DEP | PRD_NO       |    QTY | STD_QTY | QTY_DIF:--------- | :--------- | :-- | :----------- | -----: | :------ | :------SO17110026 | MO17110824 | MO2 | 3CA002-00120 |  40681 | 40380   | 301    SO17110026 | MO17110824 | MO2 | 3CA002A00110 |  41125 | 40380   | 745    SO17110026 | MO17110825 | MO1 | 403010060002 |  40381 | 40380   | 1      SO17110026 | MO17110825 | MO1 | 601001000734 |     69 | 68      | 1      SO17110026 | MO17110826 | MO1 | 601002000004 |   2862 | 2861    | 1      SO17110026 | MO17110826 | MO1 | 601005000127 |   1499 | 1499    | 0      SO17110026 | MO17110826 | MO1 | 601007000001 |     69 | 68      | 1      SO17110029 | MO17110825 | MO1 | 40107008CM01 | 238010 | 237658  | 352    SO17110029 | MO17110825 | MO1 | 40107008CM33 | 110000 | 109150  | 850    

期待结果

Order_NO   | MO_NO      | DEP  | PRD_NO       |    QTY | STD_QTY | QTY_DIF:--------- | :--------- | :--- | :----------- | -----: | :------ | :------SO17110026 | MO17110824 | MO2  | 3CA002-00120 |  40681 | 40380   | 301    null       | null       | null | 3CA002A00110 |  41125 | 40380   | 745    null       | MO17110825 | MO1  | 403010060002 |  40381 | 40380   | 1      null       | null       | null | 601001000734 |     69 | 68      | 1      null       | MO17110826 | MO1  | 601002000004 |   2862 | 2861    | 1      null       | null       | null | 601005000127 |   1499 | 1499    | 0      null       | null       | null | 601007000001 |     69 | 68      | 1      SO17110029 | MO17110825 | MO1  | 40107008CM01 | 238010 | 237658  | 352    null       | null       | null | 40107008CM33 | 110000 | 109150  | 850    

演示测试链接:db<>fiddle

最佳答案

我们可以使用 ROW_NUMBER 来处理这个问题:

SELECT
CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_NO ORDER BY MO_NO, PRD_NO) = 1
THEN Order_NO END AS Order_NO,
CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_NO, MO_NO ORDER BY PRD_NO) = 1
THEN MO_NO END AS MO_NO,
CASE WHEN ROW_NUMBER() OVER (PARTITION BY Order_NO, MO_NO ORDER BY PRD_NO) = 1
THEN DEP END AS DEP,
PRD_NO,
QTY,
STD_QTY,
QTY_DIF
FROM T
ORDER BY
T.Order_NO,
T.MO_NO,
T.PRD_NO;

enter image description here

Demo

关于sql - 如何删除重复的组键值并显示详细信息项(多组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54896739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com