0

I am new to Oracle DBMS and I want to try the following:

I have an entity "Flight" with an attribute "DepartureTime", I want to know the number of flights at 2 different times and compare the count, then print which time has more flights

My attempt:

IF (SELECT COUNT(DepartureTime)   
From Flight    
WHERE DepartureTime='23:58')>   
(SELECT COUNT(DepartureTime)   
From Flight  
WHERE DepartureTime='23:00')  
 BEGIN 
PRINT 'First is bigger'    
END   
ELSE  
 BEGIN   
PRINT 'Second is bigger'  
END;

I am getting "Invalid SQL Statement"

Any help would be appreciated.

8
  • 2
    This is not valid Oracle syntax. This looks like SQL Server. Commented Nov 21, 2020 at 15:24
  • @GMB would you mind helping? I am playing with iy on my own and just googled how I can do which resulted in this syntax Commented Nov 21, 2020 at 15:25
  • Yes, syntax incorrect. If this is a part of larger SQL procedure type thing, pls post it. Commented Nov 21, 2020 at 15:25
  • 1
    I will have access to oracle in around an hour so I didn't test it as of yet, I will let you know what happens when I do. Commented Nov 21, 2020 at 15:46
  • 1
    @KoushikRoy it is working, please put it as answer so I mark it! Commented Nov 22, 2020 at 15:50

1 Answer 1

1

You can try this -

SELECT case when subq1.cnt > subq2.cnt then  'First is bigger'  else 'Second is bigger' end as output 
FROM
(SELECT COUNT(DepartureTime) cnt    From Flight     WHERE DepartureTime='23:58') subq1, 
(SELECT COUNT(DepartureTime) cnt   From Flight   WHERE DepartureTime='23:00')  subq2; 

I created two subqueries to count departure times and then compared those counts in outer query using case-when.
Its not the most efficient but it will meet your requirement.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! btw if you are interested I posted another question that you might be able to answer too stackoverflow.com/questions/64956724/…
accepted your edit, any further help would be appreciated :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.