Not sure what is going on here and why this is not working. I'm receiving the following error:
"All expressions in a derived table must have an explicit name" - working with teradata.
select clm.c_clm
,clm.c_loc
from
(select *
from pearl_p.TLTC900_CLM clm) as cl
left join
(select
max(av.d_usr_udt_lst)
from pearl_p.TLTC913_AVY av
group by 1) as avy
on cl.i_sys_clm = avy.i_sys_clm
How do you fix it? The short answer is you need to give your subqueries an alias in your SELECT statement. Add an alias after the closing bracket of the FROM clause subquery. In other SQL vendors, this is not required, but MySQL requires you to alias your subqueries.
Unlike a subquery, a derived table must have an alias so that you can reference its name later in the query. If a derived table does not have an alias, MySQL will issue the following error: Every derived table must have its own alias.
This error is caused by the fact that you basically generate a new table with your subquery for the FROM command. That's what a derived table is, and as such, it needs to have an alias (actually a name reference to it).
CREATE TABLE table1 (id INT, name VARCHAR(20), cats FLOAT); CREATE TABLE table2 (id INT, age INTEGER); SELECT id AS ID, name, cats, age FROM (SELECT table1.id, name, cats, age FROM table1 JOIN table2 ON table1.id = table2.id); According to Derived Tables at dev.mysql.com, an alias is mandatory.
Your max(av.d_usr_udt_lst) in your subquery doesn't have an explicit name. You need to alias it like this:
max(av.d_usr_udt_lst) as "MaxThing"
So the query looks like
select clm.c_clm
,clm.c_loc
from
(select *
from pearl_p.TLTC900_CLM clm) as cl
left join
(select
max(av.d_usr_udt_lst) as "MaxThing"
from pearl_p.TLTC913_AVY av
group by 1) as avy
on cl.i_sys_clm = avy.i_sys_clm
Apart from that error, you have another error in your join:
select clm.c_clm, clm.c_loc
from (select *
from pearl_p.TLTC900_CLM clm
) cl left join
(select max(av.d_usr_udt_lst)
from pearl_p.TLTC913_AVY av
group by 1
) as avy
on cl.i_sys_clm = avy.i_sys_clm
--------------------------^ This variable is not defined.
I think you might want something like:
select clm.c_clm, clm.c_loc
from (select *
from pearl_p.TLTC900_CLM clm
) cl left join
(select i_sys_clm, max(av.d_usr_udt_lst) as maxdate
from pearl_p.TLTC913_AVY av
group by i_sys_clm
) avy
on cl.i_sys_clm = avy.i_sys_clm and
cl.<date column goes here> = avy.maxdate
The Alias clm
only exists within the sub-query that defines the cl
alias. As such you cannot call clm
outside of that sub-query. Change all the outer references to cl
adn you will be fine.
While your at it, you should also gt rid of the SELECT *
and explicitly identify the colums you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With