Solved

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

Posted on 2004-08-20
8
946 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now