PL SQL查询一个变量并拆分为两列
PLSQL Querying one variable and spliting into two column
所以我有一个名为 Table1 的 table,有两列,Product 和 indicator。
Table1
Product Indicator
Product 1 Y
Product 1 Y
Product 1 Y
Product 1 N
Product 1 N
Product 2 Y
Product 2 Y
Product 2 Y
Product 2 Y
Product 2 Y
我希望能够 运行 查询来显示这样的结果
Indicator = Y Indicator = N
Product 1 Y Y
Product 2 Y N
提前致谢! :)
不需要 PL/SQL,这可以通过简单的 SQL,使用条件聚合来完成。
select product,
case
when count(case when indicator = 'Y' then 1 end) > 0 then 'Y'
else 'N'
end as "Indicator = Y",
case
when count(case when indicator = 'N' then 1 end) > 0 then 'Y'
else 'N'
end as "Indicator = N"
from table1
group by product
order by product;
所以我有一个名为 Table1 的 table,有两列,Product 和 indicator。
Table1
Product Indicator
Product 1 Y
Product 1 Y
Product 1 Y
Product 1 N
Product 1 N
Product 2 Y
Product 2 Y
Product 2 Y
Product 2 Y
Product 2 Y
我希望能够 运行 查询来显示这样的结果
Indicator = Y Indicator = N
Product 1 Y Y
Product 2 Y N
提前致谢! :)
不需要 PL/SQL,这可以通过简单的 SQL,使用条件聚合来完成。
select product,
case
when count(case when indicator = 'Y' then 1 end) > 0 then 'Y'
else 'N'
end as "Indicator = Y",
case
when count(case when indicator = 'N' then 1 end) > 0 then 'Y'
else 'N'
end as "Indicator = N"
from table1
group by product
order by product;