SQL: identificador no válido, pero ¿dónde?

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

Gracias !

preguntado el 28 de mayo de 14 a las 13:05

That means that you don't have a column called jours en la mesa logs. Perhaps the column you want is called jour o algo mas. -

The error did point for you to look at line 7, column 6. You cannot use jours in the where clause, as it is not a column but a calculated field. You must replace jours with the whole expression. -

There may also be other ways to write the query, if you edit your question to describe what you are trying to do. -

3 Respuestas

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.

contestado el 28 de mayo de 14 a las 13:05

Qué tal si:

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'

contestado el 28 de mayo de 14 a las 13:05

The format mask for date_Created should be 'MON. DD YYYY' and the date values should be 'AVR. 14 2014' AND 'AVR. 15 2014' (without an extra space between AVR. and 14/15. - Jose b

El problema en tu SELECT es que el WHERE clause is applied to the last SELECT, Donde jours is not defined. You can fix it by wrapping your query in another SELECT, Así:

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 cláusula.

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.