简体   繁体   English

聚合函数无法与子查询一起正常工作

[英]Aggregate function not working as expected with subquery

Having some fun with MySQL by asking it difficult questions. 通过询问一些棘手的问题来与MySQL一起玩乐。

Essentially i have a table full of transactions, and from that i want to determine out of all the available products ( productid ), who ( userid ) has bought the most of each? 本质上,我有一个充满交易的表,从中我想确定所有可用产品( productid )中,谁( userid )购买了最多的产品? The type in the where clause refers to transaction type, 1 being a purchase. where子句中的类型是指交易类型,1为购买。

I have a subquery that on its own returns a list of the summed products bought for each person, and it works well by itself. 我有一个子查询,它自己返回一个为每个人购买的汇总产品的列表,它本身可以很好地工作。 From this i am trying to then pick the max of the summed quantities and group by product, which is a pretty straight forward aggregate. 因此,我试图从中选择总数量的最大值并按产品分组,这是一个非常简单的汇总。 Unfortunately it's giving me funny results! 不幸的是,这给了我有趣的结果! The userid does not correspond correctly to the reported max productid sales. userid与报告的最大productid userid销售不正确对应。

select 
    `userid`, `productid`, max(`sumqty`)
from
    (select 
        `userid`, `productid`, sum(`qty`) as `sumqty`
    from
        `txarchive`
    where
        `type` = 1
    group by `userid`,`productid`) as `t1`
group by `productid`

I have removed all the inner joins to give more verbal results as they don't change the logic of it all. 我删除了所有内部联接,以提供更多的口头结果,因为它们不会改变所有逻辑。

Here is the structure of tx if you are interested. 如果您有兴趣,这里是tx的结构。

id          bigint(20)    #transaction id
UserID      bigint(20)    #user id, links to another table.
ProductID   bigint(20)    #product id, links to another table.
DTG         datetime      #date and time of transaction
Price       decimal(19,4) #price per unit for this transaction
QTY         int(11)       #QTY of products for this transaction
Type        int(11)       #transaction type, from purchase to payment etc.
info        bigint(20)    #information string id, links to another table.

*edit Working final query: (Its biggish) * edit工作中的最终查询:(它很大)

select 
    `username`, `productname`, max(`sumqty`)
from
    (select 
        concat(`users`.`firstname`, ' ', `users`.`lastname`) as `username`,
            `products`.`name` as `productname`,
            sum(`txarchive`.`qty`) as `sumqty`
    from
        `txarchive`
    inner join `users` ON `txarchive`.`userid` = `users`.`id`
    inner join `products` ON `txarchive`.`productid` = `products`.`id`
    where
        `type` = 1
    group by `productname`,`username`
    order by `productname`,`sumqty` DESC) as `t1`
group by `productname`
order by `sumqty` desc

Not the best solution (not even guaranteed to work 100% of the times): 不是最佳的解决方案(甚至不能保证100%的时间都能工作):

select 
    `userid`, `productid`, max(`sumqty`)
from
    ( select 
          `userid`, `productid`, sum(`qty`) as `sumqty`
      from
          `txarchive`
      where
          `type` = 1
      group by 
          `productid`  
        , `userid`
      order by 
          `productid`
        , `sumqty` DESC          
    ) as `t1`
group by
    `productid`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM