ORACLE分組查詢和統計等

select flow_id,rw from (select t.flow_id ,rownum as rw from apex_030200.wwv_flow_list_templates t)  where rw >= 5


1.rownum只能用<若是使用>加別名

2.子查詢引用只能在查詢出的結果中引用,好比子查詢沒有查出flow_id,外層不能用,另外外層不能引用內層的t

3.薪水前三名,內層查出薪水 order desc的虛表外層使用rownum<3

4.merge能夠實現存在數據就update不存在你就insert

merge into product a

using (select  1717 product_id, '002' req_no from dual b)

on (a.product_id = b.product.id and a.req_no = b.req.no)

when matched then

update set  product name = ''.....................

when not matched then

insert () values ()

5.start with connect by 能夠查詢出一組樹的數據,注意最後connect by的條件(父節點=子節點 向上查詢 反之向下查詢)

能夠order排序,能夠加入兩棵樹(or),也能夠加入where條件

select * from emp

where......

start with empnc = 7369 or empnc = 7204(注意不能用and )

connect by prior mgr = empno

order by ...

6 份額(查詢某數據佔有總數據的百分比)

select t.empno,t.ename,t.sal,
100*round(sal/sum(sal) over(),5)
 from emp t

7 連續求和(同名分爲同組 累加)

select t.empno,t.ename,t.sal,
sum(sal) over(order by sal)
 from emp t

8.帶條件的連續求和(分部門連續求和)

select t.empno,t.ename,t.sal,t.deptno,
sum(sal) over(partition by t.deptno order by sal)
 from emp t

9.分部門總和(取出orderby)

select t.empno,t.ename,t.sal,t.deptno,
sum(sal) over(partition by t.deptno)
 from emp t

10工資的分組查詢份額(總數的百分比)帶上部門分組

select t.empno,t.ename,t.sal,t.deptno,
100*round(sal/sum(sal) over (partition by t.deptno),4)
 from emp t

注意這裏查詢的是「」分組「,所以這裏查詢的是變成一組爲一個100%,查詢的是一個部門中員工在本部分所佔用的薪水比例

11分組查詢出單一條件並分級(查詢某一個部門的薪水的級別)注意rank()和row_number()的區別 rank是跳躍性並列(1.1.3.3.5) row_number(1.2.3.4.5)

select t.*,ROW_NUMBER() over(partition by t.deptno order by t.sal desc) rank from emp t

12「總」。。。。。。這個字眼通常使用group by(區分於over(partition by order by))

按部門分組查詢部門的總薪水

select sum(t.sal),t.deptno from emp t group by t.deptno

13 總的基礎上再次分組 group by + rollup

select sum(t.sal),t.deptno from emp t group by rollup (t.deptno)彙總後將總和進行求和

select sum(t.sal),t.job,t.deptno from emp t group by rollup (t.deptno,t.job)注意多個rollup其實只有第一個參數有效

14cube 鏈接在order by後面(代替ROLLUP) rollup升級版 所有分組

15grouping實現不用java代碼就能夠對oracle 查詢出的null字段進行賦值 0 自己結果 1合計結果

select sum(t.sal),t.deptno,
(case
       when((grouping(t.job)=1 and grouping(t.deptno)=0)) then '部門小計'
       when((grouping(t.job)=1 and grouping(t.deptno)=1)) then '部門總計'
else t.job end) as
job from emp t group by rollup (t.deptno,t.job)

16分組後的字段累加(好比按照員工名稱,根據月份分組,實現自1月份到12月份工資累加,即二月份是 1月 + 2月 。。)
select t.empno, t.ename, t.sal, sum(sal) over (partition by t.ename order by t.sal desc) from emp t

17分組最高值 最低值 平均值 使用 max() over(partition by order by)代替sum() 還能夠用min() avg()

java

18select * from v$transaction 查看事務oracle

19多層分組函數和子查詢之間的衝突問題函數

 

select a.CREATOR, a.count_Sum, b.full_name, b.dept_code, b.area_code
  from (select temp.c CREATOR, count(temp.c) as count_Sum
          from (select t.ca,
                       c.lv,
                       instr(t.ca, ',', 1, c.lv) + 1,
                       substr(t.ca,
                              instr(t.ca, ',', 1, c.lv) + 1,
                              instr(t.ca, ',', 1, c.lv + 1) -
                              (instr(t.ca, ',', 1, c.lv) + 1)) AS c
                  from (select ',' || checker || ',' AS ca,
                               checker,
                               LENGTH(checker),
                               length(checker || ','),
                               REPLACE(checker, ','),
                               length(REPLACE(checker, ',')),
                               nvl(length(REPLACE(checker, ',')), 0),
                               length(checker || ',') -
                               nvl(length(REPLACE(checker, ',')), 0) AS cnt
                          FROM wm_time_info a
                         where a.check_result != 1) t,
                          (select LEVEL lv from dual CONNECT BY LEVEL <= 100) c
                 where c.lv <= t.cnt) temp
         group by temp.c) a,
       base_user b
 where a.CREATOR = b.user_code

  

  外層查詢和內層分組衝突code

--select   a.CREATOR, a.count_Sum ,b.full_name,b.dept_code,b.area_code from (    
select o.CREATOR,
       count(o.CREATOR) as count_Sum,
       FULLNAME,
       DEPTCODE,
       AREACODE
  from (select temp.c      CREATOR,
               b.full_name FULLNAME,
               b.dept_code DEPTCODE,
               b.area_code AREACODE,
               work_date
          from (select work_date,
                       t.ca,
                       c.lv,
                       instr(t.ca, ',', 1, c.lv) + 1,
                       substr(t.ca,
                              instr(t.ca, ',', 1, c.lv) + 1,
                              instr(t.ca, ',', 1, c.lv + 1) -
                              (instr(t.ca, ',', 1, c.lv) + 1)) AS c
                  from (
                        ---  
                        select work_date,
                                ',' || checker || ',' AS ca,
                                checker,
                                LENGTH(checker),
                                length(checker || ','),
                                REPLACE(checker, ','),
                                length(REPLACE(checker, ',')),
                                nvl(length(REPLACE(checker, ',')), 0),
                                length(checker || ',') -
                                nvl(length(REPLACE(checker, ',')), 0) AS cnt
                          FROM wm_time_info a
                         where a.check_result != 1
                        ---         
                        ) t,
                          (select LEVEL lv from dual CONNECT BY LEVEL <= 100) c
                 where c.lv <= t.cnt) temp,
               base_user b
         where temp.c = b.user_code) o
--where work_date >='2016-01-10'   
 group by o.CREATOR, FULLNAME, DEPTCODE, AREACODE
--) a ,base_user b where a.CREATOR = b.user_code

  

  20 注意 本條select中的分組和子查詢都不能夠做爲函數的參數傳入blog

相關文章
相關標籤/搜索