当前位置: 首页 > 知识库问答 >
问题:

我只是不能切换一些列SQL server(简单)[重复]

易炳
2023-03-14

我的查询:

select (sum( c20 ))as total , año
FROM(
select  distinct  datepart(year,DATEADD(DAY,CONVERT(FLOAT(10),KDUD.C16),A1.C7))as año, (a1.c20) as c20
from

KDUE A1
LEFT JOIN KDMM ON KDMM.C1='U' AND A1.C3=KDMM.C2 AND A1.C4=KDMM.C3 AND       A1.C5=KDMM.C4
LEFT JOIN KDMS ON A1.C1=KDMS.C1
LEFT JOIN KDUD ON A1.C2=KDUD.C2
LEFT JOIN KDUV ON A1.C18=KDUV.C2
where a1.c20 != 0) as aas

group by año

这给出了这个表的结果:

total                                   año
--------------------------------------- -----------
409782.45                               2013
2442993.24                              2014
10810460.01                             2015

但我希望我能把年份作为标题,把总数作为一行。。。帮助:c

共有1个答案

郭浩穰
2023-03-14
with x as 
    (select (sum( c20 ))as total , año
FROM(
select  distinct  datepart(year,DATEADD(DAY,CONVERT(FLOAT(10),KDUD.C16),A1.C7))as año, (a1.c20) as c20
from

KDUE A1
LEFT JOIN KDMM ON KDMM.C1='U' AND A1.C3=KDMM.C2 AND A1.C4=KDMM.C3 AND       A1.C5=KDMM.C4
LEFT JOIN KDMS ON A1.C1=KDMS.C1
LEFT JOIN KDUD ON A1.C2=KDUD.C2
LEFT JOIN KDUV ON A1.C18=KDUV.C2
where a1.c20 != 0) as aas

group by año)
select case when ano = '2013' then total end as '2013',
       case when ano = '2014' then total end as '2014',
       case when ano = '2015' then total end as '2015'
from x

这是一种方法,但将更加手动,因为您在“ano”列中有超过几个值。

 类似资料: