1

我有一张这样的桌子。

在此处输入图像描述

我需要最终结果只显示每个订单的一条记录(基本上合并子订单)。仅对不同子订单的收入求和(对于订单 0935744,求和 575.04 + 31.68)。一个订单最多可以有 2 名员工

最终结果应该是这样的:

order      totalrevenue  employee1 employee2 
0813700     258.57        CREW      NULL      
0935744     606.72        95liv     95nat  

我尝试过使用 row_number 并进行一些连接,但我没有运气。

示例代码

SELECT N'0813700' AS [OrderNum], N'1077980' AS [SubOrder], N'CREW' AS [employeeid], N'258.57' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1257060' AS [SubOrder], N'95LIV' AS [employeeid], N'575.04' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1342944' AS [SubOrder], N'95LIV' AS [employeeid], N'31.68' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1257060' AS [SubOrder], N'95NAT' AS [employeeid], N'575.04' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1342944' AS [SubOrder], N'95NAT' AS [employeeid], N'31.68' AS [revenue]
4

4 回答 4

0

这个怎么样?(在 OP 评论后修改)

假设:

  1. 每个订单不超过两名员工。
  2. 每个子订单一名员工(如示例中所示)
  3. 子订单的收入在子订单的行上始终重复(每个示例)

代码示例

select order, sum(revenue) as totalrevenue, max(employee1) as employee1,
case
    when max(employee1) = max(employee2) then null
    else max(employee2)
end as employee2
from (
    select order, suborder, max(revenue) as revenue, max(employeeid)
    from orders
    group by order, suborder
) SubOrderTotal
group by order

一般来说,我不建议对两名员工进行刚性转换或重复子订单收入。在处理现实世界的数据时,做出如此严格的假设通常会导致错误。但是,我不知道你的数据。

于 2012-04-30T19:07:25.570 回答
0
DECLARE @SubOrder TABLE
(
    OrderNum    INT NOT NULL,
    SubOrder    INT NOT NULL,
    EmployeeID  NVARCHAR(50) NOT NULL,
    Revenue     NUMERIC(10, 2) NOT NULL
);

INSERT  @SubOrder (OrderNum, SubOrder, EmployeeID, Revenue)
SELECT N'0813700' AS [OrderNum], N'1077980' AS [SubOrder], N'CREW' AS [employeeid], N'258.57' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1257060' AS [SubOrder], N'95LIV' AS [employeeid], N'575.04' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1342944' AS [SubOrder], N'95LIV' AS [employeeid], N'31.68' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1257060' AS [SubOrder], N'95NAT' AS [employeeid], N'575.04' AS [revenue] UNION ALL
SELECT N'0935744' AS [OrderNum], N'1342944' AS [SubOrder], N'95NAT' AS [employeeid], N'31.68' AS [revenue];

SELECT  pvt.OrderNum,
        pvt.TotalRevenue,
        pvt.[1] AS Emp1,
        pvt.[2] AS Emp2
FROM
(
        SELECT  dt.OrderNum,
                dt.EmployeeID,
                DENSE_RANK()    OVER(PARTITION BY dt.OrderNum ORDER BY dt.EmployeeID) AS Rnk,
                SUM(dt.Revenue) OVER(PARTITION BY dt.OrderNum) AS TotalRevenue
        FROM
        (
                SELECT  so.OrderNum,
                        so.EmployeeID,
                        ROW_NUMBER() OVER(PARTITION BY so.OrderNum, so.SubOrder ORDER BY @@SPID) AS RowNum,
                        so.Revenue
                FROM    @SubOrder so
        ) dt
        WHERE   dt.RowNum = 1
) src
PIVOT   ( MAX(src.EmployeeID) FOR src.Rnk IN ([1], [2]) ) pvt

结果:

OrderNum TotalRevenue Emp1   Emp2
-------- ------------ ------ -----
813700   258.57       CREW   NULL
935744   606.72       95LIV  95NAT

中间结果(...) src

OrderNum EmployeeID Rnk TotalRevenue
-------- ---------- --- ------------
813700   CREW       1   258.57
935744   95LIV      1   606.72
935744   95NAT      2   606.72

中间结果(...) dt

OrderNum EmployeeID RowNum Revenue
-------- ---------- ------ -------
813700   CREW       1      258.57
935744   95LIV      1      575.04
935744   95NAT      2      575.04
935744   95NAT      1      31.68
935744   95LIV      2      31.68
于 2012-04-30T21:50:29.420 回答
0

这应该给出您正在寻找的结果:

create table #temp
(
    ordernum int,
    suborder int,
    employeeid varchar(50),
    revenue money
)

insert into #temp values(0813700, 1077980, 'CREW', 258.57)
insert into #temp values(0935744, 1257060, '95LIV', 575.04)
insert into #temp values(0935744, 1342944, '95LIV', 31.68)
insert into #temp values(0935744, 1257060, '95NAT', 575.04)
insert into #temp values(0935744, 1342944, '95NAT', 31.68)

select ordernum
    , sum(revenueperorder) as total
    , employee1
    , case when employee1 = employee2 then null else employee2 end as employee2
from 
(
    select ordernum
        , revenue as revenueperorder
        , min(employeeid) as employee1
        , max(employeeid) as employee2
    from #temp
    group by ordernum, revenue
) x
group by ordernum, employee1, employee2

drop table #temp

Results:

813700  258.57  CREW    NULL
935744  606.72  95LIV   95NAT
于 2012-04-30T20:00:03.033 回答
0

到目前为止的答案需要一个硬编码的枢轴,其中employee1和2定义为枢轴实体,当然,如果您的数据是动态的,我想您将拥有不同数量的员工(因此需要不同数量的列) ? 如果是这样,我建议您采用混合动态 SQL / Pivot 示例,例如:

数据透视表和连接列

或这个:

sql 2005 中的 PIVOT

于 2012-04-30T21:59:49.647 回答