Oracle SQL Script Fails on "command not ended properly"

Using Oracle SQL Developer, I'm attempting to run the SQL shown below but get the following error each time:

ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
Error at Line: 6 Column: 13

SQL that I am Attempting to Run
-----------------------------------------------

select
(select count(*) from workorder where status='CLOSE' and istask=0 and worktype='EM' and downtime=0)
/
(select count(*) from workorder where status='CLOSE' and istask=0 and worktype = 'EM')
* 100
from dummy_table;

Can you figure out what I am doing wrong?
cbridgmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
Interesting.  Seems sqlplus has issues when it is broken up like that.

Put it on the same line and it seems to be fine:
select 
(select count(*) from workorder where status='CLOSE' and istask=0 and worktype='EM' and downtime=0) / (select count(*) from workorder where status='CLOSE' and istask=0 and worktype = 'EM') * 100
from dummy_table;

Open in new window



You can also more the * 100 inside the select
select 
(select count(*) from workorder where status='CLOSE' and istask=0 and worktype='EM' and downtime=0) / (select count(*) * 100 from workorder where status='CLOSE' and istask=0 and worktype = 'EM') 
from dummy_table;

Open in new window


I also don't like the two selects.  See if this also returns the same results:
select count(*) / count(case when downtime=0 then 1 end) * 100
from workorder
where status='CLOSE' and istask=0 and worktype='EM'
/ 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cbridgmanAuthor Commented:
Thanks very much. Your solution worked perfectly.
0
sdstuberCommented:
I'm late to the party, if you're interested in "why" it fails, it's because the slash  "/" you are using for division is also interpreted as a command to execute the statement.

That's why putting it on one line works, when the slash is in the middle of a statement it isn't parsed as an execute command.
When it's on a line by itself it is.

Also, I think slightwv reversed your quotient.  Assuming you are looking for the percentage of workorders with 0 downtime, that should be

SELECT COUNT(CASE WHEN downtime = 0 THEN 1 END) / COUNT(*) * 100
  FROM workorder
 WHERE status = 'CLOSE' AND istask = 0 AND worktype = 'EM';


Note the downtime=0 count is on "top" of the division as in the original syntax.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.