sqlite 多列多行转置到单行的用法
1、sqlite:group_concat()多列
参考https://qa.1r1g.com/sf/ask/685814741/
SELECT
step,
group_concat(concat) AS formated
FROM (
SELECT
step,
CASE
WHEN intvl IS NULL THEN moverno || ',' || position || ',0'
ELSE moverno || ',' || position || ',' || intvl
END AS concat
FROM processdetail2 order by moverno
)
GROUP BY step
取出结果:
step formated
1 90,0.1,60,0,30,0.2
2 600,0.3,300,0.1,150,0.2
3 900,0,600,0,200,0
2、但group_concat命令是随机的,如何让此命令按次序连接?
可参考https://qa.1r1g.com/sf/ask/132814671/
3、如何在SQLite中转置表格?
参考https://www.coder.work/article/6246663
select step,
max(case when seq = 1 then moverno end) as 'NO001',
max(case when seq = 1 then position end) as 'LOC001',
max(case when seq = 1 then intvl end) as 'ITV001',
max(case when seq = 2 then moverno end) as 'NO002',
max(case when seq = 2 then position end) as 'LOC002',
max(case when seq = 2 then intvl end) as 'ITV002',
max(case when seq = 3 then moverno end) as 'NO003',
max(case when seq = 3 then position end) as 'LOC003',
max(case when seq = 3 then intvl end) as 'ITV003'
from (select t.*,row_number() over (partition by step order by moverno) as seq from processdetail2 t
) t
group by step;
运行结果正确,但只有三列,多列呢,如何使用循环?
step NO001 LOC001 ITV001 NO002 LOC002 ITV002 NO003 LOC003 ITV003
1 1 90 0.1 2 60 3 30 0.2
2 1 600 0.3 2 300 0.1 3 150 0.2
3 1 900 2 600 3 200