Why when adding * to query I get an error? - oracle

This is probably a very very basic issue but I'm a beginner with Oracle. I'm running a simple query, which works fine and returns results, but when adding a * to the list of columns displayed, I get the below error:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 4 Column: 7
The query I'm running is:
select
sql_plan_hash_value col1
, elapsed_seconds col2
, *
from
(select *
from SYS.V_$SESSION_LONGOPS
order by elapsed_seconds desc) result_set
where rownum <= 10;
I thought that it's because I'm not giving aliases to my first two columns, so I did, but the query is still not working.

You can't mix raw * with other columns. You need to use the appropriate alias:
select
sql_plan_hash_value col1
, elapsed_seconds col2
, result_set.* --> Like this
from
(select *
from SYS.V_$SESSION_LONGOPS
order by elapsed_seconds desc) result_set
where rownum <= 10;

Related

ORA-00979 Not a Group function error for query with User defined function in select statement

I have this query where a user defined function is added in the select and group by statement.
The inner select query without the WITH clause runs fine and doesn't give any error. But after adding WITH clause it gives the following error -
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action: Error at Line: 3 Column: 29
I need the WITH clause to return only a subset of the entire result set based on input ranges.
Query is as follows:
WITH INFO AS (
SELECT
GET_EVAULATED_VALUE(T.C_IMP, T.IMP) AS IMPORTANCE,
count(*) AS NO_OF_PC_AFFECTED
FROM TABLE_NAME T
WHERE T.ACNT_REL_ID = 16
GROUP BY
(GET_EVAULATED_VALUE(T.C_IMP, T.IMP))
ORDER BY IMPORTANCE desc
)
SELECT * FROM
(
SELECT ROWNUM AS RN,
(SELECT COUNT(*) FROM INFO) COUNTS,
IMPORTANCE
FROM INFO
)
WHERE RN > 0 AND RN <= 10;
I am not sure how to use CTE with group by on user defined function. But I realized that I can rewrite the query to remove sub-query and CTE and make it simpler as following (and it works):
select * from (
select a.*, ROWNUM rnum from
(SELECT
count(*) over() as COUNTS,
GET_EVAULATED_VALUE(T.C_IMP, T.IMP) AS IMPORTANCE,
count(*) AS NO_OF_PC_AFFECTED
FROM TABLE_NAME T
WHERE T.ACNT_RELATION_ID = 16
GROUP BY
(GET_EVAULATED_VALUE(T.C_IMP, T.IMP))
ORDER BY importance desc) a
where ROWNUM <= 10 )
where rnum >= 0;
Same issue here, I created a table "TABLE_CTE" instead of using a CTE and it worked.
CREATE TABLE TABLE_CTE
AS
SELECT
USER_DEFINED_FUNCTION(date_1),
COUNT(*)
FROM
TABLE_NAME
GROUP BY
USER_DEFINED_FUNCTION(date_1)
;
SELECT * FROM TABLE_CTE

FROM keyword not found where expected - basic query

I don't understand why this is not working (my production query is much more complicated):
select *, rownum from (
select 1 from dual);
I am getting ORA-00923: FROM keyword not found where expected error.
What is wrong with this query?
You'd need an alias on your inline view and on the * in your select list
select a.*, rownum
from (select 1 from dual) a;

FROM keyword not found where expected while filtering data

I got one error
ORA-00923: FROM keyword not found where expected
Here is my query
SELECT * FROM
(SELECT *, (SELECT COUNT(*) FROM invoices) AS numberOfRows
FROM invoices ORDER BY Id DESC) WHERE rownum <= 1
I am begginer in Oracle SQL, but as I see here I have FROM keyword and it looks everythink OK.
I try to modify this query something like but still get another error
ORA-00933: SQL command not properly ended
SELECT * FROM
(SELECT COUNT(*) FROM invoices) AS numberOfRows
FROM invoices ORDER BY Id DESC) WHERE rownum <= 1
What is wrong in first select query ? What is missing ? Since I check everything, start from special character ( . , )
Also I try this kind of solution and get error
ORA-00936: missing expression
SELECT * FROM (SELECT , (SELECT COUNT() FROM invoices) AS numberOfRows FROM invoices ORDER BY Id DESC) WHERE rownum <= 1
The railroad diagram in the documentation:
... shows that you can either use * on its own, or <something>.* along with other columns or expressions. So you need to precede your * with the table name or an alias:
SELECT * FROM
(SELECT i.*, (SELECT COUNT(*) FROM invoices) AS numberOfRows
FROM invoices i ORDER BY Id DESC) WHERE rownum <= 1
If you're on a recent version of Oracle you can do this much more simply with:
select i.*, count(*) over () as numberOfRows
from invoices i
order by id desc
fetch first row only
On older version you still need a subquery, but only one level:
select *
from (
select i.*, count(*) over () as numberOfRows
from invoices i
order by id desc
)
where rownum = 1
db<>fiddle
looks like the FROM is missing from this select "SELECT *,"
SELECT * FROM
(SELECT , (SELECT COUNT() FROM invoices) AS numberOfRows
FROM invoices ORDER BY Id DESC) WHERE rownum <= 1

first alias works second doesn't throws invalid identifier [duplicate]

How to 'group by' a query using an alias, for example:
select count(*), (select * from....) as alias_column
from table
group by alias_column
I get 'alias_column' : INVALID_IDENTIFIER error message. Why? How to group this query?
select
count(count_col),
alias_column
from
(
select
count_col,
(select value from....) as alias_column
from
table
) as inline
group by
alias_column
Grouping normally works if you repeat the respective expression in the GROUP BY clause. Just mentioning an alias is not possible, because the SELECT step is the last step to happen the execution of a query, grouping happens earlier, when alias names are not yet defined.
To GROUP BY the result of a sub-query, you will have to take a little detour and use an nested query, as indicated above.
Nest the query with the alias column:
select count(*), alias_column
from
( select empno, (select deptno from emp where emp.empno = e.empno) as alias_column
from emp e
)
group by alias_column;
select count(*), (select * from....) as alias_column
from table
group by (select * from....)
In Oracle you cannot use an alias in a group by clause.
To use an alias in Oracle you need to ensure that the alias has been defined by your query at the point at which the alias is being used.
The most straightforward way to do this is to simply treat the original query as a subquery -- in this case,
select count(*), (select * from....) as alias_column
from table
group by (select * from....)
becomes
select count, alias_column
from
(select count(*) as count, (select * from....) as alias_column
from table)
group by alias_column
I can't speak to the performance implications, but it's very quick to write if you're trying to re-use an alias in your query - throw everything in parentheses and jump up a level...
If you don't have to use an alias you could do it this way:
select
EXTRACT(year from CURRENT_DATE), count(*) from something
group by EXTRACT(year from CURRENT_DATE)
order by EXTRACT(year from CURRENT_DATE)
Instead of using alias and subquery.

why am I getting ORA-00936 error

I can run the following statements and get the correct results:
select moon_phase_text (sysdate)
from dual;
select date_day3
from aday3import t1,
aasum_report t2
where t1.date_day3 = t2.game_date;
This statement gives me a list of days ie 22-FEB-03
However when I am trying to update a field in a table with the following statement I get the error ORA-00936: missing expression
update aday3import
set moon_phase = select moon_phase_
(select date_day3
from aday3import t1,
aasum_report t2
where t1.date_day3 = t2.game_date )
from dual;
Instead of going for dual. Try concatenating the moon_phase_ in the select statement itself
update aday3import set moon_phase =
(select 'moon_phase_'||date_day3
from aday3import t1,
aasum_report t2
where t1.date_day3 = t2.game_date )

Resources