views:

110

answers:

5

Greetings, I am having problems with a line of code. I am trying to create a count function for a view that I created. I have done this a bunch of different ways but below is the format that I have most recently used.

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS
SELECT EVENTNAME, RACES.DESCRIPTION, 
       RIDERS_FIRSTNAME||' '||RTRIM(RIDERS_LASTNAME)
FROM EVENTS, RACES, PARTICIPATION, RIDERS
WHERE EVENTS.EVENTID = RACES.EVENTID
AND RACES.RACEID = PARTICIPATION.RACEID
AND RIDERS.RIDERID = PARTICIPATION.RIDERID
ORDER BY RIDERS.RIDERS_LASTNAME, EVENTNAME;

The error I am getting is ORA-00907: missing right parenthesis the error is at (COUNT(RIDERS) part of the code. Any ideas how I should tackle this the reason would be very hepful.

+1  A: 
CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS

....

Should not it be:

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS)) AS
Charles Bretana
+1  A: 

As the error message points out you are missing a closing bracket ). The bracket is opened here : 'ERP_REPORT(EVENTNAME' and is never closed.

Vincent Ramdhanie
Oh my god! You mean the error message was correct? Who would have thought!
Paul Tomblin
+1  A: 

Hi Michael,

you can't have brackets in column names unless you put quotation marks around. Try this:

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, "COUNT(RIDERS)") AS ...

or

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT_RIDERS) AS ...

For example:

SQL> CREATE OR REPLACE VIEW foo ("count(*)") AS SELECT COUNT(*) FROM dual;

View created

SQL> CREATE OR REPLACE VIEW foo (count_all) AS SELECT COUNT(*) FROM dual;

View created
Vincent Malgrat
Vencent I tried this and could only get it to count the Count_Riders column and would not display the rest of the columns.
Michael
@Michael: you mean when you query `SELECT * FROM erp_report` you only see one column ?
Vincent Malgrat
Yes, when I SELECT * erp_report .. I am new at this so I may not have followed your instructions correctly. I will run it again just you you provided. I am going to go with the second option. the run the two separate sets of code.
Michael
+6  A: 

The list of names in parentheses on line 1 should be the names of the view columns:

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, COUNT(RIDERS) AS ...

You can't create a column called "COUNT(RIDERS" or even "COUNT(RIDERS)" since a column name may not contain ( or ). This would work:

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_FULL_NAME) AS ...

However it appears that you really do want a count of something, though I'm not sure of what. To do that the view definition would have to be something like:

CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_COUNT) AS
SELECT EVENTNAME, RACES.DESCRIPTION, COUNT(*)
FROM EVENTS, RACES, PARTICIPATION, RIDERS
WHERE EVENTS.EVENTID = RACES.EVENTID
AND RACES.RACEID = PARTICIPATION.RACEID
AND RIDERS.RIDERID = PARTICIPATION.RIDERID
GROUP BY EVENTNAME, DESCRIPTION;

(i.e. the COUNT function goes in the SELECT part, not in the list of column names).

As an aside, since you are presumably new to Oracle, I would suggest you start using the more modern ANSI join syntax to make your queries clearer:

...
FROM EVENTS
JOIN RACES ON RACES.EVENTID = EVENTS.EVENTID
JOIN PARTICIPATION ON PARTICIPATION.RACEID = RACES.RACEID
JOIN RIDERS ON RIDERS.RIDERID = PARTICIPATION.RIDERID
Tony Andrews
+1 Beat me to it for a complete answer.
APC
I suppose I am still doing something wrong... I am getting an, ORA-00937: not a single-group group function, error the code I am using is.. CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_COUNT) AS SELECT EVENTNAME, RACES.DESCRIPTION, COUNT(RIDERS_LASTNAME) FROM EVENTS, RACES, PARTICIPATION, RIDERS WHERE EVENTS.EVENTID = RACES.EVENTID AND RACES.RACEID = PARTICIPATION.RACEID AND RIDERS.RIDERID = PARTICIPATION.RIDERID; ORDER BY EVENTNAME, DESCRIPTION;
Michael
I suppose I am still doing something wrong... I am getting an, ORA-00937: not a single-group group function, error the code I am using is.. CREATE VIEW ERP_REPORT(EVENTNAME, DESCRIPTION, RIDER_COUNT) AS SELECT EVENTNAME, RACES.DESCRIPTION, COUNT(RIDERS_LASTNAME) FROM EVENTS, RACES, PARTICIPATION, RIDERS WHERE EVENTS.EVENTID = RACES.EVENTID AND RACES.RACEID = PARTICIPATION.RACEID AND RIDERS.RIDERID = PARTICIPATION.RIDERID; ORDER BY EVENTNAME, DESCRIPTION;
Michael
Yes, you haven't got a GROUP BY clause in the query. Se my example that does.
Tony Andrews
Thanks - I will modify and try again.
Michael
Tony thanks that seemed to work very well. I also appreciate the example in how to use standardized syntax for joining tables.
Michael
I should have known that you can't create a function on a view you are creating......
Michael
+1  A: 

As Tony points out there are actually several syntax errors in your statement. The missing bracket is just the first.

I find it useful to have an IDE which supports bracket matching, because it can be hard to walk back through the code and find which bracket lacks a mate. As it happens my choice is TextPad but just about anything which is more advanced than NotePad ought to be able to do this.

APC
+1 for TextPad!
Tony Andrews