我的桌子看起来像这样:
acronym | word
FCN | FCN
FCN | Fourth Corner Neurosurgical Associates
FHS | FHS
HW | HW
如你所见,有些首字母缩略词有匹配词,有些则没有。我想保留具有匹配单词的首字母缩略词。对于没有匹配单词的首字母缩略词,我想保留首字母缩略词。我希望结果表看起来像:
acronym | word
FCN | Fourth Corner Neurosurgical Associates
FHS | FHS
HW | HW
我想不出有办法实现这个目标。可能按“缩写”分组并选择“单词”,但是什么算法可以决定删除“FCN”或“Fourth Corner Neurosurgical Associates”
答案 0 :(得分:3)
假设没有任何“坏”字:
DELETE myTable
FROM myTable del
WHERE [acronym] = [word]
AND EXISTS ( SELECT *
FROM myTable lw -- Longer Word
WHERE lw.[acronym] = del.[acronym]
AND Len(lw.[word]) > Len(lw.[acronym]) )
或者您是否希望避免在有“其他(较长)”记录的情况下删除SQL|SQL
,例如。 SQL|Strange Things Happen
?
重读这个问题我现在有疑问,如果你真的想要DELETE
这些记录,或者只是希望通过过滤掉的bespoken记录来SELECT
。在后一种情况下,你必须使用(包括mellamokb的建议)
SELECT [acronym], [word]
FROM myTable mt
WHERE [acronym] <> [word]
OR NOT EXISTS ( SELECT *
FROM myTable lw
WHERE lw.[acronym] = mt.[acronym]
AND lw.[word] <> lw.[acronym] )
答案 1 :(得分:0)
根据您的数据,您可以使用Distinct和Case语句的组合
select distinct acronym,
(case acronym when word then word else acronym end) as Abbr
from acronyms
答案 2 :(得分:0)
试试这个:
create table #t (acronym varchar(100), word varchar(100));
go
insert #t values
('FCN', 'FCN'),
('FCN', 'Fourth Corner Neurosurgical Associates'),
('FHS', 'FHS'),
('HW', 'HW');
go
;with x as (
select *,
row_number() over(partition by acronym order by case when acronym=word then 1 else 0 end) as rn
from #t
)
delete x where rn <> 1;