簡體   English   中英

MSSQL跳過按“總和”列分組

[英]MSSQL Skip Group by on Sum column

我找不到我想要做的相關標題和描述。 因此,我希望您可以從示例中了解。

  • 我有這些列,其中兩個是數量。
  • 其中一個(LotQty)是一個和。
  • 另一個(SumQuantity)必須是前者的總和。
  • 其余(Productid和Lot)保持分組。

結果應如下所示:

    SumQuantity    Productid  LotQty        Lot
    512            40652      256.000000    2020-12-20
    512            40652      256.000000    2020-12-21

即SumQuantity = sum(LotQty)。 我應該如何更改我的選擇(如下)以創建那種效果?

    select s.productid,sum(s.cuquantity) LotQty,la.Value Lot 
    from log l 
    left join logstock s on s.logid=l.id
    left join Logstockattributes la on la.LogStockID=s.id and la.AttributeID=10
    where l.receiptid=5950195
    group by productid,la.value

結果是:

     Productid    LotQty        Lot
     40652        256.000000    2020-12-20
     40652        256.000000    2020-12-21

樣品表

  Logid        Productid      Cuquantity    Lot
      1        40652          256.000000    2020-12-20
      2        40652          255.000000    2020-12-21
      3        40652            1.000000    2020-12-21

您似乎想要一個窗口函數:

select s.productid, sum(s.cuquantity) as LotQty, la.Value as Lot,
       sum(sum(s.cuquantity)) over () as totalLogQty
from log l left join
     logstock s
     on s.logid = l.id left join
     Logstockattributes la
     on la.LogStockID = s.id and la.AttributeID = 10
where l.receiptid = 5950195
group by productid,la.value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM