SQL Server:合并子查询,不重复

时间:2015-04-07 06:48:04

标签: sql sql-server tsql

我想将此结果合并为2行,无论如何都要这样做?提前谢谢!

目前的结果:

cust-no     document    order-no    Black Description          Black CR     Yellow Description           Yellow CR
CE074L00    10012107    0           NULL                       NULL         841437 P.CART YLW C3501S;    -5
CE074L00    10012107    0           NULL                       NULL         841696 P.CART YLW C5502S;    -7
CE074L00    10012107    0           841436 P.CART BLK C3501S;  -8           NULL                         NULL
CE074L00    10012107    0           841695 P.CART BLK C5502S;  -3           NULL                         NULL

预期结果:

cust-no     document    order-no    Black Description          Black CR     Yellow Description           Yellow CR
CE074L00    10012107    0           841436 P.CART BLK C3501S;  -8           841437 P.CART YLW C3501S;    -5
CE074L00    10012107    0           841436 P.CART BLK C3501S;  -3           841696 P.CART YLW C5502S;    -7

当前的SQL查询:

select 
    a.[cust-no], a.[document],a.[order-no],
    a.[Black Description],a.[Black CR],a.[Yellow Description],a.[Yellow CR]
from
    (select 
         i1.[cust-no], i1.[document], i1.[order-no], 
         il1.[description] [Black Description], 
         il1.[qty-shipped] [Black CR], null [Yellow Description],
         null [Yellow CR] 
     from 
         invoice i1 
     inner join 
         [invoice-line] il1 on il1.[document] = i1.[document] 
     inner join
         toner t on t.[edp code] = il1.[item-no] 
                  and t.[color] = 'black' and i1.[dbill-type] = 'PS' 
                  and i1.[invoice-date] > '2015-01-01' 
                  and i1.[order-code] = 'FOCA'
                  and i1.[cust-no] = 'CE074L00'

    union 

    select 
        i1.[cust-no], i1.[document], i1.[order-no],
        null [Black Description], null [Black CR],
        il1.[description] [Yellow Description],il1.[qty-shipped] [Yellow CR] from invoice i1 
    inner join [invoice-line] il1 on il1.[document] = i1.[document] inner join
    toner t on t.[edp code] = il1.[item-no] and t.[color] = 'yellow' and i1.[dbill-type] = 'PS' 
    and i1.[invoice-date] > '2015-01-01' 
    and i1.[order-code] = 'FOCA'
    and i1.[cust-no] = 'CE074L00') a

修改后的查询仍然只显示一行,不知道如何使用 RowNo

SELECT 
    a.[cust-no],
    a.[document],
    a.[order-no],
    MAX(CASE WHEN a.[color] = 'black' THEN a.[description] END) AS [Black Description],
    MAX(CASE WHEN a.[color] = 'black' THEN a.[qty-shipped] END) AS [Black CR],
    MAX(CASE WHEN a.[color] = 'yellow' THEN a.[description] END) AS [Yellow Description],
    MAX(CASE WHEN a.[color] = 'yellow' THEN a.[qty-shipped] END) AS [Yellow CR] 
FROM 
(
SELECT 
    ROW_NUMBER() OVER (PARTITION BY i1.[cust-no],i1.[document],i1.[order-no] ORDER BY i1.[cust-no] desc) AS RowNo,
    i1.[cust-no],i1.[document],i1.[order-no],t.[color],il1.[description],il1.[qty-shipped]
FROM 
    invoice i1 
INNER JOIN 
    [invoice-line] il1 
ON 
    il1.[document] = i1.[document]
INNER JOIN 
    toner t 
ON 
    t.[edp code] = il1.[item-no]
WHERE
    t.[color] IN('black', 'yellow')
    and i1.[dbill-type] = 'PS' 
    and i1.[invoice-date] > '20150101' 
    and i1.[order-code] = 'FOCA'
    and i1.[cust-no] = 'CE074L00'
) AS a
GROUP BY
    a.[cust-no],
    a.[document],
    a.[order-no]

修改后的查询结果:

cust-no     document    order-no    Black Description           Black CR    Yellow Description          Yellow CR
CE074L00    10012107    0           841695 P.CART BLK C5502S;   -3          841696 P.CART YLW C5502S;   -5

测试数据:

create table #Invoice(
    [document] int,
    [cust-no] varchar(15),
    [order-no] int,
    [dbill-type] varchar(15),
    [invoice-date] datetime,
    [order-code] varchar(15))

create table #Invoice_line(
    [document] int,
    [item-no] int,
    [description] varchar(100),
    [qty-shipped] int)

create table #toner(
    [edp code] int,
    [color] varchar(15))

insert into #invoice values (10012107,'CE074L00',0,'PS','2015-03-01','FOCA')
insert into #Invoice_line values (10012107,841436,'841436 P.CART BLK C3501S;',-8)
insert into #Invoice_line values (10012107,841695,'841695 P.CART BLK C5502S;',-3)
insert into #Invoice_line values (10012107,841437,'841437 P.CART YLW C3501S;',-5)
insert into #Invoice_line values (10012107,841696,'841696 P.CART YLW C5502S;',-7)
insert into #toner values(841436,'black')
insert into #toner values(841695,'black')
insert into #toner values(841437,'yellow')
insert into #toner values(841696,'yellow')

查询测试:

SELECT 
    a.[cust-no],
    a.[document],
    a.[order-no],
    MAX(CASE WHEN a.[color] = 'black' THEN a.[description] END) AS [Black Description],
    MAX(CASE WHEN a.[color] = 'black' THEN a.[qty-shipped] END) AS [Black CR],
    MAX(CASE WHEN a.[color] = 'yellow' THEN a.[description] END) AS [Yellow Description],
    MAX(CASE WHEN a.[color] = 'yellow' THEN a.[qty-shipped] END) AS [Yellow CR] 
FROM 
(
    SELECT 
        ROW_NUMBER() OVER (PARTITION BY i1.[cust-no],i1.[document],i1.[order-no] ORDER BY i1.[cust-no] desc) AS RowNo,
        i1.[cust-no],i1.[document],i1.[order-no],t.[color],il1.[description],il1.[qty-shipped]
    FROM 
        #invoice i1 
    INNER JOIN 
        #invoice_line il1 
    ON 
        il1.[document] = i1.[document]
    INNER JOIN 
        #toner t 
    ON 
        t.[edp code] = il1.[item-no]
    WHERE
        t.[color] IN('black', 'yellow')
        and i1.[dbill-type] = 'PS' 
        and i1.[invoice-date] > '20150101' 
        and i1.[order-code] = 'FOCA'
        and i1.[cust-no] = 'CE074L00'
) AS a
GROUP BY
    a.[cust-no],
    a.[document],
    a.[order-no]

3 个答案:

答案 0 :(得分:2)

您可以使用case-based aggregationconditional aggregation

SELECT 
    i1.[cust-no],
    i1.[document],
    i1.[order-no],
    MAX(CASE WHEN t.[color] = 'black' THEN il1.[description] END) AS [Black Description],
    MAX(CASE WHEN t.[color] = 'black' THEN il1.[qty-shipped] END) AS [Black CR],
    MAX(CASE WHEN t.[color] = 'yellow' THEN il1.[description] END) AS [Yellow Description],
    MAX(CASE WHEN t.[color] = 'yellow' THEN il1.[qty-shipped] END) AS [Yellow CR] 
FROM invoice i1 
INNER JOIN [invoice-line] il1 
    ON il1.[document] = i1.[document]
INNER JOIN toner t 
    ON t.[edp code] = il1.[item-no]
WHERE
    t.[color] IN('black', 'yellow')
    and i1.[dbill-type] = 'PS' 
    and i1.[invoice-date] > '20150101' 
    and i1.[order-code] = 'FOCA'
    and i1.[cust-no] = 'CE074L00'
GROUP BY
    i1.[cust-no],
    i1.[document],
    i1.[order-no]

答案 1 :(得分:0)

@OP,似乎这很好用,如果我错了,请纠正我。

SELECT a.[cust-no]
    , a.[document]
    , a.[order-no]
    , MAX(CASE WHEN a.[color] = 'black' THEN a.[description] END) AS [Black Description]
    , MAX(CASE WHEN a.[color] = 'black' THEN a.[qty-shipped] END) AS [Black CR]
    , MAX(CASE WHEN a.[color] = 'yellow' THEN a.[description] END) AS [Yellow Description]
    , MAX(CASE WHEN a.[color] = 'yellow' THEN a.[qty-shipped] END) AS [Yellow CR]
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY i1.[cust-no], i1.[document], i1.[order-no], t.[color] ORDER BY i1.[cust-no] DESC) AS RowNo
        , i1.[cust-no]
        , i1.[document]
        , i1.[order-no]
        , t.[color]
        , il1.[description]
        , il1.[qty-shipped]
    FROM #invoice i1
    INNER JOIN #invoice_line il1
        ON il1.[document] = i1.[document]
    INNER JOIN #toner t
        ON t.[edp code] = il1.[item-no]
    WHERE t.[color] IN ('black', 'yellow')
        AND i1.[dbill-type] = 'PS'
        AND i1.[invoice-date] > '20150101'
        AND i1.[order-code] = 'FOCA'
        AND i1.[cust-no] = 'CE074L00'
    ) AS a
GROUP BY a.[cust-no]
    , a.[document]
    , a.[order-no]
    , a.[RowNo]

答案 2 :(得分:0)

Evaldas是对的。当您向PARTITION BY子句添加Color时,您将获得您正在寻找的分区:

RowNo   cust-no document    order-no    color   description qty-shipped
1   CE074L00    10012107    0   black   841436 P.CART BLK C3501S;   -8
2   CE074L00    10012107    0   black   841695 P.CART BLK C5502S;   -3
1   CE074L00    10012107    0   yellow  841437 P.CART YLW C3501S;   -5
2   CE074L00    10012107    0   yellow  841696 P.CART YLW C5502S;   -7

当您将RowNo添加到GROUP BY子句时,您将获得两行而不是一行,因为它现在也在RowNo上进行分组。

cust-no document    order-no    Black Description   Black CR    Yellow Description  Yellow CR
CE074L00    10012107    0   841436 P.CART BLK C3501S;   -8  841437 P.CART YLW C3501S;   -5
CE074L00    10012107    0   841695 P.CART BLK C5502S;   -3  841696 P.CART YLW C5502S;   -7