如何在postgresql中转置表值?
如何转换这些值以获取此表: 让它看起来像这样:如何在postgresql中转置表值?,sql,postgresql,pivot,crosstab,Sql,Postgresql,Pivot,Crosstab,如何转换这些值以获取此表: 让它看起来像这样: postgresql中没有可以使用的Pivot函数,每次我尝试交叉表函数时,代码中都会出现错误。让我们假设我的表名为select*fromtattributes使用条件聚合,它在Postgres中的意思是过滤: select id, max(attributenumber) filter (where attributeid = 2000), max(attributenumber) filter (where at
postgresql中没有可以使用的Pivot函数,每次我尝试交叉表函数时,代码中都会出现错误。让我们假设我的表名为select*fromtattributes使用条件聚合,它在Postgres中的意思是过滤:
select id,
max(attributenumber) filter (where attributeid = 2000),
max(attributenumber) filter (where attributeid = 2001),
max(attributenumber) filter (where attributeid = 2002),
max(attributenumber) filter (where attributeid = 2003)
from t
group by id;
是一把小提琴。试试这个:
SELECT id,
MAX((SELECT attributenumber FROM public.t WHERE ext_t.id = id AND ext_t.attribuiteid = attribuiteid AND attribuiteid = 2000)) AS '2000',
MAX((SELECT attributenumber FROM public.t WHERE ext_t.id = id AND ext_t.attribuiteid = attribuiteid AND attribuiteid = 2001)) AS '2001',
MAX((SELECT attributenumber FROM public.t WHERE ext_t.id = id AND ext_t.attribuiteid = attribuiteid AND attribuiteid = 2002)) AS '2002',
MAX((SELECT attributenumber FROM public.t WHERE ext_t.id = id AND ext_t.attribuiteid = attribuiteid AND attribuiteid = 2003)) AS '2003'
FROM public.t ext_t GROUP BY id
这回答了你的问题吗?不幸的是,这只给了我空值throughout@oaker . . . 我加了一把小提琴。代码可以工作并回答您的问题。看起来它工作了!我可以对日期也这样做吗?例如,max(attributedate)filter(其中attributeid=2007),等等。我正在尝试,但它给了我一个错误:sytanx ERROR在或接近“max”是的,您可以使用日期
MAX()
适用于几乎所有的数据类型。