Solved

select unique values (selecting the most recent record based on date)

Posted on 2004-08-20
8
931 Views
Last Modified: 2012-08-13
hi

i need to select unique values based on date(the recent date)

the table is like this

name     created
aa           5/5/03
bb           6/3/04
cc           3/3/04
aa           8/8/04
bb           3/3/03

the output i am looking for is
name      created
aa           8/8/04
bb           6/3/04
cc           3/3/04

thanks
red
0
Comment
Question by:red27man
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 11854996
red27man:

> i need to select unique values based on date(the recent date)

Try this:
    SELECT Name, MAX(Created) FROM YourTable GROUP BY Name

Hope That Helps,
Dex*
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11855202
This is works if you have MORE than just NAME in your source table

select *
from MyTable m
inner join ( select name, max([created]) as maxdate
                 from MyTable
                 group by name ) as x
                 on m.name= x.name
                 and m.[created] = x.maxdate )

Jay
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 11855244
oops ... PLEASE delete the LAST )
the function should read

select *
from MyTable m
inner join ( select name, max([created]) as maxdate
                 from MyTable
                 group by name ) as x
                 on m.name= x.name
                 and m.[created] = x.maxdate

Jay
0
 

Author Comment

by:red27man
ID: 11855387
It still gives the duplicate values



the output i am looking for is
name      created
aa           8/8/04
bb           6/3/04
cc           3/3/04


one record each of the "name" field ( that  one record being the most recent)

let me put it the other way
i need to have  distinct value of  " name" field ( where the distinct value being selected is based on created)

thanks
shri


thanks
red
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 19

Expert Comment

by:Dexstar
ID: 11856134
red27man:

> let me put it the other way
> i need to have  distinct value of  " name" field ( where the distinct value being
> selected is based on created)

Did you get duplicates with my suggestion too?  The "Group By" part of it means to put all the Names in a group, so you shouldn't get any duplicates.

-D*
0
 
LVL 2

Accepted Solution

by:
pek99 earned 100 total points
ID: 11856755
If you have duplicates with GROUP BY NAME, then you have probably "hidden" spaces in the column NAME.

Then this may work for you:

SELECT TRIM( Name), MAX(Created) FROM YourTable GROUP BY TRIM(Name);
0
 
LVL 10

Assisted Solution

by:Jay Toops
Jay Toops earned 150 total points
ID: 11857678
add a sequential id field so that you can MAX that too.

jay then you WILL get only one record...
with mine if you have more than one identical date you can get duplicates.

Jay
0
 

Author Comment

by:red27man
ID: 11872473
Thanks...........i had to work a bit but both of them were helpful

red
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

18 Experts available now in Live!

Get 1:1 Help Now