oracle数据库的分组查询语句,主要是根据一个字段,使用关键字group
by来分组,如下代码:
select to_char(date_column, 'yyyy-Q'),count()
from xxx
where date_column between '01-Jan-2007' and '31-Dec-2009'
group by to_char(date_column, 'yyyy-Q')//分组查询
例如: select id,name,age,sex from talbe1 group by sex,id,name,age
--前面查询多少个字段 后面就跟多少个 这个是根据sex
假设你的表结构为tb_list(city,total),下面是SQL SERVER的写法:
select city,
cast((select sum(left(total,len(total)-1)) from tb_list where city like t1city+'%') as varchar)+'次'
from tb_list t1
group by city
select min(JifenID) as id, jifenliebie as 类别,sum(jifen) as 积分,
xingming as 姓名,jifenriqi as 日期
from table_name
group by jifenliebie ,xingming ,jifenriqi
--不知道你需要依据什么分组,如下是根据table1_id进行分组所得结果
select from (select aid as a_id,aname,atime,acontent,bid as b_id,buser from table1 a inner join table2 b on aid = btable1_ID) new_tbl where b_id in (select min(id) from table2 group by table1_ID)
没考虑效率(oracle),创建表插入数据:
create table test(年级 varchar2(2),
班级 varchar2(4),
学生编号 varchar2(6));
insert into test values ('01','0101',null);
insert into test values ('01','0101',null);
insert into test values ('01','0101',null);
insert into test values ('01','0102',null);
insert into test values ('01','0102',null);
insert into test values ('01','0103',null);
insert into test values ('02','0201',null);
insert into test values ('02','0201',null);
insert into test values ('02','0202',null);
insert into test values ('02','0202',null);
insert into test values ('02','0203',null);
insert into test values ('03','0301',null);
insert into test values ('03','0302',null);
insert into test values ('04','0401',null);
insert into test values ('04','0402',null);
commit;
执行:
update test a set a学生编号=(select brn from(select rowid,年级,班级,班级||substr('0'||row_number() over (partition by 班级 order by rowid),-2) rn from test) b
where arowid=browid);
commit;
执行后结果:
以上就是关于oracle数据库的分组查询的语句怎么写全部的内容,包括:oracle数据库的分组查询的语句怎么写、sql中多个字段怎么分组、数据库 分组问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)