Solved

query lowest costs from table

Posted on 2012-03-12
8
259 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 44
xpath sql query 2008 8 42
grouping logic 6 46
Update data using formula 22 15
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

12 Experts available now in Live!

Get 1:1 Help Now