假设我没有名为Animals的用户友好表。 像这样:
ID NAME CHANGED
1 Dog 2016-01-02
2 Dog 2016-01-05
3 Dog2 2016-01-05
4 Dog3 2016-01-06
5 Cat 2016-01-06
6 Fish 2016-01-07
7 Cat2 2016-01-07
我想知道表中有多少相似的单词以相同的方式开始,但可能会以不同的方式结束。此外,我只想显示没有额外'2'或'3'的姓名。更确切地说,我想要这样的结果:
Counted NAME
4 Dog
2 Cat
1 Fish
以下是我一直试图与之相处的代码(尝试了很多方法):
SELECT COUNT(
CASE
WHEN NAME LIKE '%' + NAME + '%' THEN 1
ELSE 0
END
) AS Counted, NAME FROM Animals
WHERE NAME LIKE '%' + NAME + '%' AND
NAME NOT LIKE '%2' AND
NAME NOT LIKE '%3'
GROUP BY NAME
ORDER BY Counted DESC
答案 0 :(得分:1)
繁琐但简单的方法是从数字字符中删除数据:
select new_name,
count(*) as counted
from (
select replace(replace(replace....replace(Name, '0', ''), '1', ''), '2','')... as new_name
from Animals
) as a
group by new_name
答案 1 :(得分:1)
select case when patIndex('%[0-9]', val) = 0 then val
else substring(val, 1, patIndex('%[0-9]', val)-1) end, count(*)
from table
Group by case when patIndex('%[0-9]', val) = 0 then val
else substring(val, 1, patIndex('%[0-9]', val)-1) end
答案 2 :(得分:0)
你必须在单独的表中定义动物类别,这将永远无法解决。对于同一动物的拼写不同,分组将非常困难。
如果它始终是动物名称末尾的数字,那么
select left(Name,isnull(nullif(patindex('%[0-9]%',Name)-1,-1),len(Name))),count(1)
From yourtable
Group by left(Name,isnull(nullif(patindex('%[0-9]%',Name)-1,-1),len(Name)))
答案 3 :(得分:0)
这将删除数据中的所有数字,无论数据中有多少数字或数据存在于何处。这有点荒谬,但有时SQL只是......
declare @t table(ID int, Name nvarchar(10), Changed date);
insert into @t values
(1 ,'Dog','20160102')
,(2 ,'Dog','20160105')
,(3 ,'Dog2','20160105')
,(4 ,'Dog30','20160106')
,(5 ,'Cat','20160106')
,(6 ,'Fish','20160107')
,(7 ,'Cat27676','20160107');
select count(1) as Counted
,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(Name
, '0', ''),'1', ''),'2', ''),'3', ''),'4', ''),'5', ''),'6', ''),'7', ''),'8', ''),'9', '') as Name
from @t
group by replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(Name
, '0', ''),'1', ''),'2', ''),'3', ''),'4', ''),'5', ''),'6', ''),'7', ''),'8', ''),'9', '')
order by Counted desc;
输出:
+---------+------+
| Counted | Name |
+---------+------+
| 4 | Dog |
| 2 | Cat |
| 1 | Fish |
+---------+------+