(总和(作为别名的情况......)

时间:2015-10-15 21:59:59

标签: sql teradata

SELECT q1.reportdate
    ,q1.eid
    ,(cast(totalweight AS DECIMAL(20, 2)) / 60)
FROM (
    SELECT last_day(LNDSpace.UW_Review_Ticket.Review_Date) AS reportdate
        ,LNDSpace.UW_Review_Ticket.Underwriter_EID AS eid
        ,(
            sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.Loan_Type) = 'Conventional'
                        AND (LNDSpace.UW_Review_Ticket.Review_State) = 'New'
                        AND (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) <> 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END) + sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) = 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END)
            ) * 8 + (
            sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.Loan_Type) = 'VA'
                        AND (LNDSpace.UW_Review_Ticket.Review_State) = 'New'
                        AND (LNDSpace.UW_Review_Ticket.Review_Type) IN ('Appraisal / LAPP')
                        AND (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) <> 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END)
            ) * 40 AS totalweight
    FROM LNDSpace.UW_Review_Ticket
    GROUP BY 1, 2
    ) AS q1

我需要每个&#34;总和&#34;成为别名...这可能吗?

( sum(case when ( LNDSpace.UW_Review_Ticket.Loan_Type )='VA' and (   LNDSpace.UW_Review_Ticket.Review_State )='New' and ( LNDSpace.UW_Review_Ticket.Review_Type ) in ('Appraisal / LAPP') 
and  ( LNDSpace.UW_Review_Ticket.SH_2R_Denial )='No' and ( LNDSpace.UW_Review_Ticket.SH_Appeal )='No' and ( LNDSpace.UW_Review_Ticket.Decision )<>'Withdrawn' then 1 else 0 end ) as TEST1) 

1 个答案:

答案 0 :(得分:0)

使用派生表,您可以将此总和作为别名...

SELECT q1.reportdate
    ,q1.eid
    ,(cast(totalweight AS DECIMAL(20, 2)) / 60)
FROM (
    SELECT DT.reportdate
        ,DT. eid
        ,(
            DT.sum1 + 
            DT.sum2
            ) * 8 
            + DT.sum3 * 40 AS totalweight
    FROM 
            (Select last_day(LNDSpace.UW_Review_Ticket.Review_Date) AS reportdate,
            LNDSpace.UW_Review_Ticket.Underwriter_EID AS eid,
            sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.Loan_Type) = 'Conventional'
                        AND (LNDSpace.UW_Review_Ticket.Review_State) = 'New'
                        AND (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) <> 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END) as sum1 ,
        sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) = 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END) as Sum2 ,

         sum(CASE 
                    WHEN (LNDSpace.UW_Review_Ticket.Loan_Type) = 'VA'
                        AND (LNDSpace.UW_Review_Ticket.Review_State) = 'New'
                        AND (LNDSpace.UW_Review_Ticket.Review_Type) IN ('Appraisal / LAPP')
                        AND (LNDSpace.UW_Review_Ticket.SH_2R_Denial) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.SH_Appeal) = 'No'
                        AND (LNDSpace.UW_Review_Ticket.Decision) <> 'Withdrawn'
                        THEN 1
                    ELSE 0
                    END) as sum3                            
                    FROM LNDSpace.UW_Review_Ticket 
            )DT
    GROUP BY 1, 2
    ) AS q1

请在继续操作之前检查您的查询输出。