1

我有这样的查询

select company, extract(week from date) as week, sum(value) 
from temporary GROUP BY company,yearweek(date) 

在我看到之后

Company       | Week  | Value 
Dunaferr      |   1   | 310        
Dunaferr      |   2   | 220        
Dunaferr      |   3   | 310
Pentele-Invest|   1   | 450
Pentele-Invest|   2   | 543
Pentele-Invest|   3   | 344 

最后,我想要的是一张表格,该表格每周汇总按公司名称排序的值

company_name | week1 | week2  | week3 | week4 ...
dunaferr     | 35000 | 36000  | 28000 | 3411
pentele      | 34000 | 255000 | 3341  | 3433

而且我知道有一种方法可以使用旋转,但我不知道该怎么做。

4

1 回答 1

1
select  szallito_nev,
sum(case extract(week from beerkezes_datuma) when '1' then merlegelt_suly else 0 end) as 'Het 1',
sum(case extract(week from beerkezes_datuma) when '2' then merlegelt_suly else 0 end) as 'Het 2',
sum(case extract(week from beerkezes_datuma) when '3' then merlegelt_suly else 0 end) as 'Het 3',
    from 2013_backup GROUP BY szallito_nev with rollup;

我会尝试这个,它的工作很好,但不是那么优雅:因为我一年中的所有星期都必须去扔。可能有一种方法可以用循环来做到这一点

于 2013-10-30T12:33:04.003 回答