Solved

T-SQL Select Statement

Posted on 2007-12-06
3
2,199 Views
Last Modified: 2012-05-05
A typical record in one of our database tables reads:

<field1>: Joe Bloggs
<field2>: 06/12/2007
<field3>: ko
<field4>: sales
<field5>: Microsoft Corporation
<field6>: Bill Gates

In this table there will be multiple records for Joe Bloggs each with different data in the rest of the fields; I am trying to bring back one record for each different name in <field1> where the date in <field2> is the maximum date from each set of records matching the name.  By using the code attached I can achieve this but if I try to add <field5> and <field6> to the script it shows all records containing Joe Bloggs that match the criteria, not just the one I previously got.

Does any of this make sense?  If so can someone please help!

Thanks

Danny
use <database>
 
select
<field1>
max(field2),
<field3>
 
from
<table1>
where
<field3> = xxx
and <field2> <= getdate()
and <field4> in
(
xxx,
xxx,
xxx
)
group by <field1>, <field2>, <field3>
order by <field1>

Open in new window

0
Comment
Question by:SquareOneResources
[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
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 20418691
select t.*
from yourtable t
where t.field2 = ( select max(i.field2) from yourtable i where i.field1 = t.field1 )
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 250 total points
ID: 20418695
Hello SquareOneResources,

Select * from urTable u where  u.date  = (select MAX(Date) from urtable where filed1= u.filed1 )


Aneesh R
0
 
LVL 1

Author Comment

by:SquareOneResources
ID: 20418792
Thanks guys/gals, both solutions worked.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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