我不确定这是否令人费解,请提前道歉。
CREATE TABLE TestOrder
(
[OrderLineID] INT NOT NULL IDENTITY PRIMARY KEY,
[Item] NVARCHAR(30) NULL,
[OrderQty] INT NULL,
[Status1] INT NULL,
[Quantity_at_sts1] INT NULL,
[Status2] INT NULL,
[Quantity_at_sts2] INT NULL,
[Status3] INT NULL,
[Quantity_at_sts3] INT NULL,
[Status4] INT NULL,
[Quantity_at_sts4] INT NULL,
[Status5] INT NULL,
[Quantity_at_sts5] INT NULL,
[OrderRef] NVARCHAR(10) NULL
)
INSERT INTO TestOrder
values
('TSHIRT','1','100','1','0','0','0','0','0','0','0','0','Ord.1'),
('SOCKS','4','50','4','0','0','0','0','0','0','0','0','Ord.2'),
('SHIRT','5','10','1','50','2','0','0','0','0','0','0','Ord.3'),
('SHIRT','5','100','2','0','0','0','0','0','0','0','0','Ord.3'),
('SOCKS','10','10','4','50','2','0','0','0','0','0','0','Ord.4'),
('SOCKS','10','50','3','100','1','0','0','0','0','0','0','Ord.4')
。
| OrderLineID | Item | OrderQty | Status1 | Quantity_at_sts1 | Status2 | Quantity_at_sts2 | Status3 | Quantity_at_sts3 | Status4 | Quantity_at_sts_4 | Status5 | Quantity_at_sts5 | OrderRef |
|-------------|------|----------|---------|------------------|---------|------------------|---------|------------------|---------|-------------------|---------|------------------|----------|
| 1 |TSHIRT| 1 | 100 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.1 |
| 2 |SOCKS | 4 | 50 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.2 |
| 3 |SHIRT | 5 | 10 | 1 | 50 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.3 |
| 4 |SHIRT | 5 | 100 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.3 |
| 5 |SOCKS | 10 | 10 | 4 | 50 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.4 |
| 6 |SOCKS | 10 | 50 | 3 | 100 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.4 |
因此,该表实际上是一个订单表,并且所有的“status1,quantity_at_sts1,status2 ...”等列实际上都是该订单的进度。想象一下,您处于10到100之间的各种状态,其中10个状态为“未启动”,100个状态为“已完成并已发货”。实际上,“OrderRef”列是这些关注订单的关键标识符。例如实际上,“Ord.3”和“Ord.4”的4行实际上只是每个订单上的一个项目,但每行状态不同。
到目前为止,我一直在使用case语句将所有这些状态放入一个易于阅读的列中(忽略所有强制类型转换,因为它们仅在实际数据上是必需的):
select
CASE
WHEN status2 = 0 THEN '['+CAST(status1 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts1 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))
WHEN status2 > 0 AND status3 = 0 THEN '['+CAST(status1 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts1 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - '+'['+CAST(status2 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts2 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))
WHEN status3 > 0 AND status4 = 0 THEN '['+CAST(status1 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts1 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - '+'['+CAST(status2 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts2 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - ['+CAST(status3 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts3 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))
WHEN status4 > 0 AND status5 = 0 THEN '['+CAST(status1 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts1 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - '+'['+CAST(status2 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts2 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - ['+CAST(status3 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts3 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10)) + ' - ['+CAST(status4 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts4 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))
when status5 > 0 then '['+CAST(status1 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts1 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - '+'['+CAST(status2 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts2 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))+' - ['+CAST(status3 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts3 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10)) + ' - ['+CAST(status4 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts4 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10)) + ' - ['+CAST(status5 AS NVARCHAR(10))+'] '+CAST(CAST(CAST(quantity_at_sts5 AS DECIMAL(20, 6)) AS FLOAT) AS NVARCHAR(10))
END AS 'all_statuses'
, *
from testorder
。
| all_statuses | OrderLineID | Item | OrderQty | Status1 | Quantity_at_sts1 | Status2 | Quantity_at_sts2 | Status3 | Quantity_at_sts3 | Status4 | Quantity_at_sts_4 | Status5 | Quantity_at_sts5 | OrderRef |
|------------------|--------------|------|----------|---------|------------------|---------|------------------|---------|------------------|---------|-------------------|---------|------------------|----------|
| [100] 1 | 1 |TSHIRT| 1 | 100 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.1 |
| [50] 4 | 2 |SOCKS | 4 | 50 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.2 |
| [10] 1 - [50] 2 | 3 |SHIRT | 5 | 10 | 1 | 50 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.3 |
| [100] 2 | 4 |SHIRT | 5 | 100 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.3 |
| [10] 4 - [50] 2 | 5 |SOCKS | 10 | 10 | 4 | 50 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.4 |
| [50] 3 - [100] 1 | 6 |SOCKS | 10 | 50 | 3 | 100 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | Ord.4 |
我的理想是汇总每个唯一的OrderRef值,其中all_statuses是具有该OrderRef值的所有行中的总和:
| all_statuses | OrderQty | OrderRef |
|----------------------------|------------|-----------|
| [100] 1 | 1 | Ord.1 |
| [50] 4 | 4 | Ord.2 |
| [10] 1 - [50] 2 - [100] 2 | 5 | Ord.3 |
| [10] 4 - [50] 5 - [100] 1 | 10 | Ord.4 |
,但是我遇到了2个问题:
我对STUFF FOR XML PATH有一点经验,并认为我可以通过STUFF组合来完成某种荒谬的CASE,但我一直在努力寻找一种方法。这有点太复杂了吗?
最佳答案
第一步是将列取消透视成行,可以使用CROSS APPLY
和表值构造函数来完成
SELECT o.OrderRef, x.*
FROM #TestOrder AS o
CROSS APPLY
(VALUES
(1, o.Status1, o.Quantity_at_sts1),
(2, o.Status2, o.Quantity_at_sts2),
(3, o.Status3, o.Quantity_at_sts3),
(4, o.Status4, o.Quantity_at_sts4),
(5, o.Status5, o.Quantity_at_sts5)
) x (StatusNumber, StatusID, Quantity)
WHERE x.StatusID > 0;
这使:
OrderRef StatusNumber StatusID Quantity
--------------------------------------------------
Ord.1 1 100 1
Ord.2 1 50 4
Ord.3 1 10 1
Ord.3 2 50 2
Ord.3 1 100 2
Ord.4 1 10 4
Ord.4 2 50 2
Ord.4 1 50 3
Ord.4 2 100 1
然后,您需要对按订单分组的数量进行汇总,这是一个简单的
SUM
/ GROUP BY
。我还添加了一个ROW_NUMBER()
列,以便以后在串联期间订购商品时使用:SELECT o.OrderRef,
Quantity = SUM(x.Quantity),
x.StatusID,
RowNumber = ROW_NUMBER() OVER(PARTITION BY o.OrderRef ORDER BY MIN(o.OrderLineID), MIN(x.StatusNumber))
FROM #TestOrder AS o
CROSS APPLY
(VALUES
(1, o.Status1, o.Quantity_at_sts1),
(2, o.Status2, o.Quantity_at_sts2),
(3, o.Status3, o.Quantity_at_sts3),
(4, o.Status4, o.Quantity_at_sts4),
(5, o.Status5, o.Quantity_at_sts5)
) x (StatusNumber, StatusID, Quantity)
WHERE x.StatusID > 0
GROUP BY x.StatusID, o.OrderRef
OrderRef Quantity StatusID RowNumber
----------------------------------------------
Ord.1 1 100 1
Ord.2 4 50 1
Ord.3 1 10 1
Ord.3 2 50 2
Ord.3 2 100 3
Ord.4 4 10 1
Ord.4 5 50 2 <-- TWO ROWS GROUPED HERE
Ord.4 1 100 3
最后,您可以使用上面的派生表来创建您的字符串,然后使用SQL Server的XML扩展将它们连接起来:
WITH ALLStatuses AS
( SELECT o.OrderRef,
Quantity = SUM(x.Quantity),
x.StatusID,
RowNumber = ROW_NUMBER() OVER(PARTITION BY o.OrderRef ORDER BY MIN(o.OrderLineID), MIN(x.StatusNumber))
FROM #TestOrder AS o
CROSS APPLY
(VALUES
(1, o.Status1, o.Quantity_at_sts1),
(2, o.Status2, o.Quantity_at_sts2),
(3, o.Status3, o.Quantity_at_sts3),
(4, o.Status4, o.Quantity_at_sts4),
(5, o.Status5, o.Quantity_at_sts5)
) x (StatusNumber, StatusID, Quantity)
WHERE x.StatusID > 0
GROUP BY x.StatusID, o.OrderRef
)
SELECT all_statuses = STUFF(( SELECT CONCAT(' - ', QUOTENAME(S.StatusID), ' ', s.Quantity)
FROM ALLStatuses AS s
WHERE s.OrderRef = o.OrderRef
ORDER BY RowNumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 3, ''),
o.OrderQty,
o.OrderRef
FROM #TestOrder AS o
GROUP BY o.OrderRef, o.OrderQty;
输出
all_statuses OrderQty OrderRef
------------------------------------------------
[100] 1 1 Ord.1
[50] 4 4 Ord.2
[10] 1 - [50] 2 - [100] 2 5 Ord.3
[10] 4 - [50] 5 - [100] 1 10 Ord.4