Solved

T-SQL Select Statement

Posted on 2007-12-06
3
2,196 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

806 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