如何减少行数和增加列数以实现新视图

时间:2019-11-21 13:19:01

标签: sql pivot h2

我有一个与此类似的表:

+=========+========+============+
| 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 |
+---------+---------+-------------+---------+-------------+---------+-------------+

类似地,当我有这张桌子时,

+=========+========+============+
| 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

但是它显示我要获取的结果。它显示的行比以前更多,我无法正确过滤掉行。任何建议将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:1)

如果您有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

答案 1 :(得分:1)

也许是这样的:

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
...

答案 2 :(得分:1)

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

这是SQL Server的语法

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