我有一个难以解释的不同问题 这是我现在得到的结果:
╔═════════╦════════╗
║ Id ║ Saldo ║
╠═════════╬════════╣
║ 1 ║10441,91║
╠═════════╬════════╣
║ 2 ║ 441,91 ║
╚═════════╩════════╝
这就是我想要的结果(彼此相邻):
╔═════════╦════════╗════════╗════════╗
║ Id ║ Saldo ║ Id ║ Saldo ║
╠═════════╬════════╣════════║════════║
║ 1 ║10441,91║ 2 ║ 441,91 ║
╚═════════╩════════╝════════╝════════╝
所以我想要的就是彼此相邻。
这是我使用的查询:
SELECT
o.id_shop AS id,
SUM(CASE
WHEN (s.amount + s.shipping_cost_amount) != '' THEN o.total_paid_tax_excl - (s.amount + s.shipping_cost_amount)
ELSE o.total_paid_tax_excl
END) AS Shop
FROM
expoled.ps_orders o
LEFT JOIN
expoled.ps_oxoquotation_quotation q ON q.idOrder = o.id_order
LEFT JOIN
expoled.ps_order_slip s ON s.id_order = o.id_order
WHERE
o.current_state IN (4 , 5, 20, 23)
AND YEAR(q.date_add) = YEAR(UTC_TIMESTAMP())
GROUP BY o.id_shop
我尝试了什么: 我已经尝试过UNION来组合结果集但是没有用。 我可以把它们全部放在彼此之下但不能彼此相邻。
答案 0 :(得分:1)
你总是只有两个可能的ID,1和2吗?如果是这样,您可以删除GROUP BY o.id_shop并以这种方式创建两个sum列:
SUM(CASE
WHEN (s.amount + s.shipping_cost_amount) != '' AND o.id_shop = 1 THEN o.total_paid_tax_excl - (s.amount + s.shipping_cost_amount)
WHEN (s.amount + s.shipping_cost_amount) = '' AND o.id_shop = 1 THEN o.total_paid_tax_excl
ELSE 0
END) AS Shop_ID_1
SUM(CASE
WHEN (s.amount + s.shipping_cost_amount) != '' AND o.id_shop = 2 THEN o.total_paid_tax_excl - (s.amount + s.shipping_cost_amount)
WHEN (s.amount + s.shipping_cost_amount) = '' AND o.id_shop = 2 THEN o.total_paid_tax_excl
ELSE 0
END) AS Shop_ID_2