使用 SQL 中另一列的所有唯一值创建新列
Create new column with all unique values from another column in SQL
我有以下 table:
>>> id crop grower loc
0 11 maize Lulu Fiksi
1 13 maize Lulu Menter
2 05 maize Felix Hausbauch
3 04 apples Lulu Fiksi
4 02 apples Meni Linter
5 06 cotton Delina Marchi
6 12 cotton Lexi Tinta
7 16 cotton Lexi Ferta
...
我想创建新的 table,它将显示唯一的作物名称、作物出现次数以及种植该作物的所有种植者的列表,因此结果 table 应该看起来像像这样:
>>> crop total_count growers
0 maize 3 Lulu, Felix
1 apples 2 Lulu,Meni
2 cotton 3 Delina, Lexi
我设法创建了 table 来显示作物和总数,但没有种植者的名字:
select "CROP",count(*) "totalCount"
from "table"
group by "CROP"
order by "totalCount" desc
我的问题是如何使用新列创建新的 table,其中包含每种作物的独特种植者列表(如示例中所示)。
您可以根据您的数据库使用 GROUP_CONCAT() 或任何相关的乐趣
select "CROP",count(*) "totalCount",GROUP_CONCAT(grower) as growers
from "table"
group by "CROP"
order by "totalCount" desc
GROUP_CONCAT用于MySQL,Snowflake使用LISTAGG:
create or replace table test (
id int,
crop varchar,
grower varchar,
loc varchar
);
insert into test values
(11, 'maize', 'Lulu', 'Fiksi'),
(13, 'maize', 'Lulu', 'Menter'),
(5, 'maize', 'Felix', 'Hausbauch'),
(4, 'apples', 'Lulu', 'Fiksi'),
(2, 'apples', 'Meni', 'Linter'),
(6, 'cotton', 'Delina', 'Marchi'),
(12, 'cotton', 'Lexi', 'Tinta'),
(16, 'cotton', 'Lexi', 'Ferta');
select
crop,
count(1) as total_count,
listagg(distinct grower, ', ') as growers
from test
group by crop
;
+--------+-------------+--------------+
| CROP | TOTAL_COUNT | GROWERS |
|--------+-------------+--------------|
| maize | 3 | Lulu, Felix |
| apples | 2 | Lulu, Meni |
| cotton | 3 | Delina, Lexi |
+--------+-------------+--------------+
我有以下 table:
>>> id crop grower loc
0 11 maize Lulu Fiksi
1 13 maize Lulu Menter
2 05 maize Felix Hausbauch
3 04 apples Lulu Fiksi
4 02 apples Meni Linter
5 06 cotton Delina Marchi
6 12 cotton Lexi Tinta
7 16 cotton Lexi Ferta
...
我想创建新的 table,它将显示唯一的作物名称、作物出现次数以及种植该作物的所有种植者的列表,因此结果 table 应该看起来像像这样:
>>> crop total_count growers
0 maize 3 Lulu, Felix
1 apples 2 Lulu,Meni
2 cotton 3 Delina, Lexi
我设法创建了 table 来显示作物和总数,但没有种植者的名字:
select "CROP",count(*) "totalCount"
from "table"
group by "CROP"
order by "totalCount" desc
我的问题是如何使用新列创建新的 table,其中包含每种作物的独特种植者列表(如示例中所示)。
您可以根据您的数据库使用 GROUP_CONCAT() 或任何相关的乐趣
select "CROP",count(*) "totalCount",GROUP_CONCAT(grower) as growers
from "table"
group by "CROP"
order by "totalCount" desc
GROUP_CONCAT用于MySQL,Snowflake使用LISTAGG:
create or replace table test (
id int,
crop varchar,
grower varchar,
loc varchar
);
insert into test values
(11, 'maize', 'Lulu', 'Fiksi'),
(13, 'maize', 'Lulu', 'Menter'),
(5, 'maize', 'Felix', 'Hausbauch'),
(4, 'apples', 'Lulu', 'Fiksi'),
(2, 'apples', 'Meni', 'Linter'),
(6, 'cotton', 'Delina', 'Marchi'),
(12, 'cotton', 'Lexi', 'Tinta'),
(16, 'cotton', 'Lexi', 'Ferta');
select
crop,
count(1) as total_count,
listagg(distinct grower, ', ') as growers
from test
group by crop
;
+--------+-------------+--------------+
| CROP | TOTAL_COUNT | GROWERS |
|--------+-------------+--------------|
| maize | 3 | Lulu, Felix |
| apples | 2 | Lulu, Meni |
| cotton | 3 | Delina, Lexi |
+--------+-------------+--------------+