subquery in FROM must have an alias

200,254

Add an ALIAS onto the subquery,

SELECT  COUNT(made_only_recharge) AS made_only_recharge
FROM    
    (
        SELECT DISTINCT (identifiant) AS made_only_recharge
        FROM cdr_data
        WHERE CALLEDNUMBER = '0130'
        EXCEPT
        SELECT DISTINCT (identifiant) AS made_only_recharge
        FROM cdr_data
        WHERE CALLEDNUMBER != '0130'
    ) AS derivedTable                           -- <<== HERE
Share:
200,254
roykasa
Author by

roykasa

Updated on September 18, 2021

Comments

  • roykasa
    roykasa over 2 years

    I have this query I have written in PostgreSQL that returns an error saying:

    [Err] ERROR:
    LINE 3: FROM (SELECT DISTINCT (identifiant) AS made_only_recharge

    This is the whole query:

    SELECT COUNT (made_only_recharge) AS made_only_recharge
    FROM (
        SELECT DISTINCT (identifiant) AS made_only_recharge
        FROM cdr_data
        WHERE CALLEDNUMBER = '0130'
        EXCEPT
        SELECT DISTINCT (identifiant) AS made_only_recharge
        FROM cdr_data
        WHERE CALLEDNUMBER != '0130'
    )
    

    I have a similar query in Oracle that works fine. The only change is where I have EXCEPT in Oracle I have replaced it with the MINUS key word. I am new to Postgres and don't know what it is asking for. What's the correct way of handling this?

  • Andrew Cassidy
    Andrew Cassidy about 9 years
    @JohnWoo thanks for this, but why is it needed (I guess I'm asking a theory question here)?
  • stackhelper101
    stackhelper101 over 8 years
    @AndrewCassidy You have to define so you will be able to add further constraints on your query (WHERE derivedTable.<attribute> = 5). otherwise your db will not know how to refer to the subquery
  • Tregoreg
    Tregoreg over 8 years
    @AndrewCassidy It is just unlucky syntax. As long as you are not referencing to that subquery, it does not matter what it's alias is. Personally, I'm using AS pg_sucks, meaning "well, here you have some redundant identifier, but you could generate some internally by yourself, damn postgres!" :)
  • Scratte
    Scratte about 4 years
    Your wording suggest there is such a requirement for both Oracle and MySQL. Am I reading it right?
  • lmat - Reinstate Monica
    lmat - Reinstate Monica about 4 years
    @Scratte I think you're right and the wording is turned around. "MySQL and Oracle but others" should be "Postgresql, but other ssuch as MySQL and Oracle" I think. Of course it's still a run-on sentence, and could be further improved. The 2013 answer is fine and this answer adds nothing (comment on the 2013 answer if you must), so the latter should be removed.
  • jbowman
    jbowman over 3 years
    @Tregoreg My aliases always involve some sort of expletive to the same effect.
  • ysth
    ysth over 2 years
    mysql does require aliases