如何减行增列实现新视图

How to reduce rows and increase column to achieve a new view

我有一个 table 与此类似:

+=========+========+============+
| Session | Center | Efficiency |
+=========+========+============+
|       1 | A1     |         55 |
+---------+--------+------------+
|       1 | A2     |         66 |
+---------+--------+------------+
|       1 | A3     |         77 |
+---------+--------+------------+
|       2 | A1     |         80 |
+---------+--------+------------+
|       2 | A2     |         70 |
+---------+--------+------------+
|       2 | A3     |         60 |
+---------+--------+------------+

现在我正在尝试获得以下结果:

+=========+=========+=============+=========+=============+=========+=============+
| Session | Center1 | Efficiency1 | Center2 | Efficiency2 | Center3 | Efficiency3 |
+=========+=========+=============+=========+=============+=========+=============+
|       1 | A1      |          55 | A2      |          66 | A3      |          77 |
+---------+---------+-------------+---------+-------------+---------+-------------+
|       2 | A1      |          80 | A2      |          70 | A3      |          60 |
+---------+---------+-------------+---------+-------------+---------+-------------+

同样,当我有这个table,

+=========+========+============+
| Session | Center | Efficiency |
+=========+========+============+
|       1 | A1     |         55 |
+---------+--------+------------+
|       1 | A2     |         66 |
+---------+--------+------------+
|       1 | A3     |         77 |
+---------+--------+------------+
|       1 | A4     |         88 |
+---------+--------+------------+
|       2 | A1     |         80 |
+---------+--------+------------+
|       2 | A2     |         70 |
+---------+--------+------------+
|       2 | A3     |         60 |
+---------+--------+------------+
|       2 | A4     |         50 |
+---------+--------+------------+
|       3 | A1     |         56 |
+---------+--------+------------+
|       3 | A2     |         67 |
+---------+--------+------------+
|       3 | A3     |         78 |
+---------+--------+------------+
|       3 | A4     |         89 |
+---------+--------+------------+

我的输出应该是这样的:

+=========+=========+=============+=========+=============+=========+=============+=========+=============+
| Session | Center1 | Efficiency1 | Center2 | Efficiency2 | Center3 | Efficiency3 | Center4 | Efficiency4 |
+=========+=========+=============+=========+=============+=========+=============+=========+=============+
|       1 | A1      |          55 | A2      |          66 | A3      |          77 | A4      |          88 |
+---------+---------+-------------+---------+-------------+---------+-------------+---------+-------------+
|       2 | A1      |          80 | A2      |          70 | A3      |          60 | A4      |          50 |
+---------+---------+-------------+---------+-------------+---------+-------------+---------+-------------+
|       3 | A1      |          56 | A2      |          67 | A3      |          78 | A4      |          89 |
+---------+---------+-------------+---------+-------------+---------+-------------+---------+-------------+

为了得到这个,我试过这个,

SELECT 
a.session as session, a.center as center1, a. Efficiency as Efficiency1, 
b.center as center2, b.Efficiency as Efficiency2 from
mytable a
JOIN
mytable b
on a.session=b.session AND a.center != b.center

但它没有显示我试图获得的结果。它显示了比以前更多的行,我无法正确过滤掉行。任何建议将不胜感激。谢谢。

如果您有一个可预测的固定 center 列表,您可以进行条件聚合:

select 
    session,
    'A1' Center1,
    max(case when center = 'A1' then efficiency end) Efficiency1,
    'A2' Center2,
    max(case when center = 'A2' then efficiency end) Efficiency2,
    'A3' Center3,
    max(case when center = 'A3' then efficiency end) Efficiency3
    -- more columns if needed...
from mytable
group by session

也许是这样的:

select distinct T.session,A1.*,A2.*,A3.*,A4.* from mytable as T
left outer join mytable AS A1 on A1.session=T.sessionand A1.center='A1'
left outer join mytable AS A2 on A2.session=T.sessionand A2.center='A2'
left outer join mytable AS A3 on A3.session=T.sessionand A3.center='A3'
left outer join mytable AS A4 on A4.session=T.sessionand A4.center='A4'

更新

select distinct T.session,
    A1.center as Center1,A1.Efficiency as Efficiency1,
    A2.center as Center2,A2.Efficiency as Efficiency2,
    A3.center as Center3,A3.Efficiency as Efficiency3,
    A4.center as Center4,A4.Efficiency as Efficiency4
...

对于使用不同 DBMS 的读者,或者如果 h2 曾经实现过 PIVOT,这里有一个简化的解决方案:

这是 SQL 服务器

的语法
SELECT * FROM myTable PIVOT (MAX(efficiency) FOR center In (A1, A2, A3, A4)) as T
-- MAX is needed since PIVOT works with aggregate functions, but it should be MAX of a single value.

导致:

+---------+----+----+----+----+
| session | A1 | A2 | A3 | A4 |
+---------+----+----+----+----+
|       1 | 55 | 66 | 77 | 88 |
|       2 | 80 | 70 | 60 | 50 |
|       3 | 56 | 67 | 78 | 89 |
+---------+----+----+----+----+

如果您愿意,可以使查询动态化并生成 center 列的列表 (A1, A2, A3, A4)


要为 h2 实现类似的功能,您可以使用:

SELECT
    session,
    GROUP_CONCAT(CASE center WHEN '1A' THEN efficiency END) as 'efficiency_1A', 
    GROUP_CONCAT(CASE center WHEN '2A' THEN efficiency END) as 'efficiency_2A',
    GROUP_CONCAT(CASE center WHEN '3A' THEN efficiency END) as 'efficiency_3A',
    GROUP_CONCAT(CASE center WHEN '4A' THEN efficiency END) as 'efficiency_4A'
FROM myTable