将具有相似字符串的行分组
Group rows with similar strings
我搜索了很多,但大多数解决方案都是针对连接选项的,而不是我真正想要的。
我有一个名为 X
的 table(在 Postgres 数据库中):
anm_id anm_category anm_sales
1 a_dog 100
2 b_dog 50
3 c_dog 60
4 a_cat 70
5 b_cat 80
6 c_cat 40
我想通过将 'a_dog'、'b_dog'、'c_dog' 分组为狗和 'a_cat'、'b_cat'、'c_cat' 来获得总销售额作为猫。
我无法更改 table 中的数据,因为它是一个外部数据库,我只能从中获取信息。
如何使用 SQL 查询执行此操作?它不需要特定于 Postgres。
您可以按 anm_catogery
中的 substr
进行分组:
SELECT SUBSTR(anm_catogery, 3) || 's', COUNT(*)
FROM x
GROUP BY anm_catogery
使用case
语句将相同类别的动物归为一组
SELECT CASE
WHEN anm_category LIKE '%dog' THEN 'Dogs'
WHEN anm_category LIKE '%cat' THEN 'cats'
ELSE 'Others'
END AS Animals_category,
Sum(anm_sales) AS total_sales
FROM yourtables
GROUP BY CASE
WHEN anm_category LIKE '%dog' THEN 'Dogs'
WHEN anm_category LIKE '%cat' THEN 'cats'
ELSE 'Others'
END
此外,此查询应该适用于大多数数据库。
通过使用 PostgreSQL 的 split_part()
select animal||'s' animal_cat,count(*) total_sales,sum(anm_sales) sales_sum from(
select split_part(anm_cat,'_',2) animal,anm_sales from x
)t
group by animal
通过在 MySQL
中创建 split_str()
select animal||'s' animal_cat,count(*) total_sales,sum(anm_sales) sales_sum from(
select split_str(anm_cat,'_',2) animal,anm_sales from x
)t
group by animal
如果您的附录长度与示例相同:
SELECT CASE right(anm_category, 3) AS animal_type -- 3 last char
, sum(anm_sales) AS total_sales
FROM x
GROUP BY 1;
您根本不需要 CASE
语句,但如果您使用一个,请将其设为 "simple" CASE
:
- Simplify nested case when statement
使用位置引用而不是重复可能冗长的表达式。
如果长度不同,但始终有一个下划线,如示例所示:
SELECT split_part(anm_category, '_', 2) AS animal_type -- word after "_"
, sum(anm_sales) AS total_sales
FROM x
GROUP BY 1;
我搜索了很多,但大多数解决方案都是针对连接选项的,而不是我真正想要的。
我有一个名为 X
的 table(在 Postgres 数据库中):
anm_id anm_category anm_sales
1 a_dog 100
2 b_dog 50
3 c_dog 60
4 a_cat 70
5 b_cat 80
6 c_cat 40
我想通过将 'a_dog'、'b_dog'、'c_dog' 分组为狗和 'a_cat'、'b_cat'、'c_cat' 来获得总销售额作为猫。
我无法更改 table 中的数据,因为它是一个外部数据库,我只能从中获取信息。
如何使用 SQL 查询执行此操作?它不需要特定于 Postgres。
您可以按 anm_catogery
中的 substr
进行分组:
SELECT SUBSTR(anm_catogery, 3) || 's', COUNT(*)
FROM x
GROUP BY anm_catogery
使用case
语句将相同类别的动物归为一组
SELECT CASE
WHEN anm_category LIKE '%dog' THEN 'Dogs'
WHEN anm_category LIKE '%cat' THEN 'cats'
ELSE 'Others'
END AS Animals_category,
Sum(anm_sales) AS total_sales
FROM yourtables
GROUP BY CASE
WHEN anm_category LIKE '%dog' THEN 'Dogs'
WHEN anm_category LIKE '%cat' THEN 'cats'
ELSE 'Others'
END
此外,此查询应该适用于大多数数据库。
通过使用 PostgreSQL 的 split_part()
select animal||'s' animal_cat,count(*) total_sales,sum(anm_sales) sales_sum from(
select split_part(anm_cat,'_',2) animal,anm_sales from x
)t
group by animal
通过在 MySQL
中创建 split_str()select animal||'s' animal_cat,count(*) total_sales,sum(anm_sales) sales_sum from(
select split_str(anm_cat,'_',2) animal,anm_sales from x
)t
group by animal
如果您的附录长度与示例相同:
SELECT CASE right(anm_category, 3) AS animal_type -- 3 last char
, sum(anm_sales) AS total_sales
FROM x
GROUP BY 1;
您根本不需要
CASE
语句,但如果您使用一个,请将其设为 "simple"CASE
:- Simplify nested case when statement
使用位置引用而不是重复可能冗长的表达式。
如果长度不同,但始终有一个下划线,如示例所示:
SELECT split_part(anm_category, '_', 2) AS animal_type -- word after "_"
, sum(anm_sales) AS total_sales
FROM x
GROUP BY 1;