问题
This thread is related with my older one Combine fields from different rows on condition.
I adjusted the query I got from this thread to meet some other requirements.
SELECT
a.Date,
a.orderid AS AZNr,
a.Typ,
ROUND(a.Fees, 2) AS Fees,
ROUND(b.Shipping, 2) AS Shipping,
ROUND(c.Price, 2) AS Price,
d.DeliveryLand
FROM
(SELECT
posteddate AS Date,
transactiontype AS Typ,
orderid,
SUM(amount) AS Fees
FROM
report
WHERE
amounttype = 'ItemFees'
GROUP BY orderid) a
LEFT JOIN
(SELECT
orderid, SUM(amount) AS Shipping
FROM
report
WHERE
amountdescription = 'Shipping'
GROUP BY orderid) b ON a.orderid = b.orderid
LEFT JOIN
(SELECT
orderid, SUM(amount) AS Price
FROM
report
WHERE
amountdescription = 'Principal'
GROUP BY orderid) c ON b.orderid = c.orderid
LEFT JOIN
(SELECT
DeliveryLand, ExternalOrderId
FROM
orders) d ON c.orderid = d.ExternalOrderId
ORDER BY Date DESC
I had to do a LEFT JOIN
on the last table to get the DeliveryLand, but not every item from the report-table has one entry in the orders-table.
After I did some calculating I found that this query does not return all entries. There should be 25 entries with "typ" = "Refund", but I only get 20 of them. Some orders might contain entries that other orders (identified by orderid) might not have.
I did another query to sum up the fees, the shipping costs and the price just for the refunds:
SELECT
SUM(ROUND(a.Fees, 2)) AS Fees,
SUM(ROUND(b.Shipping, 2)) AS Shipping,
SUM(ROUND(c.Price, 2)) AS Price
FROM
(SELECT
orderid, SUM(amount) AS Fees
FROM
report
WHERE
amounttype = 'ItemFees'
AND transactiontype = 'Refund'
GROUP BY orderid) a
LEFT JOIN
(SELECT
orderid, SUM(amount) AS Shipping
FROM
report
WHERE
amountdescription = 'Shipping'
AND transactiontype = 'Refund'
GROUP BY orderid) b ON a.orderid = b.orderid
LEFT JOIN
(SELECT
orderid, SUM(amount) AS Price
FROM
report
WHERE
amountdescription = 'Principal'
AND transactiontype = 'Refund'
GROUP BY orderid) c ON b.orderid = c.orderid
The first two results, fees and shipping costs, are summed up correctly (I got the original data for comparison), but the last one, the price, isn't correct, its too much. I guess there is some data getting truncated by the LEFT JOIN
, but I can't figure out why and where, especially this query works perfectly fine when I sum up the same colums for "transactiontype" = "Order".
I don't know why there is some data truncated or missing. Can somebody help me with these confusing JOINs I do in both queries? If you need more information, please ask.
Thank you in advance!
€dited query:
SELECT
posteddate AS Date,
transactiontype AS Typ,
report.orderid AS AZNr,
ROUND(SUM((amounttype = 'ItemFees') * amount),
2) AS Fees,
ROUND(SUM((amountdescription = 'Shipping') * amount),
2) AS Shipping,
ROUND(SUM((amountdescription = 'Principal') * amount),
2) AS Price,
orders.DeliveryLand,
articles.ItemVAT AS VAT
FROM
report
LEFT JOIN
orders ON report.orderid = orders.ExternalOrderID
LEFT JOIN
articles ON report.sku = articles.ItemID
GROUP BY report.orderid , transactiontype
回答1:
I'd start by simplifying the query, it can be written without JOIN
s. I'd also question why you ROUND
before the SUM
which will take longer and be less precise.
I'd do:
SELECT orderid,
ROUND(SUM((amountdescription='ItemFees')*amount), 2)) AS Fees,
ROUND(SUM((amountdescription='Shipping')*amount), 2)) AS Shipping,
ROUND(SUM((amountdescription='Principal')*amount), 2)) AS Price
FROM report
WHERE transactiontype='Refund'
GROUP BY orderid
As an explanation (amountdescription='ItemFees')
returns 1 if true and 0 if not, thus the amounts are only summed that match each condition specified.. you can use the longer CASE
if you prefer:
SUM(CASE WHEN *condition* THEN amount ELSE 0 END)
You may be losing some data from the original query as it relies on all the subtables linking up.. if there are no Fees
then an order's Shipping
and Price
would not have been returned.
UPDATE
Don't forget if you have two transactiontypes on the same orderid, they will all count as the same order and will both be included in the same sum.. to get the transaction types separately use:
SELECT orderid, transactiontype,
ROUND(SUM((amountdescription='ItemFees')*amount), 2)) AS Fees,
ROUND(SUM((amountdescription='Shipping')*amount), 2)) AS Shipping,
ROUND(SUM((amountdescription='Principal')*amount), 2)) AS Price
FROM report
GROUP BY orderid, transactiontype
来源:https://stackoverflow.com/questions/26128296/confused-by-joins-data-missing-in-result