SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DISTINCT


DISTINCT

Author
Message
dreamslogic
dreamslogic
SSC-Addicted
SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)

Group: General Forum Members
Points: 499 Visits: 203
Hi Guru's

I 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_date
FROM [dbo].[Depot] d
WHERE d.status = 'L'

Thanks,


D
arun.sas
arun.sas
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5431 Visits: 3493
Use of the distinct to avoid the same value repeated (duplicate) records in the full rows,
And try this
select distinct
isnull(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_date
FROM [dbo].[Depot] d
WHERE d.status = 'L'


dreamslogic
dreamslogic
SSC-Addicted
SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)SSC-Addicted (499 reputation)

Group: General Forum Members
Points: 499 Visits: 203
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 all
select 'abc', null, getdate(), 'L' union all
select 'xyz', '123', getdate(), 'L' union all
select 'xyz', '123', getdate(), 'L' union all
select null, '123', getdate(), 'L' union all
select 'LMN', null, getdate(), 'L' union all
select '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') x
where r =1
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search