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

我还有许多与所示案例类似的案例陈述。

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

我希望我能清楚地解释事情。感谢您提前提供的所有帮助。

4

1 回答 1

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]
于 2012-09-07T06:30:37.810 回答