0

由于缺乏语言经验,我遇到了一些问题,希望您的帮助...

我希望代码返回表格,如下例所示:

T01 --  T02 -- T03
23,00  32,00  43,00

所以,这是我的代码:

SELECT
SUM([TOTAL $]) AS T01
FROM PFIN_base
WHERE [COORD] = 'alfa'
UNION
SELECT
SUM([TOTAL $]) AS T02
FROM PFIN_base
WHERE [COORD] = 'beta'
UNION
SELECT 
SUM([TOTAL $]) AS T03
FROM PFIN_base
WHERE [COORD] = 'gama'

此代码返回以下结果:

T01
23,00
32,00
43,00

我怎样才能得到想要的结果?谢谢!

4

1 回答 1

1

你想要条件聚合:

select sum(case when coord = 'alfa' then [TOTAL $] end) as T01,
       sum(case when coord = 'beta' then [TOTAL $] end) as T02,
       sum(case when coord = 'gamma' then [TOTAL $] end) as T03
from PFin_base
于 2013-06-27T15:04:54.657 回答