with CTE AS (select *,
(CASE WHEN Label='Location' THEN Val END) AS Location,
(CASE WHEN Label = 'Depth' THEN Val END) AS Depth,
(CASE WHEN Label='Date' THEN Val END) AS Dates,
ROW_NUMBER() OVER (PARTITION BY Label,Val Order By Description) as rn
from MyTable
group by Label,Description,Val )
select c.Description
,max(c.Location) AS Location
,max(c.Depth) AS Depth
,max(c.Dates) AS Dates
from cte c
where rn=1
group by c.Description
order by c.Description
select description,
max(value) filter (where label = 'location') as location,
max(value) filter (where label = 'depth') as depth,
max(value) filter (where label = 'zdate') as zdate
from table t
group by description;
4条答案
按热度按时间vwoqyblh1#
sql小提琴
postgresql 9.6架构设置:
查询1:
结果:
s4n0splo2#
一个技巧是聚合到一个json(b)字段中,并将其解包。与任何eav一样,您仍然需要处理正确的数据类型。
结果:
shstlldc3#
您可以使用tablefunc&crosstab()实现所需的结果,而不需要太复杂。为此,您需要创建下面的扩展
CREATE EXTENSION IF NOT EXISTS tablefunc;
一个select语句就可以了,演示
fdbelqdn4#
您可以不使用suq query进行条件聚合: