[英]SQL query to extract field values from rows sharing same value in specific column, and combine those value into a single row
[英]Postgres query to combine multiple rows with same value of a column into a single row
WITH A as (SELECT id, a FROM XXX WHERE a is not null),
B as (SELECT id, b FROM XXX WHERE b is not null)
SELECT A.a, B.b, A.id FROM A
INNER JOIN B on A.id = B.id;
对于这个数据集,简单的聚合可以做你想做的事:
select min(a) a, min(b) b, id
from mytable
group by id
这利用了聚合函数忽略null
值的事实; 我们可以使用max()
获得与使用min()
完全相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.