在 mysql 中使用 sum() over(Partition by) 合并多行数据
Combine multiple row data using sum() over(Partion by) in mysql
SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity FROM dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
Join lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides;
以上查询输出如下
STOCK SIDES QUANTITY
paper1 1 214
paper1 2 210
paper2 1 7
paper3 1 2
现在我的问题是,如果我想得到基于不同方面的个股总数怎么办?所以我尝试使用下面的查询,它给我一个错误,说服务器版本在第 1 行
附近使用正确语法的服务器版本
SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity, SUM(lflayouts.sides) OVER(partition by orders.stock) as Total FROM dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
Join lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides;
EXPECTED OUTPUT
STOCK SIDES QUANTITY TOTAL
paper1 1 214 414 or 214
paper1 2 210 414
paper2 1 7 7
paper3 1 2 2
MySQL 5.5.62 不支持 window 功能。
您可以使用标准连接来实现它,但查询看起来会更复杂。
SELECT T1.Stock
,T1.sides
,Sum(T2.Quantity) as RunningTotal
FROM (SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity
FROM
dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
JOIN lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides
) T1
INNER JOIN
(
SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity
FROM
dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
JOIN lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides
) T2
ON T1.sides >= T2.sides
AND T1.Stock = T2.Stock
GROUP BY T1.Stock
,T1.sides
Order BY T1.Stock
,T1.sides
SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity FROM dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
Join lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides;
以上查询输出如下
STOCK SIDES QUANTITY
paper1 1 214
paper1 2 210
paper2 1 7
paper3 1 2
现在我的问题是,如果我想得到基于不同方面的个股总数怎么办?所以我尝试使用下面的查询,它给我一个错误,说服务器版本在第 1 行
附近使用正确语法的服务器版本SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity, SUM(lflayouts.sides) OVER(partition by orders.stock) as Total FROM dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
Join lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides;
EXPECTED OUTPUT
STOCK SIDES QUANTITY TOTAL
paper1 1 214 414 or 214
paper1 2 210 414
paper2 1 7 7
paper3 1 2 2
MySQL 5.5.62 不支持 window 功能。
您可以使用标准连接来实现它,但查询看起来会更复杂。
SELECT T1.Stock
,T1.sides
,Sum(T2.Quantity) as RunningTotal
FROM (SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity
FROM
dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
JOIN lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides
) T1
INNER JOIN
(
SELECT orders.Stock ,lflayouts.sides, count(*) as Quantity
FROM
dash_relationship
JOIN orders ON orders.UID = dash_relationship.form_id
JOIN lfitems ON lfitems.uid = orders.UID
JOIN lflayouts ON lflayouts.id = lfitems.layout_id
WHERE dash_relationship.machine_id='108'
GROUP BY orders.stock,lflayouts.sides
) T2
ON T1.sides >= T2.sides
AND T1.Stock = T2.Stock
GROUP BY T1.Stock
,T1.sides
Order BY T1.Stock
,T1.sides