加入2个临时表

时间:2013-11-29 05:02:28

标签: sql postgresql

如何在下面的查询中再创建一个临时表以使其连接相同。

WITH financial_move_out_due_days AS ( 
    SELECT property_id, 
        management_company_id, 
        value 
    FROM property_preferences 
    WHERE property_id IN ( 112,332 ) AND management_company_id = 23 
    )
    SELECT 
        l.id,
        l.primary_customer_id,
        ( c.name_first || c.name_last ) AS customer_name_full,
        c.email_address AS customer_email_address,
        c.phone_number AS phone_number
    FROM leases l
    JOIN customers c ON ( c.management_company_id = l.management_company_id AND c.id = l.primary_customer_id )
    JOIN financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id )
    WHERE
        l.management_company_id = 23
        AND l.property_unit_id IS NOT NULL
        AND l.unit_space_id IS NOT NULL';

2 个答案:

答案 0 :(得分:1)

试试这样:

   WITH financial_move_out_due_days AS 
    ( 
        SELECT property_id, 
            management_company_id, 
            value 
        FROM property_preferences 
        WHERE property_id IN ( 112,332 ) AND management_company_id = 23 
    ),
    another_financial_move_out_due_days AS 
    ( 
        SELECT property_id, 
            management_company_id, 
            value 
        FROM property_preferences 
        WHERE property_id IN ( 112,332 ) AND management_company_id = 23 
    )
    SELECT 
        l.id,
        l.primary_customer_id,
        ( c.name_first || c.name_last ) AS customer_name_full,
        c.email_address AS customer_email_address,
        c.phone_number AS phone_number
    FROM leases l
    JOIN customers c ON ( c.management_company_id = l.management_company_id AND c.id = l.primary_customer_id )
    JOIN financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id )
    JOIN another_financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id )
    WHERE
        l.management_company_id = 23
        AND l.property_unit_id IS NOT NULL
        AND l.unit_space_id IS NOT NULL';

答案 1 :(得分:0)

WITH FirstTable AS
(
),
SecondTable AS
(
),
....