T-SQL :计算列中每个唯一子字符串的出现次数
T-SQL : count occurrences per unique substring in a column
我想计算列中每个唯一子字符串的出现次数。
SELECT DISTINCT LEFT(code, 3)
FROM table-with-codes
输出:
code
------------------
VJCrandomthings
PASrandomthings
CAArandomthings
PASrandomthings2
PASrandomthings3
预期输出:
caa 1
pas 3
vjc 1
我试过了
SELECT COUNT(DISTINCT LEFT(code, 3))
FROM table-with-codes
但结果是 returns 3。
您需要 count
和 group by
select Left(code,3), Count(*)
from [table-with-codes]
group by Left(code,3)
您使用 left
是正确的,但您应该 group by
而不是 distinct
declare @table_with_codes table (code varchar(50))
insert into @table_with_codes values ('VJCrandomthings'), ('PASrandomthings'), ('CAArandomthings'), ('PASrandomthings2'), ('PASrandomthings3')
select left(code, 3) as code_3,
count(*) as cnt
from @table_with_codes
group by left(code, 3)
这是如何工作的
通过使用 group by 你可以计算所有被分组的行(因此只在结果中返回一次),在这种情况下,所有在 lef(col, 3)
中具有相同值的行
结果会是
code_3 cnt
------ ---
CAA 1
PAS 3
VJC 1
你应该保留select
语句和Group by
中提取的3个字母。
查询
Select left(code, 3) as code3, count(left(code, 3)) as cnt
From tablename
Group by left(code, 3)
Order by 1;
我不喜欢 LEFT(code, 3) 在查询中出现不止一次。为了提高可维护性和可读性,我将使用 CROSS APPLY 重构它,如下所示:
SELECT subcode, COUNT(*)
FROM table-with-codes
CROSS APPLY (SELECT LEFT(code, 3)) AS T(subcode)
GROUP BY subcode
您可以使用 Common Transaction Expression 来避免两次提及 left 函数:
create table [table-with-codes] (code nvarchar(100))
go
insert into [table-with-codes] values('VJCrandomthings'),('PASrandomthings'),('CAArandomthings'),('PASrandomthings2'),('PASrandomthings3');
with cte as (select LEFT(code,3) mycode from [table-with-codes])
select mycode,count(1) N from cte group by mycode
我想计算列中每个唯一子字符串的出现次数。
SELECT DISTINCT LEFT(code, 3)
FROM table-with-codes
输出:
code
------------------
VJCrandomthings
PASrandomthings
CAArandomthings
PASrandomthings2
PASrandomthings3
预期输出:
caa 1
pas 3
vjc 1
我试过了
SELECT COUNT(DISTINCT LEFT(code, 3))
FROM table-with-codes
但结果是 returns 3。
您需要 count
和 group by
select Left(code,3), Count(*)
from [table-with-codes]
group by Left(code,3)
您使用 left
是正确的,但您应该 group by
distinct
declare @table_with_codes table (code varchar(50))
insert into @table_with_codes values ('VJCrandomthings'), ('PASrandomthings'), ('CAArandomthings'), ('PASrandomthings2'), ('PASrandomthings3')
select left(code, 3) as code_3,
count(*) as cnt
from @table_with_codes
group by left(code, 3)
这是如何工作的
通过使用 group by 你可以计算所有被分组的行(因此只在结果中返回一次),在这种情况下,所有在 lef(col, 3)
结果会是
code_3 cnt
------ ---
CAA 1
PAS 3
VJC 1
你应该保留select
语句和Group by
中提取的3个字母。
查询
Select left(code, 3) as code3, count(left(code, 3)) as cnt
From tablename
Group by left(code, 3)
Order by 1;
我不喜欢 LEFT(code, 3) 在查询中出现不止一次。为了提高可维护性和可读性,我将使用 CROSS APPLY 重构它,如下所示:
SELECT subcode, COUNT(*)
FROM table-with-codes
CROSS APPLY (SELECT LEFT(code, 3)) AS T(subcode)
GROUP BY subcode
您可以使用 Common Transaction Expression 来避免两次提及 left 函数:
create table [table-with-codes] (code nvarchar(100))
go
insert into [table-with-codes] values('VJCrandomthings'),('PASrandomthings'),('CAArandomthings'),('PASrandomthings2'),('PASrandomthings3');
with cte as (select LEFT(code,3) mycode from [table-with-codes])
select mycode,count(1) N from cte group by mycode