Solved

first  occurance of record

Posted on 2011-03-11
7
433 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

10 Experts available now in Live!

Get 1:1 Help Now