问题
Im quite a beginner at SQL and I've been trying to workout for hours now what the problem is with this :
select to_char(date_Created, 'MON DD YYYY') as jours, action, count(ID)
from Logs
group by action, to_char(date_Created, 'MON DD YYYY')
union
select distinct to_char(date_Created, 'MON DD YYYY'), action, 0
from Logs
WHERE jours BETWEEN 'AVR. 14 2014' AND 'AVR. 15 2014'
When I try it, it returns an error:
ORA-00904: "JOURS" : identificateur non valide
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Erreur à la ligne 7, colonne 6
Thanks !
回答1:
How about:
select to_char(date_Created, 'MON DD YYYY') as jours, action, count(ID)
from Logs
group by action, to_char(date_Created, 'MON DD YYYY')
union
select distinct to_char(date_Created, 'MON DD YYYY') as jours, action, 0
from Logs
WHERE to_char(date_Created, 'MON DD YYYY') BETWEEN 'AVR 14 2014' AND 'AVR 15 2014'
回答2:
Your problem is caused because Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.You cannnot USE the 'JOURS' label because the where code is not aware of it yet.
回答3:
The problem in your SELECT is that the WHERE clause is applied to the last SELECT, where jours is not defined. You can fix it by wrapping your query in another SELECT, like this:
SELECT * FROM (
-- Here is your original query without the WHERE clause
select to_char(date_Created, 'MON DD YYYY') as jours, action, count(ID)
from Logs
group by action, to_char(date_Created, 'MON DD YYYY')
union
select distinct to_char(date_Created, 'MON DD YYYY'), action, 0
from Logs
) as inner
-- Here is your WHERE clause
WHERE jours BETWEEN 'AVR. 14 2014' AND 'AVR. 15 2014'
Now that the query that produces jours is wrapped, it becomes legal to refer to that column from your WHERE clause.
来源:https://stackoverflow.com/questions/23911978/sql-invalid-identifier-but-where