如何获得独特的记录。 [英] How to get unique records.

查看:122
本文介绍了如何获得独特的记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要独特的记录



例如在名称中有数据为Nikhil Nikhil。我希望输出为Nikhil



你能帮帮我吗。



我有什么尝试过:



- 不知道如何获得这个

Need unique record

e.g in the Name there is data as Nikhil Nikhil. I want output as Nikhil

Can you help me out please.

What I have tried:

-- Don't know how to get this one

推荐答案

试试这个

Try this one
DECLARE @Table TABLE(Name varchar(100))
insert into @Table values('Nikhil Nikhil')
insert into @Table values('Nikhil Ram')
;with cte as (
select *,SUBSTRING(Name,0,CHARINDEX(' ',Name)) FirstName,SUBSTRING(Name,CHARINDEX(' ',Name)+1,LEN(Name)-CHARINDEX(' ',Name))LastName from @Table
)select Name Original_Name,case when FirstName=LastName then FirstName Else Name End New_Name from cte


这是可能的解决方案,



- >按空格分割您的名字并获得数组

- >使用id创建表格形式链接 [ ^ ]



例如:

From->

|| id || name |||

| 1 | nikhil nikhil |

| 2 | mohibur rashid |



To->

|| id || name ||

| 1 | nikhil |

| 1 | nikhil |

| 2 | mohibur |

| 2 | rashid |



- >然后运行select query对所有名称进行分组,并使用group by id重新加入。



我不知道它是否会保持你的 mohibur rashid ,或者更改为

< b> rashid mohibur
Here can be possible solution,

-> Split your name by space and get array
-> using id create tabular form link[^]

example:
From->
||id||name|||
|1|nikhil nikhil|
|2|mohibur rashid|

To->
||id||name||
|1|nikhil|
|1|nikhil|
|2|mohibur|
|2|rashid|

-> then run select query to group all the name and rejoin using group by id.

I do not know whether it will keep your mohibur rashid as it is, or change to
rashid mohibur


在我看来,你想重做你的数据库来存储两个字段中的名字和姓氏。 SQL中的字符串混搭始终是最后的选择。 DISTINCT将为您提供独特的记录,但这不是您所要求的
It seems to me that you want to rework your DB to store the first and last names in two fields. String mashing in SQL is always a last resort. DISTINCT will give you unique records, but that's not really what you're asking


这篇关于如何获得独特的记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆