[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

T-SQL Select Statement

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

656 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