乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

      postgreSQL中的row_number() 與distinct用法說明

       2021-05-28 16:37  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了postgreSQL中的row_number() 與distinct用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧。

      我就廢話不多說了,大家還是直接看代碼吧~

      select count(s.*)
      from (
       select *, row_number() over (partition by fee_date order by fee_date) as gr
       from new_order where news_id='novel' and order_status='2'
      ) s
      where s.gr = 1
      SELECT count(DISTINCT fee_date) as dis from new_order where news_id='novel' and order_status='2'

       

      這兩個(gè)SQL執(zhí)行所得到的數(shù)據(jù)是一樣的!

      工具:postgreSQL

      1.我們要清楚,sql的執(zhí)行順序:

      from語句->where語句->group by語句->having語句->order by語句->select 語句

      2.row_number()分析函數(shù)

      說明:返回結(jié)果集分區(qū)內(nèi)行的序列號(hào),每個(gè)分區(qū)的第一行從 1 開始。

      語法:ROW_NUMBER () OVER ([ <partition_by_clause>]<order_by_clause> )

      備注:ORDERBY 子句可確定在特定分區(qū)中為行分配唯一 ROW_NUMBER 的順序。

      參數(shù):<partition_by_clause> :將FROM 子句生成的結(jié)果集劃入應(yīng)用了 ROW_NUMBER 函數(shù)的分區(qū)。

      <order_by_clause>:確定將 ROW_NUMBER 值分配給分區(qū)中行的順序。

      返回類型:bigint 。

      row_number()從1開始,為每一條分組記錄返回一個(gè)數(shù)字

      1select *, row_number() over (order by fee_date) from new_order

      先把 fee_date 升序排列,再為升序以后的每條記錄返回一個(gè)序號(hào)

      1select *, row_number() over (partition by fee_date order by fee_date) as gr from new_order

      表示根據(jù)fee_date分組,在分組內(nèi)部根據(jù) fee_date排序,而此函數(shù)計(jì)算的值就表示每組內(nèi)部排序后的順序編號(hào)(組內(nèi)連續(xù)的唯一的)

      2.distinct

      語法:

      1SELECT DISTINCT 列名稱 FROM 表名稱

      distinct這個(gè)關(guān)鍵字用來過濾掉多余的重復(fù)記錄只保留一條

      1select DISTINCT fee_date from new_order

      1select DISTINCT fee_date,order_status from new_order

      從結(jié)果可以看出,是根據(jù)“fee_date+order_status”來去重復(fù)數(shù)據(jù)的,distinct同時(shí)作用在了fee_date和order_status上

      1SELECT count(DISTINCT fee_date) as dis from new_order where news_id='novel' and order_status='2'

      1select id,distinct fee_date from new_order ; –會(huì)提示錯(cuò)誤,因?yàn)閐istinct必須放在開頭

      distinct語句中select顯示的字段只能是distinct指定的字段,其他字段是不可能出現(xiàn)的

      補(bǔ)充:PostgreSQL ROW_NUMBER() OVER()

      我就廢話不多說了,大家還是直接看代碼吧~

      SELECT
       *
      FROM
       (
        SELECT
         tt.s_ci s_ci,
         sm.ci,
      --    getdistance (
      --     tt.longitude,
      --     tt.latitude,
      --     sm.longitude,
      --     sm.latitude
      --    ) distance,
         ROW_NUMBER () OVER (
          PARTITION BY tt.s_ci
          ORDER BY
           getdistance (
            tt.longitude,
            tt.latitude,
            sm.longitude,
            sm.latitude
           )
         ) rn
        FROM
         sm_cl_location sm
        INNER JOIN (
         SELECT
          s_ci,
          longitude,
          latitude,
          n3_pci,
          n3_earfcn
         FROM
          plan_ott_data
         WHERE
          1 = 1
         AND (
          s_ci = '460-00-1012286-2'
          OR s_ci = '460-00-25514-130'
         )
         AND rpt_time BETWEEN '2018-04-30'
         AND '2018-05-29'
        ) tt ON sm.pci = tt.n3_pci
        AND sm.hannel_number = tt.n3_earfcn
       ) T
      WHERE
       T .rn BETWEEN 1 and 3

       

      語法:

      1ROW_NUMBER() OVER( [ PRITITION BY col1] ORDER BY col2[ DESC ] )

      解釋:

      ROW_NUMBER()為返回的記錄定義個(gè)行編號(hào), PARTITION BY col1 是根據(jù)col1分組,ORDER BY col2[ DESC ]是根據(jù)col2進(jìn)行排序。

      舉例:

      postgres=# create table student(id serial,name character varying,course character varying,score integer);
      CREATE TABLE
      postgres=#
      postgres=# \d student
              Table "public.student"
       Column |  Type  |      Modifiers     
      --------+-------------------+----------------------------------------------
       id  | integer   | not null default nextval('student_id_seq'::regclass)
       name | character varying |
       course | character varying |
       score | integer   |

       

       insert into student (name,course,score) values('周潤發(fā)','語文',89);
       insert into student (name,course,score) values('周潤發(fā)','數(shù)學(xué)',99);
       insert into student (name,course,score) values('周潤發(fā)','外語',67);
       insert into student (name,course,score) values('周潤發(fā)','物理',77);
       insert into student (name,course,score) values('周潤發(fā)','化學(xué)',87);
       insert into student (name,course,score) values('周星馳','語文',91);
       insert into student (name,course,score) values('周星馳','數(shù)學(xué)',81);
       insert into student (name,course,score) values('周星馳','外語',88);
       insert into student (name,course,score) values('周星馳','物理',68);
       insert into student (name,course,score) values('周星馳','化學(xué)',83);
       insert into student (name,course,score) values('黎明','語文',85);
       insert into student (name,course,score) values('黎明','數(shù)學(xué)',65);
       insert into student (name,course,score) values('黎明','外語',95);
       insert into student (name,course,score) values('黎明','物理',90);
       insert into student (name,course,score) values('黎明','化學(xué)',78);

       

      1. 根據(jù)分?jǐn)?shù)排序

      postgres=# select *,row_number() over(order by score desc)rn from student;
       id | name | course | score | rn
      ----+--------+--------+-------+----
       2 | 周潤發(fā) | 數(shù)學(xué) | 99 | 1
       13 | 黎明 | 外語 | 95 | 2
       6 | 周星馳 | 語文 | 91 | 3
       14 | 黎明 | 物理 | 90 | 4
       1 | 周潤發(fā) | 語文 | 89 | 5
       8 | 周星馳 | 外語 | 88 | 6
       5 | 周潤發(fā) | 化學(xué) | 87 | 7
       11 | 黎明 | 語文 | 85 | 8
       10 | 周星馳 | 化學(xué) | 83 | 9
       7 | 周星馳 | 數(shù)學(xué) | 81 | 10
       15 | 黎明 | 化學(xué) | 78 | 11
       4 | 周潤發(fā) | 物理 | 77 | 12
       9 | 周星馳 | 物理 | 68 | 13
       3 | 周潤發(fā) | 外語 | 67 | 14
       12 | 黎明 | 數(shù)學(xué) | 65 | 15
      (15 rows)

       

      rn是給我們的一個(gè)排序。

      2. 根據(jù)科目分組,按分?jǐn)?shù)排序

      postgres=# select *,row_number() over(partition by course order by score desc)rn from student;
       id | name | course | score | rn
      ----+--------+--------+-------+----
       5 | 周潤發(fā) | 化學(xué) | 87 | 1
       10 | 周星馳 | 化學(xué) | 83 | 2
       15 | 黎明 | 化學(xué) | 78 | 3
       13 | 黎明 | 外語 | 95 | 1
       8 | 周星馳 | 外語 | 88 | 2
       3 | 周潤發(fā) | 外語 | 67 | 3
       2 | 周潤發(fā) | 數(shù)學(xué) | 99 | 1
       7 | 周星馳 | 數(shù)學(xué) | 81 | 2
       12 | 黎明 | 數(shù)學(xué) | 65 | 3
       14 | 黎明 | 物理 | 90 | 1
       4 | 周潤發(fā) | 物理 | 77 | 2
       9 | 周星馳 | 物理 | 68 | 3
       6 | 周星馳 | 語文 | 91 | 1
       1 | 周潤發(fā) | 語文 | 89 | 2
       11 | 黎明 | 語文 | 85 | 3
      (15 rows)

       

      3. 獲取每個(gè)科目的最高分

      postgres=# select * from(select *,row_number() over(partition by course order by score desc)rn from student)t where rn=1;
       id | name | course | score | rn
      ----+--------+--------+-------+----
       5 | 周潤發(fā) | 化學(xué) | 87 | 1
       13 | 黎明 | 外語 | 95 | 1
       2 | 周潤發(fā) | 數(shù)學(xué) | 99 | 1
       14 | 黎明 | 物理 | 90 | 1
       6 | 周星馳 | 語文 | 91 | 1
      (5 rows)

       

      4. 每個(gè)科目的最低分也是一樣的

      postgres=# select * from(select *,row_number() over(partition by course order by score)rn from student)t where rn=1;
       id | name | course | score | rn
      ----+--------+--------+-------+----
       15 | 黎明 | 化學(xué) | 78 | 1
       3 | 周潤發(fā) | 外語 | 67 | 1
       12 | 黎明 | 數(shù)學(xué) | 65 | 1
       9 | 周星馳 | 物理 | 68 | 1
       11 | 黎明 | 語文 | 85 | 1
      (5 rows)

      只要在根據(jù)科目排序的時(shí)候按低到高順序排列就好了。

      文章來源:腳本之家

      來源地址:https://www.jb51.net/article/204797.htm

      申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)文章

      熱門排行

      信息推薦