MySql:如何仅获得四个唯一结果?

时间:2018-08-19 04:45:42

标签: mysql sql

我试图仅获得这四个突出显示的结果。我尝试将它们分组,但仍未获得所需的结果。

enter image description here

查询是:-

Set @filter := 0.00;

SELECT autorates2.ID, autorates2.Car, autorates2.Origin ,  autorates2.Destination, autorates2.Carrier,  
@FuelLevy := ( autorates2.Buy * Carrier.FuelLevy) + autorates2.Buy AS 'AfterFuelLevy',
@GST :=(@FuelLevy * 0.10) + @FuelLevy AS 'AfterGST',
@filter := (Select Margin.MarginPer from Margin where @GST between Margin.Low and Margin.High ),
@Margin :=  (@filter * @GST) + @GST  AS 'Sell',
autorates2.OriginType, autorates2.DestinationType, CONCAT(autorates2.OriginType, autorates2.DestinationType) as'ser'
from  Margin, Carrier 
RIGHT JOIN autorates2 on autorates2.Carrier =  Carrier.Carrier 
Where  autorates2.Origin = 'Melbourne' AND autorates2.Destination = 'Sydney' AND autorates2.Car = '4WD/Van'  AND Carrier.Disabled  = 0 AND autorates2.GoodsAllowed = 0   
Group by   ser
ORDER by  MIN(Sell),ser

我得到的输出是突出显示的结果是错误的地方:-

enter image description here

这是所需的实际结果。 enter image description here

表1:

enter image description here

表2:

enter image description here

表3:

enter image description here

1 个答案:

答案 0 :(得分:0)

按以下方式使用相关子查询,因为您没有共享所有源列名称,所以我猜价格是一列

     select * from yourtable t1 where price in     
       (
        select min(price) from yourtable t2 where t1.car=t2.car
        and t1.depot=t2.depot and t1.door=t2.door
        and t1.destinationtype=t2.destinationtype and t1.ser=t2.ser      
        )