Solved

MS SQL Select Maximum Date Row Only

Posted on 2006-07-05
10
1,729 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
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 31

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now