本主题与我在本主题中要求的查询相关
Combine fields from different rows on condition
及其继续
Confused by JOINs - data missing in result
如果行中的amounttype是ItemFees,Promotion或Shipping(描述),则查询会从金额中汇总所有值。 所描述的查询(请参阅链接文章以获取详细说明):
SELECT
posteddate AS Date,
transactiontype AS Type,
report.orderid AS OrderID,
ROUND(SUM((amounttype = 'ItemFees') * amount),
2) AS Fees,
ROUND(SUM((amounttype = 'Promotion') * amount),
2) AS Promo,
ROUND(SUM((amountdescription = 'Shipping') * amount),
2) AS Shipping,
ROUND(SUM((amountdescription = 'Principal' AND amounttype = 'ItemPrice') *amount),
2) AS Price,
orders.DeliveryLand AS Country,
items.ItemVAT AS VAT
FROM
report
LEFT JOIN
ordersON report.orderid = orders.ExternalOrderID
LEFT JOIN
items ON report.sku = items.ItemID
WHERE DeliveryLand = 1
GROUP BY report.orderid , transactiontype
ORDER BY Date DESC, OrderID ASC;
此查询按预期工作 - 在大多数情况下。但我们以这些条目为例:
+-----------------+---------------------+------------+-------------------+--------+
| transactiontype | orderid | amounttype | amountdescription | amount |
+-----------------+---------------------+------------+-------------------+--------+
| Order | 305-2406165-0572365 | ItemPrice | Principal | 23.24 |
| Order | 305-2406165-0572365 | ItemPrice | Shipping | 3.69 |
| Order | 305-2406165-0572365 | ItemFees | Commission | -3.49 |
| Order | 305-2406165-0572365 | ItemFees | ShippingHB | -0.55 |
| Refund | 305-2406165-0572365 | ItemPrice | Principal | -23.24 |
| Refund | 305-2406165-0572365 | ItemPrice | Shipping | -3.69 |
| Refund | 305-2406165-0572365 | ItemFees | Commission | 3.49 |
| Refund | 305-2406165-0572365 | ItemFees | ShippingHB | 0.55 |
+-----------------+---------------------+------------+-------------------+--------+
如您所见,客户购买了一篇文章,但要求退款。不幸的是,这两个动作都由相同的orderid标识,但它们需要分开(我需要单独列出订单和退款)。所以,查询完全按照它所做的去做,并给我这个输出:
+------------+--------+---------------------+-------+-------+----------+
| date | type | orderid | fees | promo | shipping | price | country | VAT
+------------+--------+---------------------+-------+-------+--------+--------+---+---+
| 1419375600 | Refund | 305-2406165-0572365 | 8,08 | 0 | -7,38 | -46,48 | 1 | 0 |
+------------+--------+---------------------+-------+-------+-------+--------+---+---+
| 1419375600 | Order | 305-2406165-0572365 | -8,08 | 0 | 7,38 | 46,48 | 1 | 0 |
+------------+--------+---------------------+-------+-------+-------+--------+---+---+
现在,如果你进行数学运算,你会发现所有的值都乘以2。我无法弄清楚为什么查询会这样做,以及如何解决这个问题。我需要将所有操作分开,首先由orderid
标识,然后通过事务类型进行区分。关于这个的任何想法?
正确的是:
+------------+--------+---------------------+-------+-------+----------+
| date | type | orderid | fees | promo | shipping | price | country | VAT
+------------+--------+---------------------+-------+-------+--------+--------+---+---+
| 1419375600 | Refund | 305-2406165-0572365 | 4,04 | 0 | -3,69 | -23,24 | 1 | 0 |
+------------+--------+---------------------+-------+-------+-------+--------+---+---+
| 1419375600 | Order | 305-2406165-0572365 | -4,04 | 0 | 3,69 | 23,24 | 1 | 0 |
+------------+--------+---------------------+-------+-------+-------+--------+---+---+
然后,在我写这个问题时发生了另一件事:
还有另一个订单(不退款)包含与上面完全相同的值。除非它有一个不同的orderid,它仍然列出错误的双倍值!对此感到困惑,我希望有人知道答案。
我希望我能提供足够的有关此问题的信息。如果没有,请随时索取您需要的任何信息。
答案 0 :(得分:1)
首先执行此位:
SELECT x.transactiontype
, x.orderid
, ROUND(SUM((amounttype = 'ItemFees') * amount),2) Fees
, ROUND(SUM((amounttype = 'Promotion') * amount),2) Promo
, ROUND(SUM((amountdescription = 'Shipping') * amount),2) Shipping
, ROUND(SUM((amountdescription = 'Principal' AND amounttype = 'ItemPrice') * amount),2) Price
FROM report x
GROUP
BY transactiontype
, orderid
然后将其作为子查询加入到查询的其余部分