Solved

query lowest costs from table

Posted on 2012-03-12
8
258 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
  • 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 31

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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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 31

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now