0

Link for previous thread- LINKED Oracle - runtime and show data based on inserted year ( not duplicate )

Summary

Based on the link above, I have continued to code more specfic result that I want, which i have met some problem during the code which is using "WHERE" statement for showing specfic data after using subsitution variable(&)

Based on the working code that provided by Mr.Barboros, I wanted to add some query to show ONLY some location , example,

I have query 2016 , which should only show 2 result

FACILITYNAME COUNT_TIMES    MONTHS
Science Lab     1             4
Science Lab     1             5

But it always show extra Location which is Biology even in 2016 there is no Biology inserted to the DB. - If i added more location with "OR" , it will still continue show the result , below is the code for my query.

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') = 2018
AND f.FACILITYNAME = 'Toilet' OR f.FACILITYNAME = 'Science Lab' OR  f.FACILITYNAME = 'Biology Lab'
GROUP BY (EXTRACT (MONTH FROM b.Timebooked )),f.FACILITYNAME
ORDER BY (EXTRACT (MONTH FROM b.Timebooked ));

http://sqlfiddle.com/#!4/13a6a/9

The result that I want is, when i entered any year , only show Toilet,Science Lab Biology Lab

Cœur
  • 37,241
  • 25
  • 195
  • 267
whalesboy
  • 21
  • 8

1 Answers1

0

Need to add '()' around the group of 'OR' conditions. Without the brackets, AND will take precedence over OR. Refer to SQL Logic Operator Precedence.

Try this:

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') = 2018
AND (f.FACILITYNAME = 'Toilet' OR f.FACILITYNAME = 'Science Lab' OR  f.FACILITYNAME = 'Biology Lab')
GROUP BY (EXTRACT (MONTH FROM b.Timebooked )),f.FACILITYNAME
ORDER BY (EXTRACT (MONTH FROM b.Timebooked ));

Without the brackets, your query is actually doing this (which is incorrect):

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') = 2018
AND  f.FACILITYNAME = 'Toilet') OR f.FACILITYNAME = 'Science Lab' OR  f.FACILITYNAME = 'Biology Lab' 
GROUP BY (EXTRACT (MONTH FROM b.Timebooked )),f.FACILITYNAME
ORDER BY (EXTRACT (MONTH FROM b.Timebooked ));
kc2018
  • 1,440
  • 1
  • 8
  • 8
  • i see , that's why there is a duplicate, thanks kc2018 ! but i do have another question ( not related to this question ) , i have online check that if i want to show April instead of '4' , how should i do it ? i tried to change 'YYYY' to 'YEAR' it not working – whalesboy Mar 31 '18 at 03:35
  • oracle , myOra (SQL EDITOR) – whalesboy Mar 31 '18 at 03:38
  • thanks ! will try to edit my code , thanks for the help @kc2018 – whalesboy Mar 31 '18 at 03:42
  • @whalesboy you are welcome. Here is a link on converting a date to month name: https://stackoverflow.com/questions/4497456/get-month-name-from-date-in-oracle – kc2018 Mar 31 '18 at 03:53