Solved

query lowest costs from table

Posted on 2012-03-12
8
265 Views
Last Modified: 2012-03-13
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
0
Comment
Question by:ExpressMan1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Expert Comment

by:rhinoceros
ID: 37712851
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
0
 
LVL 1

Expert Comment

by:ishanjrana
ID: 37713203
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
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 37715132
select t.* from tblcosts,
(select rateid, min(totalcost) as cost from tblcosts group by rateid) x
where t.rateid = x.rateid and t.totalcost = x.cost;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ExpressMan1
ID: 37715263
awking00

Getting the error..

The Microsoft Office Database engine  does not recognize "t.*" as a valid field name or expression.
0
 

Author Comment

by:ExpressMan1
ID: 37715352
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;
0
 

Author Closing Comment

by:ExpressMan1
ID: 37715574
Thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 37716111
Sorry I forgot the t alias originally.
0
 
LVL 1

Expert Comment

by:ishanjrana
ID: 37716288
I want to reopen this question beacuse I am not satisfied with distribution of points.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question