MARIADB 如何在没有准备好的语句的情况下将行转置为列

问题描述 投票:0回答:1

我正在尝试在 MariaDB 中将未知数量的行从查询转置到列,以获得单行。

enter image description here

我搜索了它,但大多数示例都与使用数据透视表的已知行数相关。

我想要获得的是:

enter image description here

我根据 –ysth 的请求添加此代码:

CREATE TABLE sampleDate(
dateExample varchar(50)
);

INSERT INTO sampleDate VALUES('22-01-2024(10:00)');
INSERT INTO sampleDate VALUES('23-02-2024(12:00)');
INSERT INTO sampleDate VALUES('23-04-2024(09:00)');
INSERT INTO sampleDate VALUES('23-10-2024(16:00)');

-- Wanted result
SELECT '22-01-2024(10:00)','23-02-2024(12:00)','23-04-2024(09:00)','23- 
10-2024(16:00)';
mariadb pivot-table
1个回答
0
投票

你会这样做:

set @sql:=(
  select concat(
    'select ',
    group_concat(quote(dateExample) order by str_to_date(dateExample,'%d-%m-%Y(%H:%i)'))
  ) from sampleDate);
execute immediate @sql

(作为来自客户端的两个单独请求或为您的连接启用多语句)

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.