Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将条件之和显示为一行_Sql_Oracle_Pivot - Fatal编程技术网

Sql 将条件之和显示为一行

Sql 将条件之和显示为一行,sql,oracle,pivot,Sql,Oracle,Pivot,嗨,我有一张这样的桌子 select id, value, condition from mytable 结果 我需要一个查询,让它变成这样 select id, value, condition from mytable 可能吗?只需使用条件聚合: select id, sum(case when condition = 'true' then value else 0 end) as num_true, sum(case when condition =

嗨,我有一张这样的桌子

select id, value, condition from mytable
结果

我需要一个查询,让它变成这样

select id, value, condition from mytable


可能吗?

只需使用条件聚合:

select id,
       sum(case when condition = 'true' then value else 0 end) as num_true,
       sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;

您已经用Oracle和SQL Server标记了该问题。两者都不直接支持SQL中的布尔类型,因此我猜条件是字符串。

只需使用条件聚合:

select id,
       sum(case when condition = 'true' then value else 0 end) as num_true,
       sum(case when condition = 'false' then value else 0 end) as num_false
from t
group by id;
您已经用Oracle和SQL Server标记了该问题。两者都不直接支持SQL中的布尔类型,因此我猜条件是字符串。

可能是

select id "id", sum(decode(condition,'TRUE',value,0))  "sum_of_condition_true", 
                sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
  from mytable
 group by id
 order by id;
是的,可能

select id "id", sum(decode(condition,'TRUE',value,0))  "sum_of_condition_true", 
                sum(decode(condition,'FALSE',value,0)) "sum_of_condition_false"
  from mytable
 group by id
 order by id;

这是用户提出的一个非常常见的问题,他只需要在网上做一些研究。因此它应该被关闭。@smn_onrocks:你确定它是“他”:?天哪!我为此浪费了3个小时。我想我一开始就尝试过你的解决方案。这是用户提出的一个非常常见的问题,他只需要在网上做一些研究。因此它应该被关闭。@smn_onrocks:你确定它是“他”:?天哪!我为此浪费了3个小时。我想我一开始就试过你的解决方案