要求查询每个用户对应的最大样品信息,忽然想到ms sql提供过 row_number() over(partition by 列 order by 列 desc),那么oracle可能也存在,
我的表结构如下:
create table NEOGOODSRULE ( ID NUMBER(22) not null, PERSONALID NVARCHAR2(50), CT_SMP_TYPE NVARCHAR2(100) ) tablespace VGSM pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );
数据如下:
insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2270, ‘JYZ‘, ‘原料‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2271, ‘JYZ‘, ‘辅料‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2359, ‘SYSTEM‘, ‘包材(内)‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2360, ‘SYSTEM‘, ‘包材(外)‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2361, ‘SYSTEM‘, ‘原料‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2362, ‘SYSTEM‘, ‘成品‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2363, ‘SYSTEM‘, ‘稳定性(加速)‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2364, ‘SYSTEM‘, ‘稳定性(长期)‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2365, ‘SYSTEM‘, ‘辅料‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2354, ‘LY‘, ‘成品‘); insert into neogoodsrule (ID, PERSONALID, CT_SMP_TYPE) values (2355, ‘LY‘, ‘原料‘);
ROW_NUMBER()语法如下:
1、row_number() over(order by column asc)先对列column按照升序,再为每条记录返回一个序列号:
select personalid,row_number() over(order by personalid asc) rn from neogoodsrule
2、row_number() over(partition by column1 order by column2 asc) 先按照column1分组,再对分组后的数据进行以column2升序排列
select personalid,ct_smp_type,row_number() over(partition by personalid order by ct_smp_type asc) rn from neogoodsrule
由此,开始所提的需求sql代码如下
select * from (select personalid,ct_smp_type,row_number() over(partition by personalid order by ct_smp_type asc) rn from neogoodsrule )
where rn=1
使用row_number()分页函数取代group by。例如:语句1:
select col1,col2 from t where col1 in (select col1 from t group by col1 having count(*) <=2 ) order
by order col2;
语句1可以用下面的语句取代:
语句2:
select col1,col2 from (select *,row_number() over(partition by col1 order by col2) as p_group from
t) where p_group<=2 ;
ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2)
表示根据COL1分组,在分组内部根据 COL2排序。而这个值就表示每组内部排序后的顺序编号(组内连续的唯一的
)。
----------------------------------------------------------------------------------
select a,b,c,sum(c)over(partition by a) from t2
得到结果:
A B C SUM(C)OVER(PARTITIONBYA)
-- -- ------- ------------------------
h b 3 3
m a 2 4
m a 2 4
n a 3 6
n b 2 6
n b 1 6
x b 3 9
x b 2 9
x b 4 9
如果用sum,group by 则只能得到
A SUM(C)
-- ----------------------
h 3
m 4
n 6
x 9
无法得到B列值
---------------------------------------------------------------------------------------------
RANK() OVER (PARTITION BY COL1 ORDER BY COL2)
类似,不过RANK 排序的时候跟派名次一样,可以并列2个第一名之后 是第3名。
dense_rank() OVER (PARTITION BY COL1 ORDER BY COL2)
类似。是连续排序,有两个第二名时仍然跟着第三名。
dept sal emp row_number rank dense_rank
---- ------- ----- ---------- ----- ----------
10 5000.00 7839 1 1 1
10 2450.00 7782 2 2 2
10 1300.00 7934 3 3 3
20 3000.00 7788 1 1 1
20 3000.00 7902 2 1 1
20 2975.00 7566 3 3 2
20 1100.00 7876 4 4 3
20 800.00 7369 5 5 4
30 2850.00 7698 1 1 1
30 1600.00 7499 2 2 2
在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果