mysql-行到列

x33g5p2x  于 2021-07-24  发布在  Java
关注(0)|答案(12)|浏览(275)

我试图搜索帖子,但只找到了针对sql server/access的解决方案。我需要一个mysql(5.x)的解决方案。
我有一个表(称为history),有3列:hostid、itemname、itemvalue。
如果我选择( select * from history ),它会回来的

+--------+----------+-----------+
   | hostid | itemname | itemvalue |
   +--------+----------+-----------+
   |   1    |    A     |    10     |
   +--------+----------+-----------+
   |   1    |    B     |     3     |
   +--------+----------+-----------+
   |   2    |    A     |     9     |
   +--------+----------+-----------+
   |   2    |    c     |    40     |
   +--------+----------+-----------+

如何查询数据库以返回

+--------+------+-----+-----+
   | hostid |   A  |  B  |  C  |
   +--------+------+-----+-----+
   |   1    |  10  |  3  |  0  |
   +--------+------+-----+-----+
   |   2    |   9  |  0  |  40 |
   +--------+------+-----+-----+
ssm49v7z

ssm49v7z1#

我将对解决这个问题所要采取的步骤做一个更长更详细的解释。如果时间太长,我道歉。
我将从你给出的基础开始,用它来定义几个术语,我将在本文的其余部分使用这些术语。这将是基表:

select * from history;

+--------+----------+-----------+
| hostid | itemname | itemvalue |
+--------+----------+-----------+
|      1 | A        |        10 |
|      1 | B        |         3 |
|      2 | A        |         9 |
|      2 | C        |        40 |
+--------+----------+-----------+

这就是我们的目标,漂亮的透视表:

select * from history_itemvalue_pivot;

+--------+------+------+------+
| hostid | A    | B    | C    |
+--------+------+------+------+
|      1 |   10 |    3 |    0 |
|      2 |    9 |    0 |   40 |
+--------+------+------+------+

中的值 history.hostid 列将成为透视表中的y值。中的值 history.itemname 列将成为x值(原因很明显)。
当我必须解决创建透视表的问题时,我使用三步流程(可选的第四步)来解决它:
选择感兴趣的列,即y值和x值
用额外的列扩展基表——每个x值一列
分组并聚合扩展表——每个y值一个组
(可选)美化聚合表
让我们将这些步骤应用于您的问题,看看我们得到了什么:
步骤1:选择感兴趣的列。在期望的结果中, hostid 提供y值和 itemname 提供x值。
步骤2:用额外的列扩展基表。通常每个x值需要一列。回想一下,我们的x值列是 itemname :

create view history_extended as (
  select
    history.*,
    case when itemname = "A" then itemvalue end as A,
    case when itemname = "B" then itemvalue end as B,
    case when itemname = "C" then itemvalue end as C
  from history
);

select * from history_extended;

+--------+----------+-----------+------+------+------+
| hostid | itemname | itemvalue | A    | B    | C    |
+--------+----------+-----------+------+------+------+
|      1 | A        |        10 |   10 | NULL | NULL |
|      1 | B        |         3 | NULL |    3 | NULL |
|      2 | A        |         9 |    9 | NULL | NULL |
|      2 | C        |        40 | NULL | NULL |   40 |
+--------+----------+-----------+------+------+------+

请注意,我们没有更改行数--只是添加了额外的列。还要注意 NULL s—与 itemname = "A" 新列的值为非空 A ,以及其他新列的空值。
步骤3:对扩展表进行分组和聚合。我们需要 group by hostid ,因为它提供y值:

create view history_itemvalue_pivot as (
  select
    hostid,
    sum(A) as A,
    sum(B) as B,
    sum(C) as C
  from history_extended
  group by hostid
);

select * from history_itemvalue_pivot;

+--------+------+------+------+
| hostid | A    | B    | C    |
+--------+------+------+------+
|      1 |   10 |    3 | NULL |
|      2 |    9 | NULL |   40 |
+--------+------+------+------+

(请注意,现在每个y值有一行。)好的,我们就快到了!我们只需要除掉那些丑陋的东西 NULL s。
第四步:美化。我们只是将所有空值替换为零,这样结果集更易于查看:

create view history_itemvalue_pivot_pretty as (
  select 
    hostid, 
    coalesce(A, 0) as A, 
    coalesce(B, 0) as B, 
    coalesce(C, 0) as C 
  from history_itemvalue_pivot 
);

select * from history_itemvalue_pivot_pretty;

+--------+------+------+------+
| hostid | A    | B    | C    |
+--------+------+------+------+
|      1 |   10 |    3 |    0 |
|      2 |    9 |    0 |   40 |
+--------+------+------+------+

我们完成了——我们用mysql构建了一个漂亮的透视表。
应用此程序时的注意事项:
在额外列中使用什么值。我曾经 itemvalue 在这个例子中
在额外的列中使用什么“中性”值。我曾经 NULL ,但也可能是 0 或者 "" ,取决于你的具体情况
分组时要使用的聚合函数。我曾经 sum ,但是 count 以及 max 也经常使用( max 通常在构建一行“对象”时使用(对象已分布在多行中)
对y值使用多列。这个解决方案并不局限于对y值使用单个列——只需将额外的列插入到 group by 条款(别忘了 select (他们)
已知限制:
此解决方案不允许透视表中有n列—在扩展基表时,需要手动添加每个透视列。所以对于5或10个x值,这个解决方案很好。100块,不太好。有些解决方案使用存储过程生成查询,但它们很难看,而且很难获得正确的结果。当透视表需要有很多列时,我目前还不知道有什么好方法来解决这个问题。

ruyhziif

ruyhziif2#

SELECT 
    hostid, 
    sum( if( itemname = 'A', itemvalue, 0 ) ) AS A,  
    sum( if( itemname = 'B', itemvalue, 0 ) ) AS B, 
    sum( if( itemname = 'C', itemvalue, 0 ) ) AS C 
FROM 
    bob 
GROUP BY 
    hostid;
xkftehaa

xkftehaa3#

另一个选择,如果您有许多需要透视的项,那么让mysql为您构建查询尤其有用:

SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'ifnull(SUM(case when itemname = ''',
      itemname,
      ''' then itemvalue end),0) AS `',
      itemname, '`'
    )
  ) INTO @sql
FROM
  history;
SET @sql = CONCAT('SELECT hostid, ', @sql, ' 
                  FROM history 
                   GROUP BY hostid');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

fiddle添加了一些额外的值来查看它的工作情况 GROUP_CONCAT 有一个默认值1000,所以如果您有一个非常大的查询,请在运行它之前更改此参数

SET SESSION group_concat_max_len = 1000000;

测试:

DROP TABLE IF EXISTS history;
CREATE TABLE history
(hostid INT,
itemname VARCHAR(5),
itemvalue INT);

INSERT INTO history VALUES(1,'A',10),(1,'B',3),(2,'A',9),
(2,'C',40),(2,'D',5),
(3,'A',14),(3,'B',67),(3,'D',8);

  hostid    A     B     C      D
    1     10      3     0      0
    2     9       0    40      5
    3     14     67     0      8
niwlg2el

niwlg2el4#

利用matt fenwick帮助我解决问题的想法(非常感谢),让我们将其简化为一个查询:

select
    history.*,
    coalesce(sum(case when itemname = "A" then itemvalue end), 0) as A,
    coalesce(sum(case when itemname = "B" then itemvalue end), 0) as B,
    coalesce(sum(case when itemname = "C" then itemvalue end), 0) as C
from history
group by hostid
nkhmeac6

nkhmeac65#

我从子查询中编辑agung sagita的答案以加入。我不确定这两种方式之间有多大的区别,但仅供参考。

SELECT  hostid, T2.VALUE AS A, T3.VALUE AS B, T4.VALUE AS C
FROM TableTest AS T1
LEFT JOIN TableTest T2 ON T2.hostid=T1.hostid AND T2.ITEMNAME='A'
LEFT JOIN TableTest T3 ON T3.hostid=T1.hostid AND T3.ITEMNAME='B'
LEFT JOIN TableTest T4 ON T4.hostid=T1.hostid AND T4.ITEMNAME='C'
gywdnpxw

gywdnpxw6#

使用子查询

SELECT  hostid, 
    (SELECT VALUE FROM TableTest WHERE ITEMNAME='A' AND hostid = t1.hostid) AS A,
    (SELECT VALUE FROM TableTest WHERE ITEMNAME='B' AND hostid = t1.hostid) AS B,
    (SELECT VALUE FROM TableTest WHERE ITEMNAME='C' AND hostid = t1.hostid) AS C
FROM TableTest AS T1
GROUP BY hostid

但如果子查询产生的结果多于一行,则会出现问题,请在子查询中使用进一步的聚合函数

rvpgvaaj

rvpgvaaj7#

我的解决方案:

select h.hostid, sum(ifnull(h.A,0)) as A, sum(ifnull(h.B,0)) as B, sum(ifnull(h.C,0)) as  C from (
select
hostid,
case when itemName = 'A' then itemvalue end as A,
case when itemName = 'B' then itemvalue end as B,
case when itemName = 'C' then itemvalue end as C
  from history 
) h group by hostid

它在提交的案例中产生预期的结果。

8ehkhllq

8ehkhllq8#

我找到了一种方法,可以使用简单的查询使报表的行到列的转换几乎是动态的。你可以在这里在线查看和测试。
查询的列数是固定的,但值是动态的,并且基于行的值。您可以构建它,因此,我使用一个查询构建表头,另一个查询查看值:

SELECT distinct concat('<th>',itemname,'</th>') as column_name_table_header FROM history order by 1;

SELECT
     hostid
    ,(case when itemname = (select distinct itemname from history a order by 1 limit 0,1) then itemvalue else '' end) as col1
    ,(case when itemname = (select distinct itemname from history a order by 1 limit 1,1) then itemvalue else '' end) as col2
    ,(case when itemname = (select distinct itemname from history a order by 1 limit 2,1) then itemvalue else '' end) as col3
    ,(case when itemname = (select distinct itemname from history a order by 1 limit 3,1) then itemvalue else '' end) as col4
FROM history order by 1;

你也可以总结一下:

SELECT
     hostid
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 0,1) then itemvalue end) as A
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 1,1) then itemvalue end) as B
    ,sum(case when itemname = (select distinct itemname from history a order by 1 limit 2,1) then itemvalue end) as C
FROM history group by hostid order by 1;
+--------+------+------+------+
| hostid | A    | B    | C    |
+--------+------+------+------+
|      1 |   10 |    3 | NULL |
|      2 |    9 | NULL |   40 |
+--------+------+------+------+

测试结果:

http://rextester.com/zswks28923
作为一个实际使用的例子,本报告在下面的列中显示了船/巴士的出发时间和到达时间,并提供了一个可视化的时间表。您将看到最后一列中未使用的另一列,而不会混淆可视化效果:

**网上售票系统

5ssjco0h

5ssjco0h9#

我把它变成 Group By hostId 然后它将只显示第一行的值,
比如:

A   B  C
1  10
2      3
rn0zuynd

rn0zuynd10#

如果你能用mariadb有一个非常简单的解决方案。
自从mariadb-10.02以来,添加了一个名为connect的新存储引擎,它可以帮助我们将另一个查询或表的结果转换为pivot表,就像您想要的那样:您可以查看文档。
首先安装连接存储引擎。
现在表的透视列是 itemname 每个项目的数据位于 itemvalue 列,因此我们可以使用此查询获得结果透视表:

create table pivot_table
engine=connect table_type=pivot tabname=history
option_list='PivotCol=itemname,FncCol=itemvalue';

现在我们可以从 pivot_table :

select * from pivot_table

更多详情请点击此处

2w2cym1i

2w2cym1i11#

这不是确切的答案,你正在寻找,但这是一个解决方案,我需要在我的项目,希望这有助于某人。这将列出由逗号分隔的1到n行项目。在mysql中,groupconcat使这成为可能。

select
cemetery.cemetery_id as "Cemetery_ID",
GROUP_CONCAT(distinct(names.name)) as "Cemetery_Name",
cemetery.latitude as Latitude,
cemetery.longitude as Longitude,
c.Contact_Info,
d.Direction_Type,
d.Directions

    from cemetery
    left join cemetery_names on cemetery.cemetery_id = cemetery_names.cemetery_id 
    left join names on cemetery_names.name_id = names.name_id 
    left join cemetery_contact on cemetery.cemetery_id = cemetery_contact.cemetery_id 

    left join 
    (
        select 
            cemetery_contact.cemetery_id as cID,
            group_concat(contacts.name, char(32), phone.number) as Contact_Info

                from cemetery_contact
                left join contacts on cemetery_contact.contact_id = contacts.contact_id 
                left join phone on cemetery_contact.contact_id = phone.contact_id 

            group by cID
    )
    as c on c.cID = cemetery.cemetery_id

    left join
    (
        select 
            cemetery_id as dID, 
            group_concat(direction_type.direction_type) as Direction_Type,
            group_concat(directions.value , char(13), char(9)) as Directions

                from directions
                left join direction_type on directions.type = direction_type.direction_type_id

            group by dID

    )
    as d on d.dID  = cemetery.cemetery_id

group by Cemetery_ID

这个公墓有两个公共名称,因此名称列在不同的行中,由一个id连接,但有两个名称id,查询产生如下结果
    墓地ID    墓地名称            纬度
    1                   阿普尔顿,萨弗斯普林斯  35.4276242832293

6tr1vspr

6tr1vspr12#

我很抱歉这么说,也许我没有完全解决你的问题,但postgresql比mysql早10年,与mysql相比非常先进,有很多方法可以轻松实现这一点。安装postgresql并执行此查询

CREATE EXTENSION tablefunc;

那你瞧!这里有大量的文档:postgresql:documentation:9.1:tablefunc或这个查询

CREATE EXTENSION hstore;

再说一遍,瞧!postgresql:文档:9.0:hs存储

相关问题