Solved

Unique records

Posted on 2009-05-06
6
238 Views
Last Modified: 2012-05-06
I have a table that has the following sample information:

ID_NUM                                                   VER               PN
-------------------------------------               ----                ------------------
0000000000000000001000016      001      2T-MNILA-AA
0000000000000000001000019      001      338602-121
0000000000000000001000019      001      338602-161
0000000000000000001000019      001      338602-291
0000000000000000001000019      001      338602-A21
0000000000000000001000019      001      338602-AA1
0000000000000000001000019      001      338602-AB1
0000000000000000001000019      001      338602-001
0000000000000000001000019      001      338602-371
0000000000000000001000019      001      338602-391
0000000000000000001000019      001      338602-AD1
0000000000000000001000020      001      36-60382-25
0000000000000000001000023      001      DY-1280A-AD
0000000000000000001000024      001      DS-SWRCA-AA
0000000000000000001000025      001      CT-WNTER-20
0000000000000000001000031      001      36-60382-25
0000000000000000001000032      001      CT-NOVUS-BC
0000000000000000001000032      001      CT-NOVUS-CC
0000000000000000001000032      001      CT-NOVUS-DC
0000000000000000001000033      001      3X-75CAA-AA
0000000000000000001000034      001      70-40530-03
0000000000000000001000035      001      263661-002
0000000000000000001000035      001      339100-001

I need to create a function or sql statement that can identify the first record when there are multiple records in the table.  If I use the group-by function, it doesn't find the unique ones.
0
Comment
Question by:Fairfield
[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
  • 2
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24318767
well, I don't know how you designate "first", but here is a way to do it:

select * from (
select *, ranking = dense_rank() over(partition by id_num order, ver order by pn, newid())
from tablename
) a
where ranking = 1
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24318772
do you mean

with a as (Select *, row_number() over(partition by id_num order by ver) as row
from your_table)

select * from a where rown=1
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 24318777
What won't this show you?

select id_num,ver,min(pn)
from SomeTable
group by id_num,ver
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 60

Expert Comment

by:chapmandew
ID: 24318884
hmmm....while Brandons solution does work, so do the others....
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24318930
While they will all function.  I personally chose my method due the fact that row_number() would not have to assign a value to every record then filter.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24318960
true...but its almost exactly the same way min() works.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

626 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