SQL 服务器:在单个列上进行部分分组
SQL Server : partial group by on single column
当天早些时候已经问过类似的问题,解决方案将我引向另一个问题(在示例之后):
- 我有这些列,其中两列是数量。
- 其中一个(LotQty)是一个总和,按Productid和Lot分组
- 另一个 (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 = sum(LotQty) 按 productid 分组,而 sum(LotQty) 按 productid AND lot 分组。
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
样本table
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 以获得我正在寻找的结果?
您只需添加分区并删除多余的 SUM
。本质上,您需要 window 函数而不是完整的聚合方法,以便您可以 return 行级详细信息。
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
我想你只需要 partition by
:
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
当天早些时候已经问过类似的问题,解决方案将我引向另一个问题(在示例之后):
- 我有这些列,其中两列是数量。
- 其中一个(LotQty)是一个总和,按Productid和Lot分组
- 另一个 (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 = sum(LotQty) 按 productid 分组,而 sum(LotQty) 按 productid AND lot 分组。
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
样本table
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 以获得我正在寻找的结果?
您只需添加分区并删除多余的 SUM
。本质上,您需要 window 函数而不是完整的聚合方法,以便您可以 return 行级详细信息。
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
我想你只需要 partition by
:
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