【问题标题】:how to add last values of same group in sql query如何在sql查询中添加同一组的最后一个值
【发布时间】:2017-05-31 03:59:53
【问题描述】:

我有一张这样的桌子

  attribute_id      product_id       quantity
       1              101               10
       2              101               7
       3              101               8
       4              102               2
       5              102               8
       6              102               6
       7              103               30
       8              103               25
       9              103               20

查询后,我想找到这样的查询。 这是半答案

    attribute_id  product_id   quantity
      3              101           8
      6              102           6
      9              103           20

最终输出将是

【问题讨论】:

  • 提示:sum(quantity)
  • MySQL 或 SQL Server
  • 这是在 MYSQL 服务器上
  • 他标记了phpmyadmin,我猜是mysql...

标签: mysql sql sql-server database


【解决方案1】:

您可以使用top 1 with tiesrow_number 来获取具有最大attribute_id 的每个product_id 的行,并在找到的数量值上使用sum

select sum(quantity) as quantity
from (
    select top 1
    with ties quantity
    from your_table
    order by row_number() over (
            partition by product_id order by attribute_id desc
            )
    ) t;

Demo

【讨论】:

    【解决方案2】:

    试试这个

    Select sum(quantity) as quantity 
      from table 
    where attribute_id in 
       (Select max(attribute_id) from 
         table 
         Group by product_id
       ) t
    

    内部查询将为您提供属性 id 3,6,9,您可以在外部查询中对它们的数量求和。

    【讨论】:

    • 查询结束时不需要“t”
    • 在许多数据库中,您需要一个表别名。放它没有害处。
    【解决方案3】:

    试试这个:使用子查询并为每个product_id 取最大attribute_idMAX 并加入如下:

    select sum(quantity) from <table> t
    inner join (select product_id, 
                    max(attribute_id) as attribute_id 
                from <table> 
                group by product_id) t1 on t1.product_id = t.product_id
        and t1.attribute_id = t.attribute_id
    

    【讨论】:

    • hahahaha..... 内部查询将给出 101,102,103.... 它肯定会汇总所有数量
    • 我认为您错过了attribute_id,它将返回最大归属为3,6,9,然后加入将完全按预期执行。请在离开任何 cmets 之前尝试一下。
    【解决方案4】:
    ;With cte(attribute_id,product_id,quantity)
    AS
    (
    SELECT  1,101,10 UNION ALL
    SELECT  2,101,7  UNION ALL
    SELECT  3,101,8  UNION ALL
    SELECT  4,102,2  UNION ALL
    SELECT  5,102,8  UNION ALL
    SELECT  6,102,6  UNION ALL
    SELECT  7,103,30 UNION ALL
    SELECT  8,103,25 UNION ALL
    SELECT  9,103,20
    )
    SELECT SUM(quantity) As Sumquantity From
    (
    SELECT *,Row_number()OVER(Partition BY product_id order by attribute_id) AS Rno from cte
    )dt where dt.Rno=3
    

    【讨论】:

      【解决方案5】:

      试试这个....

      select sum(b.quantity)
      from   table_Name1 
      where  Attribute_id in 
             (Select max(Attribute_id)
              from  table_Name1 group by Product_Id)
      

      【讨论】:

        【解决方案6】:
        SELECT
        SUM(tnm.quantity) AS MaxQuantity
        FROM(
        select product_id , MAX(attribute_id) AS LastAttribute
        from table_Name1
        group by product_id
        )lc inner join table_Name1 as tnm on tnm.attribute_id =  lc.LastAttribute 
        AND tnm.product_id =  lc.product_id
        

        【讨论】:

          猜你喜欢
          • 2014-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多