修改1个查询的输出

时间:2017-11-14 04:44:31

标签: sql oracle jsp

需要你的建议盖伊。我不知道我的问题的标题。但是我有一个查询,它给出了这张图片的输出: output Query

  

这是我的疑问:

select to_char(aa.DATE_AWAL, 'dd/mm/yyyy hh24:mi') DATE_AWAL, to_char(aa.DATE_AKHIR, 'dd/mm/yyyy hh24:mi') DATE_AKHIR,
to_char(aa.DATE_AWAL, 'hh24:mi') TIME_AWAL, to_char(aa.DATE_AKHIR, 'hh24:mi') TIME_AKHIR,
 cc.NAMARUANG,aa.IDMEETING from TMEETING_ROOM aa 
inner join MMEETING_TYPE bb on aa.IDTYPE=bb.IDMEETING 
inner join MMEETING_ROOM cc on aa.IDMEETINGROOM = cc.IDMEETINGROOM 
inner join HR.VWKARYAWAN dd on aa.IDPENGUSUL=dd.IDKARYAWAN 
inner join HR.MLOKASI ee on aa.IDLOKASI = ee.IDLOKASI 
where aa.IS_DELETE IS NULL 
and aa.IDCANCEL IS NULL 
and (
wm_overlaps ( 
wm_period(aa.DATE_AWAL, aa.DATE_AKHIR),
wm_period(
TO_DATE(TO_CHAR(trunc(sysdate) + 08/24, 'yyyy-mm-dd hh24:mi'), 'yyyy-mm-dd hh24:mi'), 
TO_DATE(TO_CHAR(trunc(sysdate) + 23/24, 'yyyy-mm-dd hh24:mi'), 'yyyy-mm-dd hh24:mi')
)
) = 1
) and aa.idlokasi = 'I' order by cc.NAMARUANG asc, aa.DATE_AWAL asc;

任何正文都可以给我建议如何从这个查询中得到这样的图片:example output

我是使用oracle SQL的新手

注意:时间和空间是动态的。

1 个答案:

答案 0 :(得分:0)

以下是如何在MySQL中实现“通用”数据透视表的示例。

使用的技术需要行编号(在MySQL的第8版中会有更简单的方法)但是现在需要使用@variables。

然后,对于每个行号,我们使用case expressions函数(条件聚合)中的max()将行“转换”为列。

您需要确定所需的列数,并注意子查询t内的顺序对于成功排列数据至关重要。

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE YourQueryHere
    (`id` int, `date_column` datetime, `code_column` varchar(7), `data_for_cells1` varchar(5), `data_for_cells2` varchar(5))
;

INSERT INTO YourQueryHere
    (`id`, `date_column`, `code_column`, `data_for_cells1`, `data_for_cells2`)
VALUES
    (1, '2017-11-14 00:00:00', 'Bintang', '09:00', '10:30'),
    (2, '2017-11-14 00:00:00', 'Bintang', '11:00', '12:30'),
    (3, '2017-11-14 00:00:00', 'Bintang', '14:00', '17:00'),
    (4, '2017-11-14 00:00:00', 'Sapporo', '11:30', '14:00'),
    (5, '2017-11-14 00:00:00', 'Sapporo', '14:30', '15:00'),
    (6, '2017-11-14 00:00:00', 'Tiger', '08:00', '09:30'),
    (7, '2017-11-14 00:00:00', 'Tiger', '11:00', '12:00')
;

查询1

select
    code_column
  , max(case when RowNumber = 1 then concat(data_for_cells1, ' ', data_for_cells2) end) as pivcol1
  , max(case when RowNumber = 2 then concat(data_for_cells1, ' ', data_for_cells2) end) as pivcol2
  , max(case when RowNumber = 3 then concat(data_for_cells1, ' ', data_for_cells2) end) as pivcol3
  , max(case when RowNumber = 4 then concat(data_for_cells1, ' ', data_for_cells2) end) as pivcol4
from (
      select * 
           , @counter :=IF(@prev=code_column,@counter+1,1)AS RowNumber
           , @prev := code_column
      from YourQueryHere
      cross join (select @counter:=0, @prev:= '') vars
      order by 
            code_column, date_column
      ) t
group by
    code_column
order by
    code_column

;

<强> Results

| code_column |     pivcol1 |     pivcol2 |     pivcol3 | pivcol4 |
|-------------|-------------|-------------|-------------|---------|
|     Bintang | 09:00 10:30 | 11:00 12:30 | 14:00 17:00 |  (null) |
|     Sapporo | 11:30 14:00 | 14:30 15:00 |      (null) |  (null) |
|       Tiger | 08:00 09:30 | 11:00 12:00 |      (null) |  (null) |