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行的'(partition)附近使用
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
答案 0 :(得分:1)
MySQL 5.5.62不支持窗口功能。
您可以使用标准联接来实现它,但是查询看起来会更加复杂。
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