Solved

MS SQL Select Maximum Date Row Only

Posted on 2006-07-05
10
1,771 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
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…
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…

679 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