Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将面向列的数据转换为面向行的数据_Php_Sql_Mariadb_Pivot Table - Fatal编程技术网

Php 将面向列的数据转换为面向行的数据

Php 将面向列的数据转换为面向行的数据,php,sql,mariadb,pivot-table,Php,Sql,Mariadb,Pivot Table,我的表中有此架构下的数据 AVG_团队字段为AVGAVG_OK,其中团队ID=团队ID,月份=月份 有些人希望在一个月内跟踪每个司机的发展。预期模式为: DRIVER_ID | TEAM_ID | AVG_OK_MONTH1 | AVG_OK_MONTH2 | AVG_OK_MONTH3 | ... | AVG_OK_MONTH12 | GLOBAL_AVG | TEAM_GLOBAL_AVG 005 XXXX 81

我的表中有此架构下的数据 AVG_团队字段为AVGAVG_OK,其中团队ID=团队ID,月份=月份

有些人希望在一个月内跟踪每个司机的发展。预期模式为:

DRIVER_ID | TEAM_ID  | AVG_OK_MONTH1  |  AVG_OK_MONTH2  |  AVG_OK_MONTH3  |   ...  |  AVG_OK_MONTH12  | GLOBAL_AVG  |  TEAM_GLOBAL_AVG
    005       XXXX          81                84               81             ...          NULL               82            ?
    070       RRRR          77                80               NULL           ...          NULL              78.5           ?
etc
正如您很可能看到的,这种方法已经存在一个很大的缺陷,因为团队ID可能会随着月份的变化而变化,因此AVG_团队字段无法用于轻松计算团队全局平均字段

但是,我们把这个瑕疵去掉,认为团队不会改变。 我看不到一种简单的方法可以将第一个模式转换为另一个模式,但是使用SQL或PHP却从来没有这样做过


我有一些非常丑陋的解决方案,在PHP中使用数组,但必须有更好更简单的方法?欢迎任何见解。

我认为您可以使用条件聚合。假设该月为日期:

select driver_id,
       max(case when month(month) = 1 then avg_ok end) as avg_ok_1,
       max(case when month(month) = 2 then avg_ok end) as avg_ok_2,
       max(case when month(month) = 3 then avg_ok end) as avg_ok_3,
       max(case when month(month) = 4 then avg_ok end) as avg_ok_4
from t
where month >= '2019-01-01' and month < '2020-01-01'
group by driver_id;
否则,您可以使用字符串或日期函数执行类似操作:

select driver_id,
       max(case when right(month, 2) = '01' then avg_ok end) as avg_ok_1,
       max(case when right(month, 2) = '02' then avg_ok end) as avg_ok_2,
       max(case when right(month, 2) = '03' then avg_ok end) as avg_ok_3,
       max(case when right(month, 2) = '04' then avg_ok end) as avg_ok_4
from t
where month >= '201901' and month < '202001'
group by driver_id;

这在PHP中可能比在SQL中要容易得多…这被称为pivot-follow the tag.Month,正如前面提到的,格式为YYYYMM的varchar。。我会尝试第二种解决方案并返回给你们,谢谢你们,非常感谢定时器让我接受
select driver_id,
       max(case when right(month, 2) = '01' then avg_ok end) as avg_ok_1,
       max(case when right(month, 2) = '02' then avg_ok end) as avg_ok_2,
       max(case when right(month, 2) = '03' then avg_ok end) as avg_ok_3,
       max(case when right(month, 2) = '04' then avg_ok end) as avg_ok_4
from t
where month >= '201901' and month < '202001'
group by driver_id;