想请教一个问题:一个select 语句中必须要group by a字段,于是有
select
max(b),
max(c),
max(d),
...,
a
from table
group by a现在要再加一个字段,该字段是sum一个字段,单该字段 不能 group by a,而必须group by c,那这个SQL该怎么写呢??
select
max(b),
max(c),
max(d),
...,
a
from table
group by a现在要再加一个字段,该字段是sum一个字段,单该字段 不能 group by a,而必须group by c,那这个SQL该怎么写呢??
解决方案 »
- 实现plsql developer里面的“比较表数据”的SQL语句是怎么写?
- sql高手进来看看如何用sql实现舍位平衡?(带测试数据)
- 哪位有oracle10 for win 的10.2.0.3 的补丁地址,给一个,急啊!!
- 98下可以按照oracle吗?
- 在sql语句中调用自建函数,执行时提示ORA-01722: 无效数字
- 如何在同一条数据中选日期最新的字段的值?在线急等!!!!!!
- 关于ORA-00600: 内部错误代码, 参数: [7999], [300], [], [], [], [], [], []错误
- 关于数据库远程连接的问题
- oracle 函数问题:java.sql.SQLException:ORA-01000:超出打开游标的最大数
- TOAD(蛤蟆)的一个问题
- 方式选择
- 如何查询oracle10中的datafile名称和编号,tablespace名称和编号
要么写成两个语句然后再根据条件关联。
要么单独sum 在关联2
这样
sum(字段) over(order by 1)
--对你group by a的结果集在外面嵌套一层,以分析函数c 分组,如:
select a,b,c,d....sum(字段)over(partition by c) from(
select
max(b) b,
max(c) c,
max(d) d,
...,
a
from table
group by a
)
from table
left join
(
select sum() sum_c
from table
group by c
)
on 条件
where 条件
group by a
1 3 3 3
1 8 2 2
2 7 3 2
2 10 3 11这是 你group a ,max(b) max(d) 结果
a maxb maxd
1 8 3
2 10 11那么你如果现要按照 group c求 sum(b) ,sum(d) 结果
c sum(b) sum(d)
2 8 2
3 20 16 这2个结果集 怎么也联系不到一起啊,呵呵
select a,c,max(a),sum(c) from table group by grouping sets(a,c);
1 3 3 3
1 8 2 2
2 7 3 2
2 10 3 11这是 你group a ,max(b) max(d) 结果
a maxb maxd
1 8 3
2 10 11
然后我还需要一个总计字段,sum(b),由于是总和所以不能group by a,得出的结果是:
a maxb maxd sum(b)
1 8 3 28
2 10 11 28虽然逻辑上有点说不过去,但是我只是想知道有没有这种写法??个人见解:
这种情况可能用个union可以解决:a,max(b) b,max(c)c ''d group by a
union
'' a, ''b, ''c sum(b) d
但是要union的字段如果多了岂不是很麻烦,所以有以上这个问题