我有这个查询。
select plantnaam, plantmaat, stapel, hoeveelheid
from kist
where rij = '11'
order by stapel ASC, datum DESC
.
我想实现的是给 "Stapel "列的每个数字一个单独的列,填充 "plantnaam + plantmaat + hoeveelheid "列的信息。像这样。
能不能建立一个这样的查询?任何帮助都将是非常感激的。
如果我没有听错的话,你可以用以下方法来实现这个目标 row_number()
和条件聚合。
select
max(case when stapel = 1 then concat_ws(' ', plantnaam, plantmaat, hoeveelheid) end) col1,
max(case when stapel = 2 then concat_ws(' ', plantnaam, plantmaat, hoeveelheid) end) col2,
max(case when stapel = 3 then concat_ws(' ', plantnaam, plantmaat, hoeveelheid) end) col3,
max(case when stapel = 4 then concat_ws(' ', plantnaam, plantmaat, hoeveelheid) end) col4,
max(case when stapel = 5 then concat_ws(' ', plantnaam, plantmaat, hoeveelheid) end) col5
from (
select
t.*,
row_number() over(partition by stapel order by plantnaam, plantmaat, hoeveelheid) rn
from mytable t
) t
group by rn
২২২২২২২২২২২২২২২২২২২২২২২২২২ order by
的条款 row_number()
控制行在结果集中显示的顺序;你可能想根据你的具体要求来调整它。
你可以使用老式的maxsum(case when staple = 1 then plantnaam+' '+plantmaat+' '+hoeveelheid end)作为1,并按staple分组,然后使用 重复 操作符来多次打印行,或者像上面建议的那样,你可以使用PIVOT操作符。
假设这里的 plantmaat 列是字符串类型,其他的你必须使用转换函数将数字转换为字符串。