Solved

T-SQL Select Statement

Posted on 2007-12-06
3
2,195 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
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys/gals, both solutions worked.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

10 Experts available now in Live!

Get 1:1 Help Now