久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

        <small id='gqWxG'></small><noframes id='gqWxG'>

        <tfoot id='gqWxG'></tfoot>
      1. <legend id='gqWxG'><style id='gqWxG'><dir id='gqWxG'><q id='gqWxG'></q></dir></style></legend>
          <bdo id='gqWxG'></bdo><ul id='gqWxG'></ul>
      2. <i id='gqWxG'><tr id='gqWxG'><dt id='gqWxG'><q id='gqWxG'><span id='gqWxG'><b id='gqWxG'><form id='gqWxG'><ins id='gqWxG'></ins><ul id='gqWxG'></ul><sub id='gqWxG'></sub></form><legend id='gqWxG'></legend><bdo id='gqWxG'><pre id='gqWxG'><center id='gqWxG'></center></pre></bdo></b><th id='gqWxG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='gqWxG'><tfoot id='gqWxG'></tfoot><dl id='gqWxG'><fieldset id='gqWxG'></fieldset></dl></div>
      3. 獲取每組分組結果的前 n 條記錄

        Get top n records for each group of grouped results(獲取每組分組結果的前 n 條記錄)

            <bdo id='IHlPF'></bdo><ul id='IHlPF'></ul>
              <tbody id='IHlPF'></tbody>
            <tfoot id='IHlPF'></tfoot>

            1. <legend id='IHlPF'><style id='IHlPF'><dir id='IHlPF'><q id='IHlPF'></q></dir></style></legend>

              <small id='IHlPF'></small><noframes id='IHlPF'>

              1. <i id='IHlPF'><tr id='IHlPF'><dt id='IHlPF'><q id='IHlPF'><span id='IHlPF'><b id='IHlPF'><form id='IHlPF'><ins id='IHlPF'></ins><ul id='IHlPF'></ul><sub id='IHlPF'></sub></form><legend id='IHlPF'></legend><bdo id='IHlPF'><pre id='IHlPF'><center id='IHlPF'></center></pre></bdo></b><th id='IHlPF'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='IHlPF'><tfoot id='IHlPF'></tfoot><dl id='IHlPF'><fieldset id='IHlPF'></fieldset></dl></div>
                • 本文介紹了獲取每組分組結果的前 n 條記錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  以下是最簡單的示例,但任何解決方案都應該能夠擴展到需要多少 n 個頂級結果:

                  給定如下表,其中包含人、組和年齡列,您將如何獲得每個組中最年長的 2 個人?(組內的關系不應產生更多結果,而是給出按字母順序排列的前 2 個)

                  <前>+--------+-------+-----+|人 |集團 |年齡 |+--------+-------+-----+|鮑勃 |1 |32 ||吉爾 |1 |34 ||肖恩 |1 |42 ||杰克 |2 |29 ||保羅 |2 |36 ||勞拉 |2 |39 |+--------+-------+-----+

                  期望的結果集:

                  <前>+--------+-------+-----+|肖恩 |1 |42 ||吉爾 |1 |34 ||勞拉 |2 |39 ||保羅 |2 |36 |+--------+-------+-----+

                  <小時>

                  注意:這個問題建立在上一個問題的基礎上 - 獲取每組分組的最大值的記錄SQL 結果 - 用于從每個組中獲得一個頂行,并且從 @Bohemian 那里收到了一個很好的 MySQL 特定答案:

                  選擇 *from (select * from mytable order by `Group`, Age desc, Person) x按組"分組

                  希望能夠以此為基礎,盡管我不知道如何.

                  解決方案

                  這是一種方法,使用 UNION ALL(參見 SQL Fiddle with Demo).這適用于兩個組,如果您有兩個以上的組,則需要指定 group 編號并為每個 group 添加查詢:

                  <代碼>(選擇 *來自 mytable其中`組`= 1按年齡排序限制 2)聯合所有(選擇 *來自 mytable其中`組`= 2按年齡排序限制 2)

                  有多種方法可以做到這一點,請參閱本文以確定適合您情況的最佳路線:

                  http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

                  這也可能對您有用,它為每條記錄生成一個行號.使用上面鏈接中的示例,這將僅返回行數小于或等于 2 的那些記錄:

                  選擇人物,`group`,年齡從(選擇人、組"、年齡、(@num:=if(@group = `group`, @num +1, if(@group := `group`, 1, 1))) row_number從測試 t交叉連接(選擇@num:=0,@group:=null)c按組"、年齡、人排序) 作為 x其中 x.row_number <= 2;

                  參見演示

                  The following is the simplest possible example, though any solution should be able to scale to however many n top results are needed:

                  Given a table like that below, with person, group, and age columns, how would you get the 2 oldest people in each group? (Ties within groups should not yield more results, but give the first 2 in alphabetical order)

                  +--------+-------+-----+
                  | Person | Group | Age |
                  +--------+-------+-----+
                  | Bob    | 1     | 32  |
                  | Jill   | 1     | 34  |
                  | Shawn  | 1     | 42  |
                  | Jake   | 2     | 29  |
                  | Paul   | 2     | 36  |
                  | Laura  | 2     | 39  |
                  +--------+-------+-----+
                  

                  Desired result set:

                  +--------+-------+-----+
                  | Shawn  | 1     | 42  |
                  | Jill   | 1     | 34  |
                  | Laura  | 2     | 39  |
                  | Paul   | 2     | 36  |
                  +--------+-------+-----+
                  


                  NOTE: This question builds on a previous one- Get records with max value for each group of grouped SQL results - for getting a single top row from each group, and which received a great MySQL-specific answer from @Bohemian:

                  select * 
                  from (select * from mytable order by `Group`, Age desc, Person) x
                  group by `Group`
                  

                  Would love to be able to build off this, though I don't see how.

                  解決方案

                  Here is one way to do this, using UNION ALL (See SQL Fiddle with Demo). This works with two groups, if you have more than two groups, then you would need to specify the group number and add queries for each group:

                  (
                    select *
                    from mytable 
                    where `group` = 1
                    order by age desc
                    LIMIT 2
                  )
                  UNION ALL
                  (
                    select *
                    from mytable 
                    where `group` = 2
                    order by age desc
                    LIMIT 2
                  )
                  

                  There are a variety of ways to do this, see this article to determine the best route for your situation:

                  http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

                  Edit:

                  This might work for you too, it generates a row number for each record. Using an example from the link above this will return only those records with a row number of less than or equal to 2:

                  select person, `group`, age
                  from 
                  (
                     select person, `group`, age,
                        (@num:=if(@group = `group`, @num +1, if(@group := `group`, 1, 1))) row_number 
                    from test t
                    CROSS JOIN (select @num:=0, @group:=null) c
                    order by `Group`, Age desc, person
                  ) as x 
                  where x.row_number <= 2;
                  

                  See Demo

                  這篇關于獲取每組分組結果的前 n 條記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
                    <tbody id='LUbNA'></tbody>

                    • <bdo id='LUbNA'></bdo><ul id='LUbNA'></ul>
                      1. <small id='LUbNA'></small><noframes id='LUbNA'>

                        <tfoot id='LUbNA'></tfoot>

                          <legend id='LUbNA'><style id='LUbNA'><dir id='LUbNA'><q id='LUbNA'></q></dir></style></legend>
                          • <i id='LUbNA'><tr id='LUbNA'><dt id='LUbNA'><q id='LUbNA'><span id='LUbNA'><b id='LUbNA'><form id='LUbNA'><ins id='LUbNA'></ins><ul id='LUbNA'></ul><sub id='LUbNA'></sub></form><legend id='LUbNA'></legend><bdo id='LUbNA'><pre id='LUbNA'><center id='LUbNA'></center></pre></bdo></b><th id='LUbNA'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='LUbNA'><tfoot id='LUbNA'></tfoot><dl id='LUbNA'><fieldset id='LUbNA'></fieldset></dl></div>
                            主站蜘蛛池模板: 日韩在线精品视频 | 波多野结衣一区二区三区 | 日韩成人在线观看 | 91黄在线观看 | 成人av一区 | 中文字幕精品一区二区三区在线 | 东方伊人免费在线观看 | 天天操夜夜操 | 久久亚洲国产精品 | 91免费看片 | 狠狠夜夜 | 久久www免费人成看片高清 | 精品免费视频一区二区 | 亚洲国产精品久久人人爱 | 日本一区二区三区在线观看 | 欧美一级二级视频 | 久久国产精品免费一区二区三区 | 成人一区二区三区在线观看 | 免费观看一级毛片 | 日韩成人精品一区 | 日韩精品成人 | 青青草一区二区 | 黄色日批视频 | 黄片毛片免费观看 | 四虎在线观看 | 毛片一区二区 | 欧美高清视频 | 精品一区二区三区在线观看 | 欧美区精品 | 国产欧美精品一区二区色综合朱莉 | 久久成人一区二区三区 | 久久大陆 | 怡红院怡春院一级毛片 | 国产 欧美 日韩 一区 | av电影手机在线看 | 欧美99 | 91在线第一页 | 成人在线h | 久久久九九 | 免费在线看黄 | 国产精品爱久久久久久久 |