?
Solved

first  occurance of record

Posted on 2011-03-11
7
Medium Priority
?
444 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
[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
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35110590


select distinct zip
from table
order by zip
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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 …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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