Solved

first  occurance of record

Posted on 2011-03-11
7
436 Views
Last Modified: 2012-05-11
I have a able  with three columns
 REP,ZIP,DATE

I need to select the first record of every unique ZIP  there is.  In other word.

Select every record that has a unique zip code(the first one it finds)

hope that makes sense.
0
Comment
Question by:acardullo
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35110590


select distinct zip
from table
order by zip
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35110621
Thats not correct, try this way

select MAX(ZIP) ZIP, MAX(REP) REP, MAX(DATE) DATE
from table
group by ZIP
order by DATE DESC
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35110748
select REP,ZIP,DATE from
(
select *, row_number() over (partition by zip order by date desc) rowno
from yourtable
) a
where a.rowno = 1
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35110773
The above query should return latest date's zip records

If you want oldest date's zip records, try this query

select REP,ZIP,DATE from
(
select *, row_number() over (partition by zip order by date) rowno
from yourtable
) a
where a.rowno = 1

Note tested, since from mobile

Raj
0
 
LVL 6

Expert Comment

by:zane_o
ID: 35110795
If you just want unique Zips, @ewangoya looks close. If you want the first row for each unique Zip that is a different matter.

Unique Zips
SELECT DISTINCT Zip FROM Table

If you want the entire row data, please answer this question: When you say you want to select the "First" record, what do you mean, are they ordered somehow? Perhaps by date?
0
 

Author Closing Comment

by:acardullo
ID: 35110802
perfect.  thanks
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35114886
@acardullo: Did you test the accepted query properly !? Is that result you asked for ?

The accepted solution, fetch the MAX of each column based on ZIP. Not from same row, each of the columns could be different rows of the same ZIP.

To be more clear see the screenshot below - First recordset is the samle data (4 rows). Next one is the result from the accepted solution's query
(Click on screenshots to open)
Accepted Query's Result with sample data
This is the result, based on my query
Result on test data based on my query
This is test data that I checked. Copy Paste to SQL Server Management Studio and check
create table #table
(
	ZIP		VARCHAR(10),
	REP		VARCHAR(10),
	DATE	DATETIME
)

INSERT INTO #table
SELECT '12345', 'AAAA', '03/15/2011' UNION ALL
SELECT '12345', 'ZZZZ', '03/01/2011' UNION ALL
SELECT '12346', 'AAAA', '03/12/2011' UNION ALL
SELECT '12346', 'ZZZZ', '03/01/2011' 

SELECT * FROM #TABLE

-- Accepted Solution
SELECT * FROM #TABLE
select MAX(ZIP) ZIP, MAX(REP) REP, MAX(DATE) DATE
from #table
group by ZIP
order by DATE DESC 


-- My Query
SELECT * FROM #TABLE 
select REP,ZIP,DATE from
(
select *, row_number() over (partition by zip order by date DESC) rowno
from #table
) a
where a.rowno = 1

Drop Table #TABLE

Open in new window

 


Hope this make sense!
Raj
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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