【问题标题】:I'm having trouble with my query. Once I add an addition where before the group by, it says that I have a group by error我的查询有问题。一旦我在 group by 之前添加了一个附加项,它就会说我有一个 group by 错误
【发布时间】:2018-10-30 08:04:56
【问题描述】:

样本数据:

+ -----+------+--------+------+------+---------+--- ---------+--------+ |夹子 |客户 |符号 |侧 |数量 |填充数量 |填充像素 |执行 | + -----+------+--------+------+------+---------+--- ---------+--------+ | 274 | C2 |国际贸易中心 |出售 | 2000 | 167 | 12.09 | 1 | | 188 | C9 | SBS |出售 | 2000 | 121 | 38.599998 | 2 | | 155 | C1 | C |出售 | 1500 | 159 | 50.650002 | 3 | | 294 | C5 | BMY |购买 | 2000 |第667章13.648224 | 4 | | 276 | C7 | FCX |购买 | 2000 | 91 | 26.59 | 5 | | 255 | C9 | FCX |出售 | 2000 | 231 | 26.389999 | 6 | | 276 | C7 | FCX |购买 | 2000 | 182 | 30 | 7 | | 277 | C1 | BMY |购买 | 2000 |第1333章11.61 | 8 | | 188 | C9 | SBS |出售 | 2000 | 91 | 40.860001 | 9 | | 133 | C5 | BMY |出售 | 5000 |第636章12.721739 | 10 | | 110 | C7 |机管局 |购买 | 5000 | 182 | 8.359823 | 11 | | 120 | C7 | IBM |购买 | 5000 | 111 | 72.419998 | 12 | | 152 | C2 |文学士 |购买 | 1500 | 167 | 142.690002 | 13 | | 276 | C7 | FCX |购买 | 2000 | 273 | 28.25 | 14 | + -----+------+--------+------+------+---------+--- ---------+--------+ 客户方符号数量 clid C5 出售 F 2000 257 C6 购买亚马逊 1500 203 C7 购买 IBM 5000 120 C6 买 F 5000 228 C1 购买 UNH 5000 172 C1 卖出 FCX 2000 242 C8买F 2000 186 C5 购买 BMY 2000 294 C1 卖出 SBS 2000 212 C9 卖出 SBS 2000 194 C6 卖 FB 1500 232 C3 出售亚马逊 2000 113 C9 购买 BAC 5000 102 C8 购买 BMY 5000 227 C9 卖 FB 5000 296 C2 出售 INTC 2000 274 C6 卖 C 2000 171

客户将提交购买 X 股的买入订单,数量为已处理的部分订单。我要做的是找到已完成订单的数量和订单的平均价格。

这行得通:

select clid, oQty, sum(fillqty) 
from 
(select clid, side, oQty, fillqty, fillpx, execid 
from fills where side='buy' group by clid, fillqty) A 
group by clid;

这不起作用:

select clid, oQty, sum(fillqty) 
from 
(select clid, side, oQty, fillqty, fillpx, execid from fills where side='buy' group by clid, fillqty) A 
where oQty = sum(fillqty) 
group by clid;

ERROR 1111 (HY000):组函数使用无效

我不明白为什么我不能在 group by 之前添加一个比较...有人可以解释为什么会出现错误吗?

【问题讨论】:

  • 请为您的表格和预期输出添加示例数据。
  • 您应该阅读 sql 操作顺序。在这种情况下,where 在 group by 之前执行,因此 where 子句不知道 sum(fillqty)。
  • 你需要一个 group by,因为如果一个 sql 已经是 groupby 孩子,那么你再做 groupby 会得到什么
  • 您通常按您选择的列进行分组,除了那些作为设置函数的参数的列。
  • 你显然还不知道GROUP BY 做了什么。在您的子查询中,您说要按 clid 和 fillqty 进行分组。这应该聚合您的行,因此每个 clid/fillqty 获得一行。每个 clid/fillqty 甚至可以找到不止一条记录吗?您的示例不包含此类行。如果不可能(因为 clid/fillqty 在您的表中是唯一的),那么为什么要按它们分组?如果可能,那么您希望为 clid/fillqty 检索哪一侧、oQty、fillpx 和 execid?可以选择多种值。

标签: mysql sql group-by


【解决方案1】:

您不能在 WHERE 子句中使用聚合函数,例如 SUM 在您的情况下。使用HAVING

SELECT 语句中创建一个别名并在HAVING 子句中使用它。这是一种更优化的方式,因为它避免了再次计算总和。

select clid, oQty, sum(fillqty) AS SumQty
from 
(select clid, side, oQty, fillqty, fillpx, execid 
from fills where side='buy' group by clid, fillqty) A 
group by clid
having SumQty = oQty;

【讨论】:

    【解决方案2】:

    现在应该

        Select * 
        From
        (select clid, oQty, sum(fillqty) as SUM 
        from 
        (select clid, side, oQty, fillqty, fillpx, execid 
        from fills where side='buy' group by clid, fillqty) A 
        group by clid)B
        where B.SUM=B.oQty;
    

    【讨论】:

      【解决方案3】:

      由于您是在比较聚合函数,因此您必须使用HAVING 而不是WHERE

      select clid, oQty, sum(fillqty) 
      from 
      (select clid, side, oQty, fillqty, fillpx, execid from fills where side='buy' group by clid, fillqty) A 
      having  sum(fillqty) = oQty 
      group by clid;
      

      【讨论】:

        【解决方案4】:

        鉴于每个部分填充的 clid、cust、side 和 oqty 都是相同的,您可以在不使用子查询的情况下进行操作,只需使用 max 或 min 来获取不同的 oqty,例如使用 group by 和 having 子句进行测试

         select clid,group_concat(side),group_concat(oqty),min(oqty),max(oqty) maxoqty,sum(fillqty) sumfill,count(*), 
                        sum(fillpx),avg(fillpx)
        from fills
        where side = 'buy'
        group by clid having maxoqty <> sumfill;
        

        请注意,为了好玩,我已经添加了一些其他聚合。

        +------+--------------------+--------------------+-----------+---------+---------+----------+-------------+-------------+
        | clid | group_concat(side) | group_concat(oqty) | min(oqty) | maxoqty | sumfill | count(*) | sum(fillpx) | avg(fillpx) |
        +------+--------------------+--------------------+-----------+---------+---------+----------+-------------+-------------+
        |  110 | BUY                | 5000               |      5000 |    5000 |     182 |        1 |        8.36 |    8.360000 |
        |  120 | BUY                | 5000               |      5000 |    5000 |     111 |        1 |       72.42 |   72.420000 |
        |  152 | BUY                | 1500               |      1500 |    1500 |     167 |        1 |      142.69 |  142.690000 |
        |  276 | BUY,BUY,BUY        | 2000,2000,2000     |      2000 |    2000 |     546 |        3 |       84.84 |   28.280000 |
        |  277 | BUY                | 2000               |      2000 |    2000 |    1333 |        1 |       11.61 |   11.610000 |
        |  294 | BUY                | 2000               |      2000 |    2000 |     667 |        1 |       13.65 |   13.650000 |
        +------+--------------------+--------------------+-----------+---------+---------+----------+-------------+-------------+
        6 rows in set (0.00 sec)
        

        【讨论】:

          猜你喜欢
          • 2021-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-02
          • 2017-07-13
          • 2010-09-29
          相关资源
          最近更新 更多