Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS SQL Select Maximum Date Row Only

Posted on 2006-07-05
10
Medium Priority
?
1,796 Views
Last Modified: 2008-01-09
Sir, I have below query:


select       Transno,
      CargoType,
      CntrSize,
      CntrType,
      Destination,
      amount,  
      SD=Max(startingDate),
      ED=EndingDate
from MTImpAgentRateDet
where transno='008'
group by transno,
      cargotype,
      CntrSize,
      CntrType,
      Destination,
      amount
left outer join MTImpAgentCost b on a.sn=b.sn
where a.transno='008'

Result
--------

008      General      20'      DC      Kabul      1600      2005-11-24 00:00:00.000      2005-12-12 00:00:00.000
008      General      20'      DC      Kabul      1900      2005-09-17 00:00:00.000      2005-11-07 00:00:00.000
008      General      20'      DC      Kabul      1930      2005-11-08 00:00:00.000      2005-11-23 00:00:00.000
008      General      20'      DC      Kabul      2000      2005-12-13 00:00:00.000      2006-09-30 00:00:00.000
008      General      40'      DC      Kabul      2450      2005-08-01 00:00:00.000      2005-09-30 00:00:00.000
008      General      40'      DC      Kabul      2675      2005-10-01 00:00:00.000      2005-11-07 00:00:00.000
008      General      40'      DC      Kabul      2755      2005-11-08 00:00:00.000      2005-12-12 00:00:00.000
008      General      40'      DC      Kabul      3155      2005-12-13 00:00:00.000      2006-06-21 00:00:00.000
008      General      40'      DC      Kabul      3255      2006-06-22 00:00:00.000      2006-09-21 00:00:00.000


I want to show to user only the following to Rows:

008      General      20'      DC      Kabul      2000      2005-12-13 00:00:00.000      2006-09-30 00:00:00.000
008      General      40'      DC      Kabul      3255      2006-06-22 00:00:00.000      2006-09-21 00:00:00.000

Becuase the above two rows have max starting date in oder words the above rates is current rate.

Please guide how to show this.

Rgds.
Mehram

0
Comment
Question by:Mehram
[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
10 Comments
 

Author Comment

by:Mehram
ID: 17041824
Revised
----------

Sir, I have below query:


select      Transno,
     CargoType,
     CntrSize,
     CntrType,
     Destination,
     amount,  
     SD=startingDate,
     ED=EndingDate
from MTImpAgentRateDet
where transno='008'
group by transno,
     cargotype,
     CntrSize,
     CntrType,
     Destination,
     amount

Result
--------

008     General     20'     DC     Kabul     1600     2005-11-24 00:00:00.000     2005-12-12 00:00:00.000
008     General     20'     DC     Kabul     1900     2005-09-17 00:00:00.000     2005-11-07 00:00:00.000
008     General     20'     DC     Kabul     1930     2005-11-08 00:00:00.000     2005-11-23 00:00:00.000
008     General     20'     DC     Kabul     2000     2005-12-13 00:00:00.000     2006-09-30 00:00:00.000
008     General     40'     DC     Kabul     2450     2005-08-01 00:00:00.000     2005-09-30 00:00:00.000
008     General     40'     DC     Kabul     2675     2005-10-01 00:00:00.000     2005-11-07 00:00:00.000
008     General     40'     DC     Kabul     2755     2005-11-08 00:00:00.000     2005-12-12 00:00:00.000
008     General     40'     DC     Kabul     3155     2005-12-13 00:00:00.000     2006-06-21 00:00:00.000
008     General     40'     DC     Kabul     3255     2006-06-22 00:00:00.000     2006-09-21 00:00:00.000


I want to show to user only the following to Rows:

008     General     20'     DC     Kabul     2000     2005-12-13 00:00:00.000     2006-09-30 00:00:00.000
008     General     40'     DC     Kabul     3255     2006-06-22 00:00:00.000     2006-09-21 00:00:00.000

Becuase the above two rows have max starting date in oder words the above rates is current rate.

Please guide how to show this.

Rgds.
Mehram

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17041855
Hi Mehram,
try this
select Transno,     CargoType,      CntrSize,      CntrType,      Destination,
     amount,       SD=MAX(startingDate),     ED=EndingDate
from MTImpAgentRateDet
where transno='008'
group by transno,
     cargotype,
     CntrSize,
     CntrType,
     Destination,
     amount


Aneesh
0
 

Author Comment

by:Mehram
ID: 17041885
Sir, when i am trying this its asking for group endingdate and when i am including endingdate column in group by clause it showing all 9 rows instead of 2.
Please help
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!

 
LVL 65

Expert Comment

by:rockiroads
ID: 17041997
would this work?

select      Transno,      CargoType,      CntrSize,      CntrType,      Destination,      amount,  startingDate, EndingDate
from MTImpAgentRateDet a
where transno='008'
group by transno,     cargotype,     CntrSize,     CntrType,      Destination,      amount
where transno='008'
where startingdate = (select max(startingdate) from MTImpAgentRateDet  where a.Transno = Transno and a.CargoType = CargoType and a.CntrSize = CntrSize and a.CntrType = CntrType and a.Destination = Destination)



0
 

Author Comment

by:Mehram
ID: 17042032
its showing error on where
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17042054
oopps, I have two wheres!
doh!

change second where to be an and

select      Transno,      CargoType,      CntrSize,      CntrType,      Destination,      amount,  startingDate, EndingDate
from MTImpAgentRateDet a
where transno='008'
group by transno,     cargotype,     CntrSize,     CntrType,      Destination,      amount
where transno='008'
and startingdate = (select max(startingdate) from MTImpAgentRateDet  where a.Transno = Transno and a.CargoType = CargoType and a.CntrSize = CntrSize and a.CntrType = CntrType and a.Destination = Destination)


u may need to use IN

select      Transno,      CargoType,      CntrSize,      CntrType,      Destination,      amount,  startingDate, EndingDate
from MTImpAgentRateDet a
where transno='008'
group by transno,     cargotype,     CntrSize,     CntrType,      Destination,      amount
where transno='008'
and startingdate IN (select max(startingdate) from MTImpAgentRateDet  where a.Transno = Transno and a.CargoType = CargoType and a.CntrSize = CntrSize and a.CntrType = CntrType and a.Destination = Destination)
0
 

Author Comment

by:Mehram
ID: 17042067
When I am trying this its asking for group statingdate and endingdate

select Transno,      
      CargoType,      
      CntrSize,      
      CntrType,      
      Destination,      
      amount,  
      startingDate,
      EndingDate
from MTImpAgentRateDet a
where transno='008'
and startingdate = (select max(startingdate) from MTImpAgentRateDet  where a.Transno = Transno and a.CargoType = CargoType and a.CntrSize = CntrSize and a.CntrType = CntrType and a.Destination = Destination)
group by transno,    
      cargotype,    
      CntrSize,    
      CntrType,      
      Destination,            
      amount
0
 

Author Comment

by:Mehram
ID: 17042078
Sir, my Boss is calling me I am coming back
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17042536
remove the group by, u dont need it in the outer
The inner query returns the max date, and we join on that



select Transno,      
     CargoType,      
     CntrSize,      
     CntrType,      
     Destination,      
     amount,  
     startingDate,
     EndingDate
from MTImpAgentRateDet a
where transno='008'
and startingdate = (select max(startingdate) from MTImpAgentRateDet  where a.Transno = Transno and a.CargoType = CargoType and a.CntrSize = CntrSize and a.CntrType = CntrType and a.Destination = Destination)

0
 
LVL 32

Accepted Solution

by:
awking00 earned 2000 total points
ID: 17042568
select m.transno
      ,m.cargotype
      ,m.cntrsize
      ,m.destination
      ,m.amount
      ,m.startingdate
      ,m.endingdate
from mtimpagentratedet m
    ,(select cntrsize, max(startingdate) maxdate
      from mtimpagentratedet
      group by cntrsize) x
where m.cntrsize = x.cntrsize
  and m.startingdate = x.maxdate;
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

610 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