oracle 中怎么动态group by

fengsky491 2009-09-01 10:19:31
当字段G = '0'时,group by a
当字段G = '1'时,group by b
当字段G = '2'时,group by a,b
当字段G = '3'时,group by c

用case when好像不可以,
应该怎么实现?
...全文
370 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_bear 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wildwave 的回复:]
对这个例子来说
group 后的case 需要写两个
并且由于分组的对象不确定,所有查询字段都需要聚合
例如
SQL codeselectmax(a),max(b),max(c),max(g),count(1)from testgroupbycase gwhen0then awhen1then bwhen2then awhen3then cend,case gwhen2then bend;
[/Quote]

还能这么用,学习了!
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
我也不知道,怎么说

我的目的就是对费用表中的应收人民币(cny),应收美金(usd)等金额进行分组统计,

分组统计的条件就是根据页面传递的参数,来判断根据那个字段分组

表的话,我不会建临时表,项目中的表也太复杂,不好拿出来

[Quote=引用 15 楼 inthirties 的回复:]
lz还是把你的数据给个例子吧,

这样的统计不知道有什么用的具体应用。

把数据的例子贴出来,你期望的结果也贴出来,毕竟是sql语句,除了要没有错,还要出来的数据正确。
[/Quote]
inthirties 2009-09-01
  • 打赏
  • 举报
回复
lz还是把你的数据给个例子吧,

这样的统计不知道有什么用的具体应用。

把数据的例子贴出来,你期望的结果也贴出来,毕竟是sql语句,除了要没有错,还要出来的数据正确。
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
好的,我试试
[Quote=引用 13 楼 wildwave 的回复:]
可能按几个参数分组就要写几个case
[/Quote]
小灰狼W 2009-09-01
  • 打赏
  • 举报
回复
可能按几个参数分组就要写几个case
小灰狼W 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fengsky491 的回复:]
就是有when g='2' then a,b 这样的
引用 9 楼 wildwave 的回复:

有错误的话就是你的列没聚合好
或者你case后用了when g='2' then a,b ?

[/Quote]
你看我上面写的代码group后面有什么不一样,有两个CASE
你那么写会有语法错误
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
值固定的,是页面传递过去的
[Quote=引用 8 楼 tianyazlf 的回复:]
字段 G的值在一次SQL查询中固定吗?还有Group函数是sum,count还是其他Group函数?
[/Quote]
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
就是有when g='2' then a,b 这样的
[Quote=引用 9 楼 wildwave 的回复:]

有错误的话就是你的列没聚合好
或者你case后用了when g='2' then a,b ?
[/Quote]
小灰狼W 2009-09-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fengsky491 的回复:]
这样写,不对吧,我开始就是这样用case when 写的,
引用 5 楼 wildwave 的回复:
对这个例子来说
group 后的case 需要写两个
并且由于分组的对象不确定,所有查询字段都需要聚合
例如
SQL codeselectmax(a),max(b),max(c),max(g),count(1)from testgroupbycase gwhen0then awhen1then bwhen2then awhen3then cend,case gwhen2then bend;

[/Quote]
有错误的话就是你的列没聚合好
或者你case后用了when g='2' then a,b ?
tianyazlf 2009-09-01
  • 打赏
  • 举报
回复
字段 G的值在一次SQL查询中固定吗?还有Group函数是sum,count还是其他Group函数?
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
这样写,不对吧,我开始就是这样用case when 写的,
[Quote=引用 5 楼 wildwave 的回复:]
对这个例子来说
group 后的case 需要写两个
并且由于分组的对象不确定,所有查询字段都需要聚合
例如
SQL codeselectmax(a),max(b),max(c),max(g),count(1)from testgroupbycase gwhen0then awhen1then bwhen2then awhen3then cend,case gwhen2then bend;
[/Quote]
fengsky491 2009-09-01
  • 打赏
  • 举报
回复
就像5楼说的那样,group by 要根据页面 传递的值,来确定那个分组
[Quote=引用 3 楼 inthirties 的回复:]
能说的清楚一些吗,

把你的数据和表结构

已经最后你需要的结果

一并贴出来看看。
[/Quote]
小灰狼W 2009-09-01
  • 打赏
  • 举报
回复
对这个例子来说
group 后的case 需要写两个
并且由于分组的对象不确定,所有查询字段都需要聚合
例如
select max(a),max(b),max(c),max(g),count(1)
from test
group by case g when 0 then a when 1 then b when 2 then a when 3 then c end,
case g when 2 then b end;
Warrior_hsn 2009-09-01
  • 打赏
  • 举报
回复
动态sql
inthirties 2009-09-01
  • 打赏
  • 举报
回复
能说的清楚一些吗,

把你的数据和表结构

已经最后你需要的结果

一并贴出来看看。
  • 打赏
  • 举报
回复
declare @s varchar2(4000);

@s := 'select ...';

if g = '0' then
@s := @s||' group by a';
end if;

if g = '1' then
@s := @s||' group by b';
end if;

if g = '2' then
@s := @s||' group by a,b';
end if;

if g = '3' then
@s := @s||' group by c';
end if;

open cur for @s;
chinesesword 2009-09-01
  • 打赏
  • 举报
回复
动态 sql
tianyazlf 2009-09-01
  • 打赏
  • 举报
回复
如果Group By两个栏位的话就要两个Case When。
case G when '2' then colC end
tianyazlf 2009-09-01
  • 打赏
  • 举报
回复

select sum(case G when '1' then colUSD when '2' then colCNY end) 应收数
from tablename
group by case G when '1' then colA when '2' then colB end
qin_phoenix 2009-09-01
  • 打赏
  • 举报
回复
关注
加载更多回复(2)

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧