I am trying to fetch records from 4 tables by using joins. These tables are rates, carriers, depots and margin. I am able to get the records that needed from this 4 table, but when I am using a group by and aggregate function (MIN), my min sell column is correct, however, the other data such as carrierID, depotID and ratesID are different.
I have OriginType (OT) and DestinatioType (DT)and there are 2 cases for each; Depot and Door. So when I group them, I get four options as (services):
- Depot To Depot
- Depot To Door
- Door To Depot
- Door To Door
I am trying to fetch the min(rates) as 'Sell' for each of these service and display it. The Sell is calculated as:
- autoRate table as A has a column Buy, carrier, OT, DT, Origin (Value: Canberra), Destination (Value: Melbourne) and Car (Value :4WD/Van)
- Left Join with carriers as C with A.carrier = C.ID
- C.Fuellevy column as Percentage ((C.FuelLevy * A.buy) + C.FuelLevy)as EQ1
- ((EQ1 * 10%) + EQ1)as EQ2
- EQ2 price for each row with have a Margin table Percentage. For example, if the EQ2 value is 400, then It will look in the Margin table, find the range (low and high as 350(low) and 500(high)) and its percentage is 25%, so ((EQ2 * 25%)+ EQ2)gives the sell value.
I am not too sure how to upload my data and database table here, so I tried to explain here what I want.
the query I build is:-
Select   Depo.*,  DL.id as DepoID,  DL.carrier as CarNo, DL.depotCity, DL.depoSuburb, min(Depo.Sell)  as sellcost , Depo.OriginType as OT From (
    Select Mar.*,  M.MarginPer, round((eq2 * M.MarginPer) + eq2)as Sell   From (
                        Select GST.* , EQ1 as 'FinalEQ1' , round((EQ1 * .10) + EQ1,2) as eq2 From (
                                Select A.ID as RateID, A.Origin, A.OriginState, A.Destination, A.DestinationState, A.Carrier as RateCarrier, A.Car as CarType, A.Buy as Buy, A.OriginType, A.DestinationType ,
                                C.ID as CarrierID, C.Carrier, C.FuelLevy , round((A.buy * C.FuelLevy) + A.Buy, 2) As EQ1,
                                CONCAT(A.OriginType, ' to ' ,A.DestinationType ) as service,
                                 D.id as DepoID,  D.carrier as CarNo, D.depotCity, D.depoSuburb
                                from carrier C
                                left join autorates A on A.carrier = C.ID
                                left join dList D on D.carrier =  C.ID
                                where A.origin = 'Canberra' and A.destination = 'Melbourne' and A.car = '4WD/Van'  AND  D.carrier = A.carrier AND A.goodsAllowed =  0
                                AND C.Disabled = 0
                                AND D.depotCity  = 'Canberra'
                                 order by  EQ1
                            ) As GST
                        order by  eq2
             ) As Mar
             Left Join margin M on Mar.eq2 >=  M.low and  Mar.eq2 <= M.high
                   order by  Sell
        ) As Depo
            Left  Join dList DL  on DL.Carrier = Depo.RateCarrier
            Where DL.depotCity = 'Melbourne'
             group by OT
             order by  sellcost
Results before the Group by and MIN():-

As we see the sellcost value 412 and the carNo is 51 here
And in this screenshot, the carrierno has changed, but the the MIN value remains the same.

 
     
    