2015-11-07 81 views
1

我有一個子查詢結果 這是如何組合兩個Oracle查詢

Select string_Agg(amount) from(select amount from Audit_comp where auditreqid=id) 

返回300500700

,我需要使用

select sum(amount) from(select amount from Audit_comp where auditreqid=id) 

一些所有這些返回1500 但我的要求是我需要在單個單元中的以下結果

300 + 500 + 700 = 1500

請建議我

回答

1

這是你想要的嗎?

Select (list_agg(amount, '+') within group (order by amount)) || '=' || sum(amount) 
from Audit_comp 
where auditreqid = id; 

如果Oracle的版本沒有list_agg(),你可以用你的方法:

Select replace(string_Agg(amount), ',', '+') || '=' || sum(amount) 
from Audit_comp 
where auditreqid = id 
+0

不能老是list_agg()''沒有組內(按順序))' –

+0

使用@生病了。 。我不這麼認爲。 –

+0

非常感謝它的工作 –