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

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

      • <bdo id='NHOkH'></bdo><ul id='NHOkH'></ul>

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

      SQL 中的 GROUP BY/聚合函数混淆

      GROUP BY / aggregate function confusion in SQL(SQL 中的 GROUP BY/聚合函数混淆)
      <legend id='QB3NV'><style id='QB3NV'><dir id='QB3NV'><q id='QB3NV'></q></dir></style></legend>

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

      • <tfoot id='QB3NV'></tfoot>

            <tbody id='QB3NV'></tbody>

          • <bdo id='QB3NV'></bdo><ul id='QB3NV'></ul>

                <i id='QB3NV'><tr id='QB3NV'><dt id='QB3NV'><q id='QB3NV'><span id='QB3NV'><b id='QB3NV'><form id='QB3NV'><ins id='QB3NV'></ins><ul id='QB3NV'></ul><sub id='QB3NV'></sub></form><legend id='QB3NV'></legend><bdo id='QB3NV'><pre id='QB3NV'><center id='QB3NV'></center></pre></bdo></b><th id='QB3NV'></th></span></q></dt></tr></i><div id='QB3NV'><tfoot id='QB3NV'></tfoot><dl id='QB3NV'><fieldset id='QB3NV'></fieldset></dl></div>
              1. 本文介绍了SQL 中的 GROUP BY/聚合函数混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要一些帮助来理顺一些问题,我知道这是一个非常简单的问题,但它在 SQL 中让我有点困惑.

                I need a bit of help straightening out something, I know it's a very easy easy question but it's something that is slightly confusing me in SQL.

                此 SQL 查询在 Oracle 中引发不是 GROUP BY 表达式"错误.我明白为什么,因为我知道,一旦我按元组的属性分组,我就无法再访问任何其他属性.

                This SQL query throws a 'not a GROUP BY expression' error in Oracle. I understand why, as I know that once I group by an attribute of a tuple, I can no longer access any other attribute.

                SELECT * 
                FROM order_details 
                GROUP BY order_no
                

                但是这个确实有效

                SELECT SUM(order_price)
                FROM order_details
                GROUP BY order_no
                

                只是为了具体说明我对此的理解......假设每个订单的 order_details 中有多个元组,一旦我根据 order_no 对元组进行分组,我仍然可以访问每个单独元组的 order_price 属性组,但只使用聚合函数?

                Just to concrete my understanding on this.... Assuming that there are multiple tuples in order_details for each order that is made, once I group the tuples according to order_no, I can still access the order_price attribute for each individual tuple in the group, but only using an aggregate function?

                换句话说,在 SELECT 子句中使用聚合函数时,可以深入到组中查看隐藏"属性,而简单地使用SELECT order_no"会抛出错误?

                In other words, aggregate functions when used in the SELECT clause are able to drill down into the group to see the 'hidden' attributes, where simply using 'SELECT order_no' will throw an error?

                推荐答案

                在标准 SQL(但不是 MySQL)中,当您使用 GROUP BY 时,您必须在 GROUP BY 子句中列出所有不是聚合的结果列.因此,如果 order_details 有 6 列,那么您必须在GROUP BY 子句.

                In standard SQL (but not MySQL), when you use GROUP BY, you must list all the result columns that are not aggregates in the GROUP BY clause. So, if order_details has 6 columns, then you must list all 6 columns (by name - you can't use * in the GROUP BY or ORDER BY clauses) in the GROUP BY clause.

                你也可以这样做:

                SELECT order_no, SUM(order_price)
                  FROM order_details
                 GROUP BY order_no;
                

                这会起作用,因为所有非聚合列都列在 GROUP BY 子句中.

                That will work because all the non-aggregate columns are listed in the GROUP BY clause.

                你可以这样做:

                SELECT order_no, order_price, MAX(order_item)
                  FROM order_details
                 GROUP BY order_no, order_price;
                

                这个查询没有真正意义(或者很可能没有意义),但它会工作".它将列出每个单独的订单号和订单价格组合,并给出与该价格相关的最大订单项目(数量).如果订单中的所有商品都有不同的价格,您最终会得到每组一行.OTOH,如果订单中有几件商品的价格相同(比如每件 0.99 英镑),那么它会将这些商品组合在一起并返回该价格的最大订单商品编号.(我假设该表在 (order_no, order_item) 上有一个主键,其中订单中的第一项具有 order_item = 1,第二项是 2,依此类推.)

                This query isn't really meaningful (or most probably isn't meaningful), but it will 'work'. It will list each separate order number and order price combination, and will give the maximum order item (number) associated with that price. If all the items in an order have distinct prices, you'll end up with groups of one row each. OTOH, if there are several items in the order at the same price (say 0.99 each), then it will group those together and return the maximum order item number at that price. (I'm assuming the table has a primary key on (order_no, order_item) where the first item in the order has order_item = 1, the second item is 2, etc.)

                这篇关于SQL 中的 GROUP BY/聚合函数混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                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 查询中包含缺失的月份)

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

                    <small id='1gByM'></small><noframes id='1gByM'>

                        • <bdo id='1gByM'></bdo><ul id='1gByM'></ul>