Solved

Help with SQL - Return 1 complete record for each group

Posted on 2015-01-09
4
118 Views
Last Modified: 2015-01-09
Hi..
I need to return 1 record , a sample record for each state in a table. Table has millions of records.
Table as a state field.

I need 1 single record for each state with all the fields..

table looks something like this

name, address, city, STATE, field1, field2, field3...
0
Comment
Question by:JElster
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 125 total points
ID: 40540756
assuming ID is the primary key

select * from yourTable where ID in  (  SELECT MIN(ID) from yourTable GROUP BY STATE )
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 40540762
SELECT name, address, city, STATE, field1, field2, field3, ...
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY state ORDER BY city) AS row_num
    FROM table_name
) AS derived
WHERE
    row_num = 1
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 125 total points
ID: 40540765
is there a primary key / rownumber on the table?

if so you can do something like:

--find the max row for each state.
select max(id) id, state
into #temp
from <table>
group by state

then:
select * from
table a
join #temp t on a.ID = t.ID



If not I guess it would work with Name, but not sure if you allow duplicate names in a state.
0
 
LVL 1

Author Closing Comment

by:JElster
ID: 40540824
no primary key

thx
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

11 Experts available now in Live!

Get 1:1 Help Now