﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / DISTINCT / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 17:41:49 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: DISTINCT</title><link>http://www.sqlservercentral.com/Forums/Topic793669-338-1.aspx</link><description>Sorry this is the right answer, I think.if you want distinct on one column then you  will have multiple other columns for that column.e.g. if you are asking for distinct name then against one name there would be multiple codes and dates. in such case you have to select one and ignore the others. i am  selecting the data on the basis of  Last_date for distinct name.declare @Depot table (name nvarchar(100), code nvarchar(100), last_date datetime, status nvarchar(5))insert into @Depot select 'abc', '789', getdate(), 'L' union allselect 'abc', null, getdate(), 'L' union allselect 'xyz', '123', getdate(), 'L' union allselect 'xyz', '123', getdate(), 'L' union allselect null, '123', getdate(), 'L' union allselect 'LMN', null, getdate(), 'L' union allselect 'abc', '456', getdate(), 'L'select depot_name, depot_code, status, last_date from 	(SELECT (coalesce(nullif(d.[name],''),'Unknown'))as   depot_name		  ,coalesce(nullif(d.[code],''),'Unknown') as depot_code 		  , d.[status]		  ,cast(floor(cast(d.[last_date] as float)) as smalldatetime)as last_date		  , row_number() over (partition By name order by last_date desc) r	FROM  @Depot  d	WHERE d.status = 'L') xwhere r =1</description><pubDate>Fri, 25 Sep 2009 01:25:37 GMT</pubDate><dc:creator>dreamslogic</dc:creator></item><item><title>RE: DISTINCT</title><link>http://www.sqlservercentral.com/Forums/Topic793669-338-1.aspx</link><description>Use of the distinct to avoid the same value repeated (duplicate) records in the full rows,And try this[code]select distinctisnull(d.[name],'Unknown')as depot_name,isnull(d.[code],'Unknown') as depot_code , d.[status],cast(floor(cast(d.[last_date] as float)) as smalldatetime)as last_dateFROM [dbo].[Depot] dWHERE d.status = 'L'[/code]</description><pubDate>Fri, 25 Sep 2009 01:00:31 GMT</pubDate><dc:creator>arun.sas</dc:creator></item><item><title>DISTINCT</title><link>http://www.sqlservercentral.com/Forums/Topic793669-338-1.aspx</link><description>Hi Guru'sI can understand without sample data it's hard but I am sure for below issue, you guys doesn't need sample data.I want to distinct either code or name. How can I do that.SELECT (coalesce(nullif(d.[name],''),'Unknown'))as   depot_name      ,coalesce(nullif(d.[code],''),'Unknown') as depot_code       , d.[status]      ,cast(floor(cast(d.[last_date] as float)) as smalldatetime)as last_dateFROM [dbo].[Depot] dWHERE d.status = 'L'Thanks,D</description><pubDate>Fri, 25 Sep 2009 00:20:22 GMT</pubDate><dc:creator>dreamslogic</dc:creator></item></channel></rss>