Solved

Unique records

Posted on 2009-05-06
6
231 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
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
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
Comment Utility
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
Comment Utility
What won't this show you?

select id_num,ver,min(pn)
from SomeTable
group by id_num,ver
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 60

Expert Comment

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

Expert Comment

by:BrandonGalderisi
Comment Utility
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
Comment Utility
true...but its almost exactly the same way min() works.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

7 Experts available now in Live!

Get 1:1 Help Now