我想转置我的结果。
这是我的结果表:
CUMA 13:30 ORHAN SAVAS
CUMA 14:00 FATMA ETA
CUMA 14:30 ISMAHAN YALDIZ
PAZARTESI 13:00 SEYHAN UNVER
PAZARTESI 13:30 SELMA CALISKAN
PAZARTESI 17:45 ESMA COMERT
SALI 09:45 SEYMA DURLANIK
SALI 10:00 HASAN GOC
SALI 13:00 TURKAN BICAK
SALI 14:30 ISMAHAN YALDIZ
PERSEMBE 08:30 ZUHRE YEL
PERSEMBE 08:48 AYSEL POLAT
PERSEMBE 09:00 AHMET OZGUNGOR
PERSEMBE 09:12 TELEFON RANDEVUSU
我可以像这样转换结果表吗?
CUMA PAZARTESI SALI PERSEMBE
13:30 ORHAN SAVAS 13:00 SEYHAN UNVER 09:45 SEYMA DURLANIK 08:30 ZUHRE YEL
14:00 FATMA ETA 13:30 SELMA CALISKAN 10:00 HASAN GOC 08:48 AYSEL POLAT
14:30 ISMAHAN YALDIZ 17:45 ESMA COMERT 13:00 TURKAN BICAK 09:00 AHMET OZGUNGOR
14:30 ISMAHAN YALDIZ 09:12 TELEFON RANDEVUSU
İt是我的疑问:
WITH got_r_num AS
(
SELECT TO_CHAR (t.r_tarihi, 'DY') AS gun
, TO_CHAR (t.baslama, 'HH24:MI') AS saat
, h.adi || ' ' || h.soyadi AS hasta
, ROW_NUMBER () OVER ( PARTITION BY TO_CHAR (t.r_tarihi, 'DY')
ORDER BY t.baslama
) AS r_num
FROM randevu_entegre_deneme t
LEFT OUTER JOIN hasta_deneme h ON h.id = t.hasta_id
WHERE (r_tarihi BETWEEN TO_DATE ('20.5.2011', 'dd.mm.yyyy')
AND TO_DATE ('20.5.2011', 'dd.mm.yyyy') + 9)
)
SELECT *
FROM got_r_num PIVOT ( MIN (SAAT) AS s , MIN (HASTA) AS h
FOR gun IN ( 'MON' AS monday
, 'TUE' AS tuesday
, 'WED' AS wednesday
, 'THU' AS thursday
, 'FRI' AS friday
)
)
ORDER BY r_num;
这是我的CREATE TABLE和INSERT语句
create table RANDEVU_ENTEGRE_DENEME
(
hasta_id INTEGER,
baslama DATE,
R_TARIHI DATE
);
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (39733, to_date('24-05-2011 13:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('24-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (367216, to_date('23-05-2011 13:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('23-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (522956, to_date('20-05-2011 13:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('20-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (801923, to_date('23-05-2011 17:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('23-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (815746, to_date('24-05-2011 09:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('24-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (815746, to_date('20-05-2011 08:54:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('20-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (842677, to_date('20-05-2011 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('20-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (842677, to_date('24-05-2011 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('24-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (854143, to_date('26-05-2011 08:48:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('26-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (854559, to_date('23-05-2011 13:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('23-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (861624, to_date('20-05-2011 10:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('20-05-2011', 'dd-mm-yyyy'));
insert into RANDEVU_ENTEGRE_DENEME (hasta_id, baslama, r_tarihi)
values (868595, to_date('26-05-2011 08:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('26-05-2011', 'dd-mm-yyyy'));
commit;
create table HASTA_DENEME
(
id INTEGER,
adi VARCHAR2(25),
soyadi VARCHAR2(25)
);
insert into HASTA_DENEME (id, adi, soyadi)
values (39733, 'TURKAN', 'BICAK');
insert into HASTA_DENEME (id, adi, soyadi)
values (367216, 'SELMA', 'CALISKAN');
insert into HASTA_DENEME (id, adi, soyadi)
values (522956, 'ORHAN', 'SAVAS');
insert into HASTA_DENEME (id, adi, soyadi)
values (801923, 'ESMA', 'COMERT');
insert into HASTA_DENEME (id, adi, soyadi)
values (815746, 'SEYMA', 'DURLANIK');
insert into HASTA_DENEME (id, adi, soyadi)
values (842677, 'FATMA', 'ETA');
insert into HASTA_DENEME (id, adi, soyadi)
values (854143, 'AYSEL', 'POLAT');
insert into HASTA_DENEME (id, adi, soyadi)
values (854559, 'SEYHAN', 'UNVER');
insert into HASTA_DENEME (id, adi, soyadi)
values (861624, 'SENGUL', 'AKBAS');
insert into HASTA_DENEME (id, adi, soyadi)
values (868595, 'ZUHRE', 'YEL');
commit;
我得到了ora-00933。我该如何解决..
答案 0 :(得分:3)
PIVOT
运算符为added in Oracle 11gR1。尝试在10gR2中运行代码会出现错误:
FROM got_r_num PIVOT ( MIN (SAAT) AS s , MIN (HASTA) AS h
*
ERROR at line 15:
ORA-00933: SQL command not properly ended
在11g之前,您需要使用不同的机制like this one。
我想你想要这样的东西:
WITH got_r_num AS
(
SELECT TO_CHAR (t.r_tarihi, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') AS gun
, TO_CHAR (t.baslama, 'HH24:MI') AS saat
, h.adi || ' ' || h.soyadi AS hasta
, ROW_NUMBER () OVER ( PARTITION BY TO_CHAR (t.r_tarihi, 'DY')
ORDER BY t.baslama
) AS r_num
FROM randevu_entegre_deneme t
LEFT OUTER JOIN hasta_deneme h ON h.id = t.hasta_id
WHERE (r_tarihi BETWEEN TO_DATE ('20.5.2011', 'dd.mm.yyyy')
AND TO_DATE ('20.5.2011', 'dd.mm.yyyy') + 9)
)
SELECT min(case when gun = 'MON' then SAAT ||' '|| HASTA end) as pazartesi
, min(case when gun = 'TUE' then SAAT ||' '|| HASTA end) as sali
, min(case when gun = 'WED' then SAAT ||' '|| HASTA end) as carsamba
, min(case when gun = 'THU' then SAAT ||' '|| HASTA end) as persembe
, min(case when gun = 'FRI' then SAAT ||' '|| HASTA end) as cuma
FROM got_r_num
GROUP BY r_num
ORDER BY r_num;
使用您发布的表和插入语句给出:
PAZARTESI SALI CARSAMBA PERSEMBE CUMA
-------------------- -------------------- -------------------- -------------------- --------------------
13:00 SEYHAN UNVER 09:45 SEYMA DURLANIK 08:30 ZUHRE YEL 08:54 SEYMA DURLANIK
13:30 SELMA CALISKAN 13:00 TURKAN BICAK 08:48 AYSEL POLAT 10:00 SENGUL AKBAS
17:45 ESMA COMERT 14:00 FATMA ETA 13:30 ORHAN SAVAS
14:00 FATMA ETA
...这与您所说的预期不符,但使用pivot
与数据和bluefeet的SQl小提琴对齐。
我使用了你似乎期待的土耳其日标签;正如realspirituals所指出的那样,在发布问题时更改代码中的内容并不完全有用。语言差异在这里很重要,因为您的日期比较假设DY
格式是某种语言 - 无论您实际使用的是英语还是土耳其语缩写并不重要,重点是您假设客户端和代码使用相同的语言。你可以通过强迫DY
使用特定语言来避免潜在的不匹配,因此你知道你要比较什么;正是这样做,使用the to_char() function的可选第三个参数:
TO_CHAR (t.r_tarihi, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH')
此外,我已将其保留为仅报告五天,因为您似乎对周末不感兴趣。虽然您的日期跨度为9天似乎有点奇怪,因为那将包括多天的某些日期,并且您将无法确定特定时间来自哪个星期。您的r_num
应该将它们保持在正常状态,因此如果您在两周内都有这些时间,那么您可能会看到出现的时间不正确,但它们也将基于日期。大概这就是你想要的。
有关排序列的后续查询;你可以通过使用dense rank
分配另一个伪列并将其用于case
而不是gun
来实现这一点:
WITH got_r_num AS
(
SELECT TO_CHAR (t.r_tarihi, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') AS gun
, TO_CHAR (t.r_tarihi, 'DAY', 'NLS_DATE_LANGUAGE=TURKISH') AS d_name
, DENSE_RANK () OVER ( ORDER BY TRUNC( t.r_tarihi) ) AS d_num
, TO_CHAR (t.baslama, 'HH24:MI') AS saat
, h.adi || ' ' || h.soyadi AS hasta
, ROW_NUMBER () OVER ( PARTITION BY TO_CHAR (t.r_tarihi, 'DY')
ORDER BY t.baslama
) AS r_num
FROM randevu_entegre_deneme t
LEFT OUTER JOIN hasta_deneme h ON h.id = t.hasta_id
WHERE (r_tarihi BETWEEN TO_DATE ('20.5.2011', 'dd.mm.yyyy')
AND TO_DATE ('20.5.2011', 'dd.mm.yyyy') + 9)
)
SELECT min(case when mod(d_num, 7) = 1 then SAAT ||' '|| HASTA end) as day1
, min(case when mod(d_num, 7) = 2 then SAAT ||' '|| HASTA end) as day2
, min(case when mod(d_num, 7) = 3 then SAAT ||' '|| HASTA end) as day3
, min(case when mod(d_num, 7) = 4 then SAAT ||' '|| HASTA end) as day4
, min(case when mod(d_num, 7) = 5 then SAAT ||' '|| HASTA end) as day5
, min(case when mod(d_num, 7) = 6 then SAAT ||' '|| HASTA end) as day6
, min(case when mod(d_num, 7) = 0 then SAAT ||' '|| HASTA end) as day7
FROM got_r_num
GROUP BY r_num
ORDER BY r_num;
DAY1 DAY2 DAY3 DAY4 DAY5 DAY6 DAY7
-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
08:54 SEYMA DURLANIK 13:00 SEYHAN UNVER 09:45 SEYMA DURLANIK 08:30 ZUHRE YEL
10:00 SENGUL AKBAS 13:30 SELMA CALISKAN 13:00 TURKAN BICAK 08:48 AYSEL POLAT
13:30 ORHAN SAVAS 17:45 ESMA COMERT 14:00 FATMA ETA
14:00 FATMA ETA
我实际上并没有使用gun
,但我已将其保留,并添加d_name
作为全天名称,但目前尚未选中。这完全是跳过carsamba
列的内容,因为它没有数据,这与您的问题显示的内容相符。如果您知道哪个列是哪个,那可能就是您想要的。很遗憾,您无法动态分配列别名,因此您不知道day1
实际上是cuma
。如果您想要carsamba
的间隙,那么您可以计算出每个日期与开始日期的偏差:
, TRUNC( t.r_tarihi) - TO_DATE ('20.5.2011', 'dd.mm.yyyy') + 1 AS d_num
但是您仍然不知道第1列代表哪一天。你可以假装,但这取决于你如何运行它。如果您从应用程序中提取数据,那么您也可以查询d_name
并使用它。如果您重复查询并使用替换变量(可能是很多开销),或者部分解决方案只是选择它们作为单独的选择,那么从SQL * Plus可以执行此操作,这也不理想:
column r_num noprint
set head off
WITH got_r_num AS
(
...
)
SELECT 0 as r_num
, min(case when mod(d_num, 7) = 1 then d_name end) as day1
, min(case when mod(d_num, 7) = 2 then d_name end) as day2
, min(case when mod(d_num, 7) = 3 then d_name end) as day3
, min(case when mod(d_num, 7) = 4 then d_name end) as day4
, min(case when mod(d_num, 7) = 5 then d_name end) as day5
, min(case when mod(d_num, 7) = 6 then d_name end) as day6
, min(case when mod(d_num, 7) = 0 then d_name end) as day7
FROM got_r_num
UNION ALL
SELECT r_num
, min(case when mod(d_num, 7) = 1 then SAAT ||' '|| HASTA end) as day1
, min(case when mod(d_num, 7) = 2 then SAAT ||' '|| HASTA end) as day2
, min(case when mod(d_num, 7) = 3 then SAAT ||' '|| HASTA end) as day3
, min(case when mod(d_num, 7) = 4 then SAAT ||' '|| HASTA end) as day4
, min(case when mod(d_num, 7) = 5 then SAAT ||' '|| HASTA end) as day5
, min(case when mod(d_num, 7) = 6 then SAAT ||' '|| HASTA end) as day6
, min(case when mod(d_num, 7) = 0 then SAAT ||' '|| HASTA end) as day7
FROM got_r_num
GROUP BY r_num
ORDER BY r_num;
CUMA PAZARTESI SALI PER¿EMBE
08:54 SEYMA DURLANIK 13:00 SEYHAN UNVER 09:45 SEYMA DURLANIK 08:30 ZUHRE YEL
10:00 SENGUL AKBAS 13:30 SELMA CALISKAN 13:00 TURKAN BICAK 08:48 AYSEL POLAT
13:30 ORHAN SAVAS 17:45 ESMA COMERT 14:00 FATMA ETA
14:00 FATMA ETA