Solved

MS SQL Select Maximum Date Row Only

Posted on 2006-07-05
10
1,784 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 500 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 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