SQL Server:单列上的部分分组依据

SQL Server:单列上的部分分组依据,sql,sql-server,group-by,sum,partial,Sql,Sql Server,Group By,Sum,Partial,今天早些时候已经问了一个类似的问题,解决方案让我又问了另一个问题(在示例之后): 我有这两列,其中两列是数量 其中一个(LotQty)为总和,按Productid和批次分组 另一个(SumQuantity)必须是仅按Productid分组的前者的总和 结果应该如下所示: SumQuantity Productid LotQty Lot ---------------------------------------------------- 512

今天早些时候已经问了一个类似的问题,解决方案让我又问了另一个问题(在示例之后):

  • 我有这两列,其中两列是数量
  • 其中一个(LotQty)为总和,按Productid和批次分组
  • 另一个(SumQuantity)必须是仅按Productid分组的前者的总和
结果应该如下所示:

SumQuantity    Productid  LotQty        Lot
----------------------------------------------------
 512           40652      256.000000    2020-12-20
 512           40652      256.000000    2020-12-21
1024           40661      512.000000    2019-12-19
1024           40661      512.000000    2019-12-20
 512           40710      256.000000    2021-03-03
 512           40710      256.000000    2021-04-04
即SumQuantity=按productid分组的总和(LotQty),而总和(LotQty)按productid和批次分组

select 
    sum(sum(s.cuquantity)) over () SumQuantity,
    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
结果是:

SumQuantity  Productid    LotQty        Lot
---------------------------------------------------
2048         40652        256.000000    2020-12-20
2048         40652        256.000000    2020-12-21
2048         40661        512.000000    2019-12-19
2048         40661        512.000000    2019-12-20
2048         40710        256.000000    2021-03-03
2048         40710        256.000000    2021-04-04 
样本表

  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
  4            40661          512.000000    2019-12-19
  5            40661          512.000000    2019-12-20
  6            40710          256.000000    2021-03-03
  7            40710          255.000000    2021-04-04
  8            40710            1.000000    2021-04-04

我应该如何更改我的选择以获得我要查找的结果?

您只需要添加分区并删除额外的
总和。本质上,您希望使用窗口函数而不是完全聚合方法,以便可以返回行级别的详细信息

select distinct
    SumQuantity = sum(Cuquantity) over (partition by Productid),
    LotQty = sum(Cuquantity) over (partition by Productid, Lot),
    Productid,
    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
la.value

我想您只需要按
分区:

select sum(sum(s.cuquantity)) over (partition by productid) as SumQuantity,
       s.productid, sum(s.cuquantity) as LotQty, la.Value as 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
到目前为止,我还没有使用过“over()”,我也没有使用过“partion by”……它看起来在测试中很有效,我希望我正确地使用了它,并且它在现实世界中也能正常工作:)10倍多!