Question

I'm working on a small script that will transfer order information from a newer database (for online orders) to an antiquated, custom database that they have 20+ years of information in and it only accepts tab delimited input (quite a pain) and can't handle multi-item SKUs (ie. each SKU is one item).

I've created a table (not shown) that relates the multi-item SKUs to the antiquated system's single SKUs - you can see below in rows 2 through 4 that there are three items in one (called "pcclean" online) that was originally one order line split into three rows (CL31, CL15, and CL13 - the old system's SKUs).

However, I'm having trouble splitting the price into three when generating this table. The prices change far too often to rely on stored values.

What I want to do is detect when Channel, Order, and Code are the same, and divide the "Total" in those rows by the number of rows that match that criteria.

So Rows 2-4 wouldn't show "12.45", they'd show "4.15" each.

Channel                 Order   QTY  SKU    Total    Code
Amazon Seller Central   1518    1    PX5    7.29     PX5
Amazon Seller Central   1519    1    CL31   12.45    pcclean
Amazon Seller Central   1519    1    CL15   12.45    pcclean
Amazon Seller Central   1519    1    CL13   12.45    pcclean
Amazon Seller Central   1520    1    MS516  25       MS516
Amazon Seller Central   1521    1    PX10   4.49     PX10
Amazon Seller Central   1522    1    CL31   12.45    pcclean
Amazon Seller Central   1522    1    CL15   12.45    pcclean
Amazon Seller Central   1522    1    CL13   12.45    pcclean
Amazon Seller Central   1523    1    PX40   12.94    PX40
Amazon Seller Central   1524    1    PX12   12.49    PX12
Amazon Seller Central   1525    1    AD254  14.05    AD36
Amazon Seller Central   1526    1    MS516  25       MS516
Amazon Seller Central   1527    1    CL31   12.45    pcclean
Amazon Seller Central   1527    1    CL15   12.45    pcclean
Amazon Seller Central   1527    1    CL13   12.45    pcclean
Amazon Seller Central   1528    1    PX13   27.54    px13
Amazon Seller Central   1529    1    PX28   18.02    PX28
Amazon Seller Central   1530    1    PX28   18.02    PX28
Amazon Seller Central   1531    1    PX40   12.94    PX40
Amazon Seller Central   1532    1    PX33   29.98    px33
Amazon Seller Central   1533    1    PX47   12.44    PX47
Amazon Seller Central   1534    6    PX1    64.44    1PX1
Amazon Seller Central   1534    1    PX40   18.43    px40px35
Amazon Seller Central   1534    1    PX35   18.43    px40px35
eBay                    104     1    MS516  16       150282363749

Any ideas?

Was it helpful?

Solution

SELECT *,total/cnt.val FROM `mytable` 
LEFT JOIN (SELECT `Channel`,`Order`,`Code`,COUNT(*) AS val 
           FROM `mytable` GROUP BY `Channel`,`Order`,`Code`) AS cnt 
USING (`Channel`,`Order`,`Code`);

example here: http://www.sqlfiddle.com/#!2/cdd16/28

OTHER TIPS

You can do it in single query by joining the table back onto itself:

SELECT  O.Channel,
        O.Order,
        O.Qty,
        O.SKU,
        O.Code,
        O.Total,
        ROUND(O.Total/GroupedOrders.OrderCount, 3) AS OriginalPrice

FROM    Orders AS O
INNER JOIN
            (SELECT Channel, `Order`, Code, COUNT(`Order`) AS OrderCount FROM Orders GROUP BY Channel, `Order`, Code) AS GroupedOrders 
                ON O.Channel = GroupedOrders.Channel AND O.Order = GroupedOrders.Order AND O.Code = GroupedOrders.Code

This will join the table onto the grouped version of itself, and divide the total through the number of rows that were grouped.

Here's the query in action: http://www.sqlfiddle.com/#!2/30b35/3

First detect rows with identical columns:

select Channel, Order, Code, Total, count(*) from mytable
group by Channel, Order, Code
having count(*) > 1

This will show all rows where you have multiple rows with Channel, Order and Code identical. Then you can take Total and count(*) and calculate your price for a single row.

You could use a subquery that returns the count of identical rows based on the three columns that you specified:

SELECT
  orders.Channel,
  orders.Order,
  orders.Code,
  orders.Total/
    (SELECT count(*)
     FROM
       orders orders_1
     WHERE
       orders_1.Channel = orders.Channel
       AND orders_1.Order = orders.Order
       AND orders_1.Code = orders.Code
     GROUP BY
       orders_1.Channel, orders_1.Order, orders_1.Code) As Total
FROM
  orders

of course you can add any column you want on the select clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top