我可以 "GROUP BY" Access/SQL 中的部分字段值吗?
Can I "GROUP BY" a partial field value in Access/SQL?
在Access/SQL中,我知道我可以GROUP BY
,比如说,country
或age
。
我是否也可以 GROUP BY
部分值,例如,字段以 "G"
或 "18"
开头,这样 "Germany"
和 "Ghana"
是在一组中,"1897"
和"1870"
在一组中?
您可以将 MID()
或 LEFT()
与 GROUP BY
子句一起使用:
select mid(country,1,1), count(*)
from table t
group by mid(country,1,1); -- or with left (country,1)
您可以按任何非聚合表达式进行分组。例如:
SELECT LEFT(country, 1), COUNT(*)
FROM mytable
GROUP BY LEFT(country, 1)
select group_concat(country), count(*) as count
from employees
group by left(country,1)
我认为这应该可以解决
答案看起来像
India, Indonesia 2
America, Australia, Argentina 3
在Access/SQL中,我知道我可以GROUP BY
,比如说,country
或age
。
我是否也可以 GROUP BY
部分值,例如,字段以 "G"
或 "18"
开头,这样 "Germany"
和 "Ghana"
是在一组中,"1897"
和"1870"
在一组中?
您可以将 MID()
或 LEFT()
与 GROUP BY
子句一起使用:
select mid(country,1,1), count(*)
from table t
group by mid(country,1,1); -- or with left (country,1)
您可以按任何非聚合表达式进行分组。例如:
SELECT LEFT(country, 1), COUNT(*)
FROM mytable
GROUP BY LEFT(country, 1)
select group_concat(country), count(*) as count
from employees
group by left(country,1)
我认为这应该可以解决
答案看起来像
India, Indonesia 2
America, Australia, Argentina 3