Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with subquery - All expressions must have explicit name

Tags:

sql

teradata

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
like image 673
gfuller40 Avatar asked Sep 09 '13 15:09

gfuller40


People also ask

How do you solve the error Every derived table must have its own alias?

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.

Why Every derived table must have its own alias?

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.

Could not run Statement 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).

How do I create an alias for derived table?

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.


3 Answers

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
like image 192
Derek Kromm Avatar answered Sep 24 '22 01:09

Derek Kromm


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
like image 34
Gordon Linoff Avatar answered Sep 25 '22 01:09

Gordon Linoff


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.

like image 24
Declan_K Avatar answered Sep 24 '22 01:09

Declan_K