我希望在参数表中将每个参数的总和作为列添加 表结构有两列ParamaterId和ResultValue 表的值是
ParameterId ResultValue
2D/Ao Root Diam 0.03154840000000
AVA (VTI) 0.00016937690000
AVA (VTI) 0.00016937690000
2D/Ao Root Diam 0.03473202000000
AVA (VTI) 0.00006636187000
AVA (VTI) 0.00006755497000
我想将ParameterId显示为列标题和ResultValue的总和
AVA (VTI) 2D/Ao Root Diam
Sum(ResultValue) Sum(ResultValue)
下面的查询生成正确的resutl但结果不是单行,我需要输出应该是单行
AVA (VTI) 2D/Ao Root Diam
.573844582491253138 0
0 18.5821659893339723
select
sum (case when ParameterId = 'AVA (VTI)' then (ResultValue) else 0 end) as 'AVA (VTI)',
sum(case when ParameterId = '2D/Ao Root Diam' then (ResultValue) else 0 end) as '2D/Ao Root Diam'
from DBCREATORS.Parameter as P
where ParameterId
in ('AVA (VTI)', '2D/Ao Root Diam')
group by ParameterId
期望的结果应该是
AVA (VTI) 2D/Ao Root Diam
.573844582491253138 18.5821659893339723
请帮忙
谢谢,
答案 0 :(得分:0)
另一种可能性是使用子查询。
select
(
select sum (ResultValue)
from DBCREATORS.Parameter as P
where ParameterId = 'AVA (VTI)'
) as 'AVA (VTI)',
(
select sum (ResultValue)
from DBCREATORS.Parameter as P
where ParameterId = '2D/Ao Root Diam'
) as '2D/Ao Root Diam'
这应该是比我之前的建议更好的解决方案。
答案 1 :(得分:0)
通过以下方式取出小组:
select
sum (case when ParameterId = 'AVA (VTI)' then (ResultValue) else 0 end) as 'AVA (VTI)',
sum(case when ParameterId = '2D/Ao Root Diam' then (ResultValue) else 0 end) as '2D/Ao Root Diam'
from DBCREATORS.Parameter as P
where ParameterId
in ('AVA (VTI)', '2D/Ao Root Diam')
没有SQL Server的组将所有内容组合成一行。您的when子句将使您的值不会发生变化。
您也可以查看pivot运算符。