Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql-比较子字符串而不是整个字符串_Sql_Sql Server_Tsql - Fatal编程技术网

sql-比较子字符串而不是整个字符串

sql-比较子字符串而不是整个字符串,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图计算表中每个子字符串的数量,出于某种原因,它会计算整个字符串。我做错了什么?试试: select substring(datapath,1,5), COUNT(substring(datapath,1,5)) from batchinfo where datapath like '%thc%' group by datapath having COUNT(substring(datapath,1,5))>1 您只需按您试图计数的子字符串分组,而不是按完整的数据路径分组。也不需要对计

我试图计算表中每个子字符串的数量,出于某种原因,它会计算整个字符串。我做错了什么?

试试:

select substring(datapath,1,5), COUNT(substring(datapath,1,5)) from batchinfo
where datapath like '%thc%'
group by datapath
having COUNT(substring(datapath,1,5))>1

您只需按您试图计数的子字符串分组,而不是按完整的数据路径分组。也不需要对计数重复substring函数

select d, count(*)
from
(
    select substring(datapath,1,5) d from batchinfo
    where datapath like '%thc%'
)
group by d
having COUNT(*) > 1

你能提供一个简单的例子吗?我喜欢这个,因为你没有重复子字符串
select substring(datapath,1,5), COUNT(*) 
    from batchinfo
    where datapath like '%thc%'
    group by substring(datapath,1,5)
    having COUNT(*)>1