我有以下查询:
(SELECT phppos_sales.sale_id as sale_id, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) +(((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as total
FROM phppos_sales_items
INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id
INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id
LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line
WHERE sale_time BETWEEN "2014-08-01 00:00:00" and "2014-08-09 23:59:59"
GROUP BY phppos_sales.sale_id, phppos_sales_items.item_id, phppos_sales_items.line)
示例结果:
| sale_id | total |
+---------+------------------------------------+
| 3863 | 172.778400000000000000000000000000 |
| 3863 | 362.815200000000000000000000000000 |
| 3863 | 129.578400000000000000000000000000 |
| 3863 | 215.973000000000000000000000000000 |
| 3863 | 615.519000000000000000000000000000 |
+---------+------------------------------------+
我想为每笔销售添加第三列payment_amount。要查找付款金额,请执行以下查询。
SELECT SUM(payment_amount) from phppos_sales_payments WHERE sale_id = XXX;
有没有办法在1个查询中执行此操作?
答案 0 :(得分:2)
到您的查询只需添加此内容。
SELECT部分中的添加temp.summed_amount
到JOIN的添加
LEFT JOIN
( SELECT
SUM(payment_amount) as summed_amount,
sale_id
FROM phppos_sales_payments
) temp on temp.sale_id = phppos_sales.sale_id;
您应该尝试格式化查询,因为它们目前基本上无法读取
所以你的查询应该看起来像这样。
SELECT
phppos_sales.sale_id as sale_id,
( item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100
) +
( item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100
) *
( SUM
( CASE WHEN cumulative != 1 THEN percent ELSE 0 END
) / 100
) +
(
(
( item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100
) *
( SUM
(CASE WHEN cumulative != 1 THEN percent ELSE 0 END) / 100
) +
( item_unit_price * quantity_purchased-item_unit_price * quantity_purchased * discount_percent / 100
)
) *
( SUM
(CASE WHEN cumulative = 1 THEN percent ELSE 0 END)
) / 100
) as total,
temp.summed_amount
FROM phppos_sales_items
INNER JOIN phppos_sales
ON phppos_sales_items.sale_id=phppos_sales.sale_id
INNER JOIN phppos_items
ON phppos_sales_items.item_id=phppos_items.item_id
LEFT OUTER JOIN phppos_sales_items_taxes
ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id
AND phppos_sales_items.item_id=phppos_sales_items_taxes.item_id
AND phppos_sales_items.line=phppos_sales_items_taxes.line
LEFT JOIN
( SELECT
SUM(payment_amount) as summed_amount,
sale_id
FROM phppos_sales_payments
) temp on temp.sale_id = phppos_sales.sale_id;
WHERE sale_time BETWEEN "2014-08-01 00:00:00" AND "2014-08-09 23:59:59"
GROUP BY
phppos_sales.sale_id,
phppos_sales_items.item_id,
phppos_sales_items.line
答案 1 :(得分:1)
尝试运行以下代码:
SELECT phppos_sales.sale_id as sale_id,
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*
(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) +
(((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*
(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) +
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100))
*(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as total, a.Sum
FROM phppos_sales_items
INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id
INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id
LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id
and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line
INNER JOIN (SELECT sale_id, SUM(payment_amount) Sum from phppos_sales_payments GROUP BY sale_id)a ON a.sale_id = phppos_sales.sale_id
WHERE sale_time BETWEEN "2014-08-01 00:00:00" and "2014-08-09 23:59:59"
GROUP BY phppos_sales.sale_id, phppos_sales_items.item_id, phppos_sales_items.line