SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query similar to deduplicating
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Riesenrind
Starting Member

Germany
4 Posts

Posted - 01/13/2013 :  05:13:20  Show Profile  Reply with Quote
Hi. I have a big dataset (1 million records) which i've been working with in excel. But its getting too slow, so i want to do the task with SQL.

the dataset contains sales data and includes the fields State, productType, and Month. I need to get one record for each combination of these fields. In Excel, I created a calculated field as State&productType&Month and then deduplicated on that.

I've done the easy bit:
Select * from salesData where ...

Can anyone help me with the where clause?

Sql newbie

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 01/13/2013 :  05:58:32  Show Profile  Reply with Quote
SELECT DISTINCT
   [State],[productType],[Month]
FROM
   salesData;
Go to Top of Page

Riesenrind
Starting Member

Germany
4 Posts

Posted - 01/13/2013 :  12:19:38  Show Profile  Reply with Quote
Thanks. I forgot to mention, that there are lots of other fields, eg productId, productPrice, and I need them included in the output (but not regarded as DISTINCT).
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 01/13/2013 :  12:23:49  Show Profile  Reply with Quote

Select * from
(
Select *,ROW_NUMBER() OVER (PARTITION BY State, productType, Month Order by <Your Field>)as Seq
from Table
)P
where P.Seq = 1
Go to Top of Page

Riesenrind
Starting Member

Germany
4 Posts

Posted - 01/13/2013 :  12:23:54  Show Profile  Reply with Quote
... A bit more explanation....
I'm looking to get one example record for each combination of State, ProductType and Month, and I need all the other fields in that example record.
Go to Top of Page

Riesenrind
Starting Member

Germany
4 Posts

Posted - 01/13/2013 :  12:25:15  Show Profile  Reply with Quote
Wow sodeep. That was really quick. That looks like exactly what I want. thanks!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000