根据 SQL 上的其他常见列将行与空列合并
Combine rows with null columns based on other common columns on SQL
我是 运行 仪表盘工具的 SQL 查询,该工具给出 table 结果如下:
article_name color spec1 spec2
article1 brown 0
article2 blue 1
article2 blue 3
我的查询:
select a.name as article_name
c.clr as color
d.sp as spec1
k.sps as spec2
from table_a a
left join table_c c on a.id=c.id
left join table_d d on a.id=d.id
left join table_k k on a.id=k.id;
我想编辑该查询以获得如下结果:
article_name color spec1 spec2
article1 brown 0
article2 blue 1 3
关于如何达到该结果的任何想法?
显然未经测试,但请尝试对 spec1/2 列进行分组和聚合:
select a.name as article_name
c.clr as color
max(d.sp) as spec1
max(k.sps) as spec2
from table_a a
left join table_c c on a.id=c.id
left join table_d d on a.id=d.id
left join table_k k on a.id=k.id
group by a.name, c.clr;
我是 运行 仪表盘工具的 SQL 查询,该工具给出 table 结果如下:
article_name color spec1 spec2
article1 brown 0
article2 blue 1
article2 blue 3
我的查询:
select a.name as article_name
c.clr as color
d.sp as spec1
k.sps as spec2
from table_a a
left join table_c c on a.id=c.id
left join table_d d on a.id=d.id
left join table_k k on a.id=k.id;
我想编辑该查询以获得如下结果:
article_name color spec1 spec2
article1 brown 0
article2 blue 1 3
关于如何达到该结果的任何想法?
显然未经测试,但请尝试对 spec1/2 列进行分组和聚合:
select a.name as article_name
c.clr as color
max(d.sp) as spec1
max(k.sps) as spec2
from table_a a
left join table_c c on a.id=c.id
left join table_d d on a.id=d.id
left join table_k k on a.id=k.id
group by a.name, c.clr;