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

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

        <tfoot id='R03YG'></tfoot>
        <legend id='R03YG'><style id='R03YG'><dir id='R03YG'><q id='R03YG'></q></dir></style></legend>
        <i id='R03YG'><tr id='R03YG'><dt id='R03YG'><q id='R03YG'><span id='R03YG'><b id='R03YG'><form id='R03YG'><ins id='R03YG'></ins><ul id='R03YG'></ul><sub id='R03YG'></sub></form><legend id='R03YG'></legend><bdo id='R03YG'><pre id='R03YG'><center id='R03YG'></center></pre></bdo></b><th id='R03YG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='R03YG'><tfoot id='R03YG'></tfoot><dl id='R03YG'><fieldset id='R03YG'></fieldset></dl></div>

          <bdo id='R03YG'></bdo><ul id='R03YG'></ul>

        SQL 僅選擇列上具有最大值的行

        SQL select only rows with max value on a column(SQL 僅選擇列上具有最大值的行)
          <bdo id='5t7HE'></bdo><ul id='5t7HE'></ul>

          <small id='5t7HE'></small><noframes id='5t7HE'>

              <tbody id='5t7HE'></tbody>

                  <legend id='5t7HE'><style id='5t7HE'><dir id='5t7HE'><q id='5t7HE'></q></dir></style></legend>

                  <tfoot id='5t7HE'></tfoot>

                  <i id='5t7HE'><tr id='5t7HE'><dt id='5t7HE'><q id='5t7HE'><span id='5t7HE'><b id='5t7HE'><form id='5t7HE'><ins id='5t7HE'></ins><ul id='5t7HE'></ul><sub id='5t7HE'></sub></form><legend id='5t7HE'></legend><bdo id='5t7HE'><pre id='5t7HE'><center id='5t7HE'></center></pre></bdo></b><th id='5t7HE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='5t7HE'><tfoot id='5t7HE'></tfoot><dl id='5t7HE'><fieldset id='5t7HE'></fieldset></dl></div>
                  本文介紹了SQL 僅選擇列上具有最大值的行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我有這個文件表(這里是簡化版):

                  I have this table for documents (simplified version here):

                  <頭>
                  id內容
                  11...
                  21...
                  12...
                  13...

                  如何為每個 ID 選擇一行并且只選擇最大的轉速?
                  有了上面的數據,結果應該包含兩行:[1, 3, ...][2, 1, ..].我正在使用 MySQL.

                  How do I select one row per id and only the greatest rev?
                  With the above data, the result should contain two rows: [1, 3, ...] and [2, 1, ..]. I'm using MySQL.

                  目前我在 while 循環中使用檢查來檢測和覆蓋結果集中的舊版本.但這是達到結果的唯一方法嗎?沒有SQL解決方案嗎?

                  Currently I use checks in the while loop to detect and over-write old revs from the resultset. But is this the only method to achieve the result? Isn't there a SQL solution?

                  推薦答案

                  乍一看...

                  您只需要一個帶有 MAX 聚合函數的 GROUP BY 子句:

                  At first glance...

                  All you need is a GROUP BY clause with the MAX aggregate function:

                  SELECT id, MAX(rev)
                  FROM YourTable
                  GROUP BY id
                  

                  事情從來沒有這么簡單,是嗎?

                  我剛剛注意到您還需要 content 列.

                  這是 SQL 中一個非常常見的問題:在每個組標識符的列中找到具有最大值的行的整個數據.在我的職業生涯中,我聽到了很多.實際上,這是我在當前工作的技術面試中回答的問題之一.

                  This is a very common question in SQL: find the whole data for the row with some max value in a column per some group identifier. I heard that a lot during my career. Actually, it was one the questions I answered in my current job's technical interview.

                  實際上,Stack Overflow 社區創建了一個標簽來處理這樣的問題非常普遍:greatest-n-per-group.

                  It is, actually, so common that Stack Overflow community has created a single tag just to deal with questions like that: greatest-n-per-group.

                  基本上,您有兩種方法可以解決該問題:

                  Basically, you have two approaches to solve that problem:

                  在這種方法中,您首先在子查詢中找到 group-identifier, max-value-in-group(上面已經解決了).然后你將你的表加入到子查詢中,在 group-identifiermax-value-in-group 上相等:

                  In this approach, you first find the group-identifier, max-value-in-group (already solved above) in a sub-query. Then you join your table to the sub-query with equality on both group-identifier and max-value-in-group:

                  SELECT a.id, a.rev, a.contents
                  FROM YourTable a
                  INNER JOIN (
                      SELECT id, MAX(rev) rev
                      FROM YourTable
                      GROUP BY id
                  ) b ON a.id = b.id AND a.rev = b.rev
                  

                  與自身左連接,調整連接條件和過濾器

                  在這種方法中,您將表與自身分開.group-identifier 中的平等.然后,2個聰明的動作:

                  Left Joining with self, tweaking join conditions and filters

                  In this approach, you left join the table with itself. Equality goes in the group-identifier. Then, 2 smart moves:

                  1. 第二個連接條件是左邊的值小于右邊的值
                  2. 當您執行第 1 步時,實際具有最大值的行將在右側具有 NULL(這是一個 LEFT JOIN,還記得嗎?).然后,我們過濾連接的結果,只顯示右側為 NULL 的行.
                  1. The second join condition is having left side value less than right value
                  2. When you do step 1, the row(s) that actually have the max value will have NULL in the right side (it's a LEFT JOIN, remember?). Then, we filter the joined result, showing only the rows where the right side is NULL.

                  所以你最終得到:

                  SELECT a.*
                  FROM YourTable a
                  LEFT OUTER JOIN YourTable b
                      ON a.id = b.id AND a.rev < b.rev
                  WHERE b.id IS NULL;
                  

                  結論

                  兩種方法都帶來了完全相同的結果.

                  Conclusion

                  Both approaches bring the exact same result.

                  如果您有兩行 max-value-in-group 用于 group-identifier,那么這兩種方法的結果中都會包含這兩行.

                  If you have two rows with max-value-in-group for group-identifier, both rows will be in the result in both approaches.

                  這兩種方法都與 SQL ANSI 兼容,因此,無論其風格"如何,都可以與您最喜歡的 RDBMS 一起使用.

                  Both approaches are SQL ANSI compatible, thus, will work with your favorite RDBMS, regardless of its "flavor".

                  這兩種方法也是性能友好的,但是您的里程可能會有所不同(RDBMS、DB 結構、索引等).因此,當您選擇一種方法而不是另一種方法時,基準.并確保您選擇對您最有意義的那個.

                  Both approaches are also performance friendly, however your mileage may vary (RDBMS, DB Structure, Indexes, etc.). So when you pick one approach over the other, benchmark. And make sure you pick the one which make most of sense to you.

                  這篇關于SQL 僅選擇列上具有最大值的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 數據幀讀取?)
                  <tfoot id='4rq7q'></tfoot>

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

                      <i id='4rq7q'><tr id='4rq7q'><dt id='4rq7q'><q id='4rq7q'><span id='4rq7q'><b id='4rq7q'><form id='4rq7q'><ins id='4rq7q'></ins><ul id='4rq7q'></ul><sub id='4rq7q'></sub></form><legend id='4rq7q'></legend><bdo id='4rq7q'><pre id='4rq7q'><center id='4rq7q'></center></pre></bdo></b><th id='4rq7q'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4rq7q'><tfoot id='4rq7q'></tfoot><dl id='4rq7q'><fieldset id='4rq7q'></fieldset></dl></div>
                        <legend id='4rq7q'><style id='4rq7q'><dir id='4rq7q'><q id='4rq7q'></q></dir></style></legend>
                          <tbody id='4rq7q'></tbody>

                          1. 主站蜘蛛池模板: 欧美性极品xxxx做受 | 国产精品日韩高清伦字幕搜索 | 国产精品久久久久影院色老大 | 91福利影院 | 视频在线观看一区 | 成人h动漫亚洲一区二区 | 国产欧美一区二区三区在线看 | 免费黄色大片 | 中文精品视频 | 国产精品久久a | 天天操天天舔 | 中文字幕 在线观看 | 99re视频 | 欧美日韩在线成人 | 成人在线电影在线观看 | 国产黄色精品在线观看 | 亚洲区视频| 亚洲精品性视频 | 伊人网在线播放 | 久久婷婷国产香蕉 | 99热精品国产 | 日韩欧美网 | 免费黄色录像视频 | 日韩精品免费在线 | 国产黄色在线观看 | 精品一区二区在线视频 | 欧美日韩一区二区三区不卡视频 | 日批免费观看 | 青青久久久 | 成人精品一区二区三区 | 国产真实乱对白精彩久久小说 | 国产在线永久免费 | 天天爽网站 | 国产精品亚洲成在人线 | 亚洲一区二区av | 罗宾被扒开腿做同人网站 | 成人中文网 | 97免费在线观看视频 | 国产午夜在线 | 欧美精品在线观看 | 96av麻豆蜜桃一区二区 |