Solved

first  occurance of record

Posted on 2011-03-11
7
430 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore 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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

14 Experts available now in Live!

Get 1:1 Help Now