Home >Database >Mysql Tutorial >Why Does My PostgreSQL Subquery Need an Alias in the FROM Clause?

Why Does My PostgreSQL Subquery Need an Alias in the FROM Clause?

Linda Hamilton
Linda HamiltonOriginal
2025-01-05 04:26:40566browse

Why Does My PostgreSQL Subquery Need an Alias in the FROM Clause?

Subquery Aliasing in PostgreSQL

When facing the error message "[Err] ERROR: LINE 3: FROM (SELECT DISTINCT (identifiant) AS made_only_recharge", it indicates that a subquery in the FROM clause is missing an alias.

In the given query, the subquery is used to calculate the number of distinct identifiers that appear in the cdr_data table only for the CALLEDNUMBER value of '0130'. However, the subquery lacks an alias, which PostgreSQL requires for subqueries appearing in the FROM clause.

To resolve this issue, add an alias to the subquery, as shown below:

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  -- ALIAS ADDED

The above is the detailed content of Why Does My PostgreSQL Subquery Need an Alias in the FROM Clause?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn