Avatar of ExpressMan1
ExpressMan1
Flag for Canada asked on

query lowest costs from table

Hi,

I have one table "tblCosts" of Ups and Fedex costs and would like to find the lowest TotalCost per RateId and also show the associated Carrier.

CostId    RateId    Carrier    TotalCost
1               12          FedEx       20
2               12          Ups          25
3               17          FedEx       50
4               17          Ups          48

Result.
CostId    RateId    Carrier    TotalCost
1              12          FedEx         20
4              17          Ups            48

Thanks
SQL

Avatar of undefined
Last Comment
ishanjrana

8/22/2022 - Mon
rhinoceros

1. Get the following result first..

SELECT min(TotalCost) as Cost , RateId from tblCosts group by RateId

RateId    Cost
12             20
17             48

2. And then.. (Syntax)

Select * from tblCosts where...  (SELECT min(TotalCost) as Cost, RateId from tblCosts group by RateId)....


* Try to use something like ... I have no database now, and no further testing
ishanjrana

create table tblCosts
(
CostId number primary key,
RateId number,
Carrier varchar2(20),
TotalCost number
);

insert into tblCosts
values(1,12,'FedEx',20);

insert into tblCosts
values(2,12,'Ups',25);

insert into tblCosts
values(3,17,'FedEx',50);

insert into tblCosts
values(4,17,'Ups',48);

Query-----------------------------------------------------------------------------------------------------
select * from tblCosts
where TotalCost in
      (
      select min(TotalCost)
      from tblCosts
      group by RateId
      );

output--
COSTID    RATEID    CARRIER         TOTALCOST
---------------------------------------------------------------
    1               12            FedEx                     20
    4              17             Ups                         48


Query---------------------------------------------------------------------------------------------------------

select * from tblCosts
where TotalCost in
                  (
                   select min(TotalCost)
                              from tblCosts
                              group by Carrier
                   );

output--
COSTID    RATEID    CARRIER         TOTALCOST
---------------------------------------------------------------
    1                12            FedEx                     20
    2                12             Ups                        25
ASKER CERTIFIED SOLUTION
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ExpressMan1

ASKER
awking00

Getting the error..

The Microsoft Office Database engine  does not recognize "t.*" as a valid field name or expression.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ExpressMan1

ASKER
Nearly there...a few to many columns in the query result but still good,

select * from tblcosts AS T1,
(select rateid, min(totalcost) as cost from tblcosts group by rateid) x
where t1.rateid = x.rateid and t1.totalcost = x.cost;
ExpressMan1

ASKER
Thanks
awking00

Sorry I forgot the t alias originally.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ishanjrana

I want to reopen this question beacuse I am not satisfied with distribution of points.