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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Distinct
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JadeV
Yak Posting Veteran

Canada
62 Posts

Posted - 09/26/2012 :  11:12:18  Show Profile  Reply with Quote
Can everyone tell me hwo to use for the query which has a lots of duplicate data.

For DISTINCT, I tried to use it, it works with the number and date, but it doesn't work when there is a filed of "note" or "text" or "description" with a long text.

Is there another way to check the DISTINCT data in a SQL query

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/26/2012 :  11:39:49  Show Profile  Reply with Quote
Distinct operator eliminates a duplicate only if EVERY column in the select list are the same for two rows. So if the text or note column is different, the result would include both rows.

If there are different data in the note column of two otherwise identical rows, what rule do you want to determine which of those notes to pick up? Here is a way to pick one based on alphanumeric ordering.
;with cte as 
(
	SELECT
		*,
		ROW_NUMBER() OVER (PARTITION BY number,dat ORDER BY [description],[note]) AS RN
	FROM
		YourTable
)
SELECT * FROM cte WHERE RN = 1;

Edited by - sunitabeck on 09/26/2012 11:40:25
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.05 seconds. Powered By: Snitz Forums 2000