我有我正在寻找的结果集,但现在我试图通过订单ID来排除行,但我很难弄清楚如何做到这一点。重复订单ID的其他字段不同,因此我不确定如何对它们进行分组,因此它只显示一个订单ID,即使其他字段不同。请参阅下面的代码和输出。在查看输入时,请注意订单ID是相同的,我需要将它们合并为一行,即使其他字段略有不同。
WITH CTE as(
Select
[Order Details].[Order ID],
SUM(Quantity * [Rental(PLTR)1]) as [Sum of all ProductID]
FROM [Order Details]
join Products ON [Order Details].[Product ID] = Products.[Product ID] AND
[Order Details].location = Products.location
WHERE [Order Details].Location = 'OC'
group by [Order Details].[Order ID]
)
SELECT
[Order Details].[Order ID],
[Order Details].[Product ID],
P.[Sum of all ProductID],
OrigSalesman,
Enterby,
Months,
Quantity
from [Order Details]
join CTE as p on p.[Order ID] = [Order Details].[Order ID]
join Orders ON Orders.[Order ID] = [Order Details].[Order ID]
join [Job Name ID] ON [Orders].[Job Name ID] = [Job Name ID].[Job Name ID]
wHERE [Order Details].[Product ID] = '3367'
order by [Order Details].[Order ID]
这是输出:
Order ID / Product ID / Sum of all ProductID / OrigSalesman / Enterby / Months / Quantity
38119 3367 275 Susan DeMarchi 144 12 1
38119 3367 275 Susan DeMarchi 144 12 2
38119 3367 275 Susan DeMarchi 144 12 8
38119 3367 275 Susan DeMarchi 144 12 1
38301 3367 80 Susan DeMarchi 91 12 1
47476 3367 830 House Sale 25 6 1
47997 3367 0 House Sale 277 6 1
48760 3367 0 Don P. C. Carpentier 270 6 1
48761 3367 0 Don P. C. Carpentier 270 6 1
48762 3367 0 Don P. C. Carpentier 270 6 1
48763 3367 0 Don P. C. Carpentier 270 6 1
48880 3367 0 Roger Riley 270 12 1
51411 3367 0 House Sale 270 12 1
52444 3367 0 Susan DeMarchi 277 6 1
53280 3367 0 Roger Riley 25 6 1
59408 3367 90 Susan DeMarchi 25 6 1
59714 3367 75 Eddie Salti 453 1 13
60327 3367 1052 Susan DeMarchi 18 10 1
60394 3367 170 Susan DeMarchi 25 16 1
62687 3367 235 Susan DeMarchi 663 12 1
62776 3367 40 Susan DeMarchi 73 10 1
62876 3367 40 Susan DeMarchi 663 10 1
62894 3367 62.5 Susan DeMarchi 73 12 1
62898 3367 130 Susan DeMarchi 73 12 1
62915 3367 0 House Sale 270 6 1
62971 3367 84 Susan DeMarchi 663 12 1
62994 3367 363 Susan DeMarchi 663 14 1
62999 3367 75 Susan DeMarchi 663 12 1
63248 3367 62.5 Susan DeMarchi 663 12 1
63252 3367 130 Susan DeMarchi 663 12 1
63562 3367 0 House Sale 33 12 1
63894 3367 527.5 Susan DeMarchi 663 8 2
63975 3367 220 Susan DeMarchi 663 12 6
63982 3367 95 Susan DeMarchi 663 12 6
63984 3367 95 Susan DeMarchi 663 12 6
63985 3367 40 Susan DeMarchi 663 12 6
63986 3367 100 Susan DeMarchi 663 12 6
63987 3367 80 Susan DeMarchi 663 12 6
64269 3367 190 Susan DeMarchi 663 8 2
64269 3367 190 Susan DeMarchi 663 8 2
64643 3367 89 Susan DeMarchi 663 10 4
答案 0 :(得分:1)
从外部查询中移除Order Details
,并在CTE内按Product ID
移动过滤器。就是这样。