2012-09-07 46 views
0

我不知道該怎麼做,使這項工作......我要回到我的數據看起來像這樣支點在第一場

Vendor1, sum(field1), sum(field2), sum(field3), sum(field4)... 
Vendor2, sum(field1), sum(field2), sum(field3), sum(field4)... 
Vendor3, sum(field1), sum(field2), sum(field3), sum(field4)... 
Vendor4, sum(field1), sum(field2), sum(field3), sum(field4)... 

但是,我的數據返回保持像這樣

Vendor1, field1, field2, filed3 field4... 
Vendor1, field1, field2, filed3 field4... 
Vendor1, field1, field2, filed3 field4... 
Vendor1, field1, field2, filed3 field4... 

這裏有一點我至今

select substring(a.warehouse, 2, len(a.warehouse)) as 'Name of Vendor', 
Case 
when b.status = 3 then 
sum(b.id) over (partition by substring(a.warehouse, 2, len(a.warehouse))) 
else 0 
End as 'Number of Orders', 
Case 
when b.status = 4 then 
sum(b.P_Name) over (partition by substring(a.warehouse, 2, len(a.warehouse))) 
else 0 
End as 'Number of Items' 
from [Table1]a 
join [Table2]b 
on a.OrderID = b.OrderID 

我有一些額外的case語句做一些類似所示。

我不知道的是how to pivot this as a grouping

我希望我解釋清楚。感謝您提前提供所有幫助。

+2

你可以發佈你的表結構和一些示例數據嗎? – Taryn

+0

是的,我們需要所有的相關數據。表結構,樣本行,一切。這樣很容易幫助\ – Coffee

回答

0

將整個select語句包裝在另一個SELECT中,然後GROUP BY。 ie:

SELECT a.[Name of Vendor], sum(a.[Number of Orders]), sum(a.[Number of Items]), ... 
FROM 
(
    select substring(a.warehouse, 2, len(a.warehouse)) as 'Name of Vendor', 
    Case 
    when b.status = 3 then 
    ... 
) AS a 
GROUP BY a.[Name of Vendor]