• this is basically the same solution as Gails in a different format; it's just a CROSS JOINED CTE to get the variable instead of a direct sub select.

    CREATE VIEW myVIEW

    AS

    WITH MyCTE

    AS (SELECT TOP 1

    nomcode AS RetainedVariable

    FROM ctlfil

    WHERE recid = 24

    ORDER BY

    nomcode)

    SELECT

    dbo.nomfil.nomcode,

    ISNULL(calc.val, 0) AS ytdper,

    calc.costcentre

    FROM dbo.nomfil

    LEFT OUTER JOIN (SELECT

    CASE

    WHEN nomfil_1.type IN ( 3, 4 )

    THEN

    nomtrn.nomcode

    ELSE

    MyCTE.RetainedVariable

    END AS nomcode,

    SUM(dbo.nomtrn.val * ( 1 - 2 * dbo.nomtrn.crddbt )) AS val,

    dbo.nomtrn.costcentre

    FROM dbo.nomtrn

    INNER JOIN dbo.nomfil AS nomfil_1

    ON dbo.nomtrn.nomcode = nomfil_1.nomcode

    CROSS JOIN MyCTE --there better be only one row!

    WHERE ( dbo.nomtrn.period < (SELECT

    glpernum / 100 * 100 AS Expr1

    FROM dbo.perfil) )

    GROUP BY

    CASE

    WHEN nomfil_1.type IN ( 3, 4 )

    THEN

    nomtrn.nomcode

    ELSE

    MyCTE.RetainedVariable

    END,

    dbo.nomtrn.costcentre) AS calc

    ON calc.nomcode = dbo.nomfil.nomcode

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!