Solved

Help with SQL - Return 1 complete record for each group

Posted on 2015-01-09
4
120 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:
Scott Pletcher 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ssis not sending failure message 2 24
Count with a subquery showing details 10 43
migrate a SQL 2008 to 2016, 2 26
MS SQL BCP Extra Lines Between Records 2 15
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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