0
我有我正在查找的結果集,但現在我試圖通過訂單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
謝謝你的幫助。 –