水平获取数据而不是垂直获取数据

时间:2011-07-05 06:51:39

标签: sql

考虑查询:

Select Dealer, Sales from DEALERDATA
where period in (201106,201107)

这将得到结果:

DEALER SALES

Dealer1 100000
Dealer1 200000
Dealer2 600000
Dealer2 700000

我希望结果为

DEALER SALES SALES
Dealer1 100000 200000
Dealer2 600000 700000

如果有人能告诉我如何实现这一目标?

非常感谢, 莎朗....

1 个答案:

答案 0 :(得分:1)

对于少数列,您可以使用CASE结合总和

SELECT Dealer
, SUM(CASE WHEN period=201106 THEN Sales ELSE 0 END) sales201106
, SUM(CASE WHEN period=201107 THEN Sales ELSE 0 END) sales201107
FROM DEALERDATA
WHERE period IN (201106,201107)
GROUP BY Dealer