views:

46

answers:

1

hello,

i am having problem with my query...

this one works..

  select name, bday, address, dbms_random.value(1, 100) as joker
  from employee order 
  by joker asc

but when i try to get what i want using either the 'where' and group/having clause, i am having a ora-09004 (invalid identifier) ERROR..

e.g.

 select name, bday, address, dbms_random.value(1, 100) as joker 
 from employee where joker>5 
 order by joker asc

 select name, bday, address, dbms_random.value(1, 100) as joker
 from employee group by name, bday, address 
 having joker>5 order by joker asc 

what could be my problem here and how can i query using the joker column...

TIA.

+2  A: 

try:

Select * from
(select name, bday, address, dbms_random.value(1, 100) as joker 
from employee)
where joker>5 
order by joker asc
Michael Pakhantsov
Any idea why his query does not work?
vc 74
tnx michael, it worked, do you know what kind of sql format is this? and why is it reformatted like that? is it because i got the new joker column? tnx
yup got the same question as vc74 .. ;(.. is this kind of query compatible with mysql, sqlite.. etc? tnx
@user415881, will works if you replace function dbms_random.value for mysql to rand(), for sqlite to random()
Michael Pakhantsov
@michael , no what i meant was, with your sql.. why is it formed that way, instead of mine.. in your sql you used .. select * (select * from ???) .. just wondering why it should be done this way.. ;(
sorry your sql is "select * from (select * from somethin)"
@user415881, because you can't reference in where clause to aliased column. instead of 'joker > 5' you have to use 'dbms_random.value(1, 100) > 5'
Michael Pakhantsov
ok tnx michael... last question. ;) ... is that alias rule about using it in a where clause (..like your sql) applies to all sql compliant rdbms? many tnx
@user415881, probably - yes :)
Michael Pakhantsov
@VC 74: This works because you can sort/group/filter by an expression, but you can't sort/group/filter by the name you give the expression in the same query. By nesting the query with the call to DBMS_RANDOM.VALUE, the alias JOKER is available to the ORDER BY clause in the outer query.
Adam Musch