Solved

first  occurance of record

Posted on 2011-03-11
7
440 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 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms sql + get number in list out of total 7 45
TSQL recursive CTE challenge... 8 34
query optimization 6 22
partitioning database after decade growth 8 59
In this article I will describe the Copy Database Wizard 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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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