• <legend id='Jfeay'><style id='Jfeay'><dir id='Jfeay'><q id='Jfeay'></q></dir></style></legend>
      <bdo id='Jfeay'></bdo><ul id='Jfeay'></ul>

    <i id='Jfeay'><tr id='Jfeay'><dt id='Jfeay'><q id='Jfeay'><span id='Jfeay'><b id='Jfeay'><form id='Jfeay'><ins id='Jfeay'></ins><ul id='Jfeay'></ul><sub id='Jfeay'></sub></form><legend id='Jfeay'></legend><bdo id='Jfeay'><pre id='Jfeay'><center id='Jfeay'></center></pre></bdo></b><th id='Jfeay'></th></span></q></dt></tr></i><div id='Jfeay'><tfoot id='Jfeay'></tfoot><dl id='Jfeay'><fieldset id='Jfeay'></fieldset></dl></div>
    1. <tfoot id='Jfeay'></tfoot>

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

        如何限制 SQL 中每个字段值的行数?

        How do I limit the number of rows per field value in SQL?(如何限制 SQL 中每个字段值的行数?)

              <tbody id='SMqx2'></tbody>

            <i id='SMqx2'><tr id='SMqx2'><dt id='SMqx2'><q id='SMqx2'><span id='SMqx2'><b id='SMqx2'><form id='SMqx2'><ins id='SMqx2'></ins><ul id='SMqx2'></ul><sub id='SMqx2'></sub></form><legend id='SMqx2'></legend><bdo id='SMqx2'><pre id='SMqx2'><center id='SMqx2'></center></pre></bdo></b><th id='SMqx2'></th></span></q></dt></tr></i><div id='SMqx2'><tfoot id='SMqx2'></tfoot><dl id='SMqx2'><fieldset id='SMqx2'></fieldset></dl></div>

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

              <bdo id='SMqx2'></bdo><ul id='SMqx2'></ul>
              <tfoot id='SMqx2'></tfoot>
                • <legend id='SMqx2'><style id='SMqx2'><dir id='SMqx2'><q id='SMqx2'></q></dir></style></legend>
                  本文介绍了如何限制 SQL 中每个字段值的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如,我在 Hive 中有一个这样的表:

                  For example, I have a table like this in Hive:

                  1 1
                  1 4
                  1 8
                  2 1
                  2 5
                  3 1
                  3 2
                  

                  我只想返回第一列的每个唯一值的前两行.我希望这能够限制我从 Hive 传输到 MySQL 以用于报告目的的数据量.我想要一个给我这个的 HiveQL 查询:

                  and I want to only return the first two rows of each unique value of the first column. I want this to be able to limit the amount of data that I transfer from Hive into MySQL for reporting purposes. I'd like a single HiveQL query that gives me this:

                  1 1
                  1 4
                  2 1
                  2 5
                  3 1
                  3 2
                  

                  推荐答案

                  不幸的是 mysql 没有分析函数.所以你必须玩弄变量.假设您有一个自动增量字段:

                  Unluckily mysql doesn't have Analytical Functions. So you have to play with variables. Supposing you have an autoincrement field:

                  mysql> create table mytab (
                      -> id int not null auto_increment primary key,
                      -> first_column int,
                      -> second_column int
                      -> ) engine = myisam;
                  Query OK, 0 rows affected (0.05 sec)
                  
                  mysql> insert into mytab (first_column,second_column)
                      -> values
                      -> (1,1),(1,4),(2,10),(3,4),(1,4),(2,5),(1,6);
                  Query OK, 7 rows affected (0.00 sec)
                  Records: 7  Duplicates: 0  Warnings: 0
                  
                  mysql> select * from mytab order by id;
                  +----+--------------+---------------+
                  | id | first_column | second_column |
                  +----+--------------+---------------+
                  |  1 |            1 |             1 |
                  |  2 |            1 |             4 |
                  |  3 |            2 |            10 |
                  |  4 |            3 |             4 |
                  |  5 |            1 |             4 |
                  |  6 |            2 |             5 |
                  |  7 |            1 |             6 |
                  +----+--------------+---------------+
                  7 rows in set (0.00 sec)
                  
                  mysql> select
                      -> id,
                      -> first_column,
                      -> second_column,
                      -> row_num
                      -> from (
                      -> select *,
                      -> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num,
                      -> @first_column:=first_column as c
                      -> from mytab order by first_column,id) as t,(select @first_column:='',@num:
                  =0) as r;
                  +----+--------------+---------------+---------+
                  | id | first_column | second_column | row_num |
                  +----+--------------+---------------+---------+
                  |  1 |            1 |             1 |       1 |
                  |  2 |            1 |             4 |       2 |
                  |  5 |            1 |             4 |       3 |
                  |  7 |            1 |             6 |       4 |
                  |  3 |            2 |            10 |       1 |
                  |  6 |            2 |             5 |       2 |
                  |  4 |            3 |             4 |       1 |
                  +----+--------------+---------------+---------+
                  7 rows in set (0.00 sec)
                  
                  mysql> select
                      -> id,
                      -> first_column,
                      -> second_column,
                      -> row_num
                      -> from (
                      -> select *,
                      -> @num := if(@first_column = first_column, @num:= @num + 1, 1) as row_num,
                      -> @first_column:=first_column as c
                      -> from mytab order by first_column,id) as t,(select @first_column:='',@num:
                  =0) as r
                      -> having row_num<=2;
                  +----+--------------+---------------+---------+
                  | id | first_column | second_column | row_num |
                  +----+--------------+---------------+---------+
                  |  1 |            1 |             1 |       1 |
                  |  2 |            1 |             4 |       2 |
                  |  3 |            2 |            10 |       1 |
                  |  6 |            2 |             5 |       2 |
                  |  4 |            3 |             4 |       1 |
                  +----+--------------+---------------+---------+
                  5 rows in set (0.02 sec)
                  

                  这篇关于如何限制 SQL 中每个字段值的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)

                • <small id='K2VPe'></small><noframes id='K2VPe'>

                  • <i id='K2VPe'><tr id='K2VPe'><dt id='K2VPe'><q id='K2VPe'><span id='K2VPe'><b id='K2VPe'><form id='K2VPe'><ins id='K2VPe'></ins><ul id='K2VPe'></ul><sub id='K2VPe'></sub></form><legend id='K2VPe'></legend><bdo id='K2VPe'><pre id='K2VPe'><center id='K2VPe'></center></pre></bdo></b><th id='K2VPe'></th></span></q></dt></tr></i><div id='K2VPe'><tfoot id='K2VPe'></tfoot><dl id='K2VPe'><fieldset id='K2VPe'></fieldset></dl></div>
                      <tfoot id='K2VPe'></tfoot>

                        <tbody id='K2VPe'></tbody>
                        <bdo id='K2VPe'></bdo><ul id='K2VPe'></ul>
                        1. <legend id='K2VPe'><style id='K2VPe'><dir id='K2VPe'><q id='K2VPe'></q></dir></style></legend>