在列oracle SQL中显示某些值

时间:2016-12-23 14:07:34

标签: oracle-sqldeveloper

请在下面找到示例数据和场景

with data_1 as (
select 'Book' as object,'Long-Ruled' as Type,20 as Amount from dual
union
select 'Book' as object,'Short-Ruled' as Type,30 as Amount from dual
union
select 'Book' as object,'All Type' as Type,5 as Amount from dual
union
select 'Pen' as object,'Fountain' as Type,55 as Amount from dual
union
select 'Pen' as object,'All Type' as Type,100 as Amount from dual
)

我希望我的输出与附加图像一样

enter image description here

Object  Type        Amount  Certain_Amount
Book    Long-Ruled  20        5
Book    Short-Ruled 30        5
Book    All Type    5         5
Pen     Fountain    55       100
Pen    All Type    100       100

我想添加新列"某些金额"这表明"所有类型"金额。

我尝试了以下查询。

select object,type,amount,sum(sub) over (partition by object) as certain_amount from
(
select object,type,amount,case when type not like 'All Type' then 0 else amount end as sub from data_1
)

我可以从上面的查询中获得所需的输出。这是一种简单的方法来进行单一查询。谢谢

1 个答案:

答案 0 :(得分:0)

在发布问题后我得到了答案

选择对象,类型,数量,总和(类型不是'全部类型'然后0其他数量结束时的情况)(从对象分区)作为来自data_1的子