Oracle - runtime and show data based on inserted y

2019-08-30 21:55发布

FacilityTable

FACILITYNO    FACILITYNAME
'FACNO01'        Toilet
'FACNO02'       Staff Office
'FACNO03'     Principle Office
'FACNO04'       Science Lab
'FACNO05'       Math Lab
'FACNO06'       Computer Lab
'FACNO07'        Biology Lab
'FACNO08'       Chemical Lab

BookingTable

FACILITYNO  bookingID       userID     Timebooked  
'FACNO01'       1             0003    10-May-2016       
'FACNO04'       2             0001    10-May-2018    
'FACNO05'       3             0001    10-Apr-2017
'FACNO01'       4             0001    10-Apr-2017
'FACNO02'       5             0003    10-Jan-2011
'FACNO04'       6             0006    10-Apr-2018
'FACNO06'       7             0003    10-Apr-2016
'FACNO07'       8             0006    10-Apr-2015
'FACNO08'       9             0001    10-Apr-2017

Expected Result (after insert &n -> Year 2016)

FACILITYNAME  count_times  MONTH
Toilet          1            5
Computer Lab    1            4

On this code I , when runtime is used (&n) and I entered 2016' it will show the expected table that I have provided above.

Below is my code ( but ended up error/duplicate , appearing the data even is not the year I have inserted (2016) )

SELECT f.FACILITYNAME,COUNT(*) AS count_times, EXTRACT (MONTH FROM b.Timebooked ) AS MONTHS
FROM BookingTable b
JOIN FacilityTable f ON b.FACILITYNO = f.FACILITYNO
WHERE TO_CHAR(b.Timebooked , 'YYYY') = &n
GROUP BY (EXTRACT (MONTH FROM b.Timebooked )),f.FACILITYNAME
ORDER BY (EXTRACT (MONTH FROM b.Timebooked ))

1条回答
放我归山
2楼-- · 2019-08-30 22:57

No problem seems with proper formatting (using quotes & to_date whenever needed) :

create table FacilityTable (FACILITYNO varchar2(30), FACILITYNAME varchar2(30));
insert into FacilityTable values('FACNO01','Toilet');
insert into FacilityTable values('FACNO02','Staff Office');
insert into FacilityTable values('FACNO03','Principle Office');
insert into FacilityTable values('FACNO04', 'Science Lab');
insert into FacilityTable values('FACNO05','Math Lab');
insert into FacilityTable values('FACNO06','Computer Lab');
insert into FacilityTable values('FACNO07','Biology Lab');
insert into FacilityTable values('FACNO08','Chemical Lab');


create table BookingTable (FACILITYNO varchar2(30),bookingID int, userID varchar2(30), Timebooked date);
insert into BookingTable values('FACNO01',1,'0003',to_date('10-May-2016','dd-Mon-yyyy'));       
insert into BookingTable values('FACNO04',2,'0001',to_date('10-May-2018','dd-Mon-yyyy'));    
insert into BookingTable values('FACNO05',3,'0001',to_date('10-Apr-2017','dd-Mon-yyyy'));
insert into BookingTable values('FACNO01',4,'0001',to_date('10-Apr-2017','dd-Mon-yyyy'));
insert into BookingTable values('FACNO02',5,'0003',to_date('10-Jan-2011','dd-Mon-yyyy'));
insert into BookingTable values('FACNO04',6,'0006',to_date('10-Apr-2018','dd-Mon-yyyy'));
insert into BookingTable values('FACNO06',7,'0003',to_date('10-Apr-2016','dd-Mon-yyyy'));
insert into BookingTable values('FACNO07',8,'0006',to_date('10-Apr-2015','dd-Mon-yyyy'));
insert into BookingTable values('FACNO08',9,'0001',to_date('10-Apr-2017','dd-Mon-yyyy'));

SELECT f.FACILITYNAME,COUNT(*) AS count_times, EXTRACT (MONTH FROM b.Timebooked ) AS MONTHS
FROM BookingTable b
JOIN FacilityTable f ON b.FACILITYNO = f.FACILITYNO
WHERE TO_CHAR(b.Timebooked , 'YYYY') = &n
GROUP BY (EXTRACT (MONTH FROM b.Timebooked )),f.FACILITYNAME
ORDER BY (EXTRACT (MONTH FROM b.Timebooked ));

FACILITYNAME    COUNT_TIMES MONTHS
Computer Lab           1    4
Toilet                 1    5

Demo(2016 directly substituted for &n)

查看更多
登录 后发表回答