是否可以按参数分组。我有这样的查询:
select :P1, sum(prod), sum(kol), sum(ps)
from promet_3b
group by :P1 ----P1 is parameter
我想按此参数分组。1,2或更多列。如何做到这一点?
从语法上讲,您使用的查询可以,但是并不能达到您的预期目的。
假设您传递的值为“ COLX”,则查询将如下执行
select 'COLX' as P1, sum(prod), sum(kol), sum(ps)
from promet_3b
group by 'COLX'
这将返回一行并完全点燃COLX。
这是一个简单的示例,实现了一个逻辑:
如果您在A列上通过了1组如果您在B列上通过了2组
确保列的类型相同
with dt as (
select 'x1' A, 'y' B, 1 cnt from dual union all
select 'x2' A, 'y' B, 1 cnt from dual)
select
decode(:i,1,A,2,B) grp_col, sum(cnt) from dt
group by decode(:i,1,A,2,B);
传回1
GRP_COL SUM(CNT)
------- ----------
x1 1
x2 1
传递2次回报
GRP_COL SUM(CNT)
------- ----------
y 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句