?
Solved

T-SQL Select Statement

Posted on 2007-12-06
3
Medium Priority
?
2,203 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 1000 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 1000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 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