• hapsa10 - Monday, October 16, 2017 11:10 AM

    Podrías intentar hacer algo como esto.

    ;With CTE_Source As (
        Select Fl, Col
        , Val = Case
                    When Val Is Null Then 0
                    Else Case
                            When IsNumeric(Val) = 1 Then Cast(Val As Money)
                            Else 0
                         End
                End
        From dbo.Test
    )

    Select
         Sum(Case When t.[Coverage] IS Null Then 0 Else t.[Coverage] End) As [Coverage]
        , Sum(Case When t.[Premium] IS Null Then 0 Else t.[Premium] End) As [Premium]
        , Sum(Case When t.[CALC1] IS Null Then 0 Else t.[CALC1] End) As [CALC1]
        , Sum(Case When t.[CALC1] IS Null Then 0 Else t.[CALC1] End) * 0.50 As [CALC2]
        , (Sum(Case When t.[CALC1] IS Null Then 0 Else t.[CALC1] End) * 0.50) + 10 As [CALC3]
    From(
            SELECT [Coverage] AS [Coverage], [Premium] AS [Premium], [CALC1] AS [CALC1]
            FROM ( SELECT Fl, Col, Val FROM CTE_Source ) As Data
            PIVOT ( SUM(Val) FOR Col IN( [Coverage], [Premium], [CALC1] ) ) AS pvt
        ) As t

    That doesn't actually work as the OP requested.  The goal is to read the formulas from the given table and apply them... not hard-code them.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)