我希望对SQL计数查询的结果数进行计数,当前结果有50个结果,但实际上只有5个结果...结果以如下格式显示:
test1-helpme1
test1-helpme3
test1-helpme4
test2-helpme1
test2-helpme2
test3-helpme4
有没有一种方法可以只计算结果的“ testx-”部分? 可能有数百个结果,所以“测试”的数字部分不能硬编码
SELECT COUNT(*) as CountbyID, OriginalId FROM Table1 GROUP BY OriginalId;
答案 0 :(得分:2)
如果您想要模式的不同出现次数,则:
SELECT
COUNT(DISTINCT LEFT(OriginalId, INSTR(OriginalId, '-') - 1)) as counter
FROM Table1
或每个计数器:
SELECT
LEFT(OriginalId, INSTR(OriginalId, '-') - 1) pattern,
COUNT(*) as counter
FROM Table1
GROUP BY LEFT(OriginalId, INSTR(OriginalId, '-') - 1)
答案 1 :(得分:0)
是的,您可以使用LEFT对字符串的子集进行分组:
declare @test as table (test varchar(100))
insert into @test
Select 'test1-helpme1' UNION ALL
Select 'test1-helpme3' UNION ALL
Select 'test1-helpme4' UNION ALL
Select 'test2-helpme1' UNION ALL
Select 'test2-helpme4'
Select count(*) as countbyID, left(test,5) as originalid from @test group by left(test,5)