以下查询使用PIVOT显示为1到6的数量订购给定产品的每个数量的次数。
结果太细,我希望列反映范围,例如[1-2],[3-4],[5-6]通过对结果求和。
我该怎么做?
WITH Quantities AS
(
SELECT ProductID, Quantity
FROM QuantityTest
)
SELECT * FROM Quantities
PIVOT(COUNT(Quantity) FOR Quantity IN ([1],[2],[3],[4],[5],[6]))AS Pvt;
CREATE TABLE [dbo].[QuantityTest]
(
[ProductID] [int] NOT NULL,
[Quantity] [int] NOT NULL
)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (71, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (72, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (74, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (14, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (9, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (62, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (58, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (45, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (43, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (47, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (42, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (41, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (8, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (70, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (30, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (54, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (25, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (62, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (65, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (63, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (32, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (58, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (29, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (22, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (49, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (26, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (33, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (38, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (55, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (72, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (14, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (71, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (7, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (77, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 6)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (9, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (76, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (32, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (10, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (73, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (78, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (75, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (56, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (53, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (60, 5)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (65, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (15, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (30, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (7, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (11, 1)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (54, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (12, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (29, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (55, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (61, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (70, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (26, 3)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (64, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (66, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (25, 2)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (16, 4)
GO
INSERT [dbo].[QuantityTest] ([ProductID], [Quantity]) VALUES (68, 3)
GO
答案 0 :(得分:2)
您可以尝试使用条件聚合:
SELECT
ProductId,
[1-2] = SUM(CASE WHEN Quantity >= 1 AND Quantity <= 2 THEN 1 ELSE 0 END),
[3-4] = SUM(CASE WHEN Quantity >= 3 AND Quantity <= 4 THEN 1 ELSE 0 END),
[5-6] = SUM(CASE WHEN Quantity >= 5 AND Quantity <= 6 THEN 1 ELSE 0 END)
FROM QuantityTest
GROUP BY ProductId
答案 1 :(得分:1)
当有人发布DDL和样本数据时,看起来很棒,令人耳目一新。感谢您提供一个简单的解决方案!
WITH Quantities AS
(
SELECT ProductID, Quantity
FROM QuantityTest
)
SELECT ProductID, [1]+[2] as [1-2], [3]+[4] as [3-4], [5]+[6] as [5-6] FROM Quantities
PIVOT(COUNT(Quantity) FOR Quantity IN ([1],[2],[3],[4],[5],[6]))AS Pvt;