Search code examples
mysqlrowsdetectmatchingdivide

MYSQL - Detect rows that have three columns in common


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?


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