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 ))
No problem seems with proper formatting (using
quotes
&to_date
whenever needed) :Demo(2016 directly substituted for &n)