Using an Alias column in the where clause in Postgresql

后端 未结 6 1106
轮回少年
轮回少年 2020-11-22 11:04

I have a query like this:

SELECT
    jobs.*, 
    (
        CASE
            WHEN lead_informations.state IS NOT NULL THEN lead_informations.state
                   


        
相关标签:
6条回答
  • 2020-11-22 11:08
    SELECT "tab_1"."BirthDate", "tab_1"."col_1" FROM (
       SELECT BirthDate, DATEADD(year, 18, BirthDate) AS "col_1" FROM Employees
    ) AS "tab_1"
    WHERE "tab_1"."col_1" >= '2000-12-31';
    
    0 讨论(0)
  • 2020-11-22 11:09

    I struggled on the same issue and "mysql syntax is non-standard" is not a valid argument in my opinion. PostgreSQL adds handy non-standard extensions as well, for example "INSERT ... RETURNING ..." to get auto ids after inserts. Also, repeating large queries is not an elegant solution.

    However, I found the WITH statement very helpful. It sort of creates a temporary view within the query which you can use like a usual table then. I'm not sure if I have rewritten your JOIN correctly, but in general it should work like this:

    WITH jobs_refined AS (
        SELECT
            jobs.*,
            (SELECT CASE WHEN lead_informations.state IS NOT NULL THEN lead_informations.state ELSE 'NEW' END) AS lead_state
        FROM jobs
        LEFT JOIN lead_informations
            ON lead_informations.job_id = jobs.id
            AND lead_informations.mechanic_id = 3
    )
    SELECT *
    FROM jobs_refined
    WHERE lead_state = 'NEW'
    
    0 讨论(0)
  • 2020-11-22 11:18

    I believe the common solution is to use an inner SELECT for the calculation (or CASE statement in this case) so that the result of the inner SELECT is available to the entire outer query by the time the execution gets to that query. Otherwise, the WHERE clause is evaluated first and knows nothing about the SELECT clause.

    0 讨论(0)
  • 2020-11-22 11:24

    MySQL's support is, as you experienced, non-standard. The correct way is to reprint the same expression used in the SELECT clause:

    SELECT
        jobs.*, 
        CASE 
             WHEN lead_informations.state IS NOT NULL THEN lead_informations.state 
             ELSE 'NEW' 
        END AS lead_state
    FROM
        jobs
        LEFT JOIN lead_informations ON
            lead_informations.job_id = jobs.id
            AND
            lead_informations.mechanic_id = 3
    WHERE
        lead_informations.state IS NULL
    
    0 讨论(0)
  • 2020-11-22 11:24

    You would need to either duplicate the case statement in the where clause, or my preference is to do something like the following:

    SELECT *
    FROM (
    SELECT 
        jobs.*, 
        (CASE WHEN lead_informations.state IS NOT NULL THEN lead_informations.state ELSE 'NEW' END) as lead_state
    FROM 
        "jobs"
        LEFT JOIN lead_informations ON lead_informations.job_id = jobs.id
        AND lead_informations.mechanic_id = 3
    ) q1
    WHERE (lead_state = 'NEW')
    
    0 讨论(0)
  • 2020-11-22 11:24

    I used alias in where like this. (INNER Query).

    Select "Vendors"."VendorId", "Vendors"."Name","Result"."Total" 
    From (Select "Trans"."VendorId", ("Trans"."A"+"Trans"."B"+"Trans"."C")    AS "Total"
            FROM "Trans"
        WHERE "Trans"."Year"=2014                                                
        ) As "Result"
    JOIN "Vendors" ON "Result"."VendorId"="Vendors"."VendorId" 
    WHERE "Vendors"."Class"='I' AND "Result"."Total" > 200
    
    0 讨论(0)
提交回复
热议问题