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
Solved

MS SQL Select Maximum Date Row Only

Posted on 2006-07-05
10
1,756 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
syadmin MSSQL 2 71
Multiple MSSQL instances on same server 4 47
Exchange 2007 standard - defrag (eseutul /d) 10 57
Mysql Left Join Case 10 70
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

860 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