我有一张这样的桌子。
我需要最终结果只显示每个订单的一条记录(基本上合并子订单)。仅对不同子订单的收入求和(对于订单 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]