代码之家  ›  专栏  ›  技术社区  ›  bvh

SQL Server:获取和求和最小值

  •  -1
  • bvh  · 技术社区  · 6 年前

    on_hand 按门店和SKU进行汇总。

    这是我的样品表。注意 sku store 是一个 composite key .

    enter image description here

    我想要的结果是 16 : (10 + 5 + 1)

    我正在使用SQL Server 2008。

    先谢谢你。

    2 回复  |  直到 6 年前
        1
  •  4
  •   Leia    6 年前

    我会用一个子查询。这将得到您要查找的16个答案:

        SELECT   SUM (min_by_store)
        FROM     (SELECT     Store, min(on_hand) AS min_by_store
                    FROM     #Temp AS T
                GROUP BY     Store) AS MBS
    
        2
  •  2
  •   Gordon Linoff    6 年前

    select sum(on_hand)
    from t
    where t.sku = (select top (1) t2.sku
                   from t t2
                   where t2.store = t.store
                   order by t2.on_hand
                  );