4

この選択では、前月と当月の値を取得し、6つの列を表示します:OLDVALUE自体〜OLDVALUE X 28,8%〜OLDVALUE X 8%〜CURVALUE自体〜CURVALUE X 28,8%〜CURVALUE X 8%

しかし、以下に示すように、同じサブクエリで前月を3回取得する必要があります。このようにすると、私のコードは非常に大きくなり、パフォーマンスが低下します(より多くの情報が必要です)。

このサブクエリの結果を同じコンテキストで再び使用できるように「一時」変数を割り当てて、ループ構造の変数のように行ごとに値を変更する方法はありますか?

よろしく。

/* == PARAMETERS == */
DECLARE @ANO INT = 2011
DECLARE @MES INT = 7

/* ===================================== SELECT ===================================== */
SELECT FUNC.CHAPA, FUNC.CODSECAO,
        (SELECT VALPROVFER FROM PFHSTPROV (NOLOCK)
          WHERE CHAPA = FUNC.CHAPA
            AND ANO = CASE WHEN (PROV.MES-1 = 0) THEN PROV.ANO-1 ELSE PROV.ANO END
            AND MES = CASE WHEN (PROV.MES-1 = 0) THEN 12 ELSE PROV.MES-1 END
        ) AS PROVFERMESANT,
        (SELECT VALPROVFER FROM PFHSTPROV (NOLOCK)
          WHERE CHAPA = FUNC.CHAPA
            AND ANO = CASE WHEN (PROV.MES-1 = 0) THEN PROV.ANO-1 ELSE PROV.ANO END
            AND MES = CASE WHEN (PROV.MES-1 = 0) THEN 12 ELSE PROV.MES-1 END
        )*0.288 AS PROVINSSMESANT,
        (SELECT VALPROVFER FROM PFHSTPROV (NOLOCK)
          WHERE CHAPA = FUNC.CHAPA
            AND ANO = CASE WHEN (PROV.MES-1 = 0) THEN PROV.ANO-1 ELSE PROV.ANO END
            AND MES = CASE WHEN (PROV.MES-1 = 0) THEN 12 ELSE PROV.MES-1 END
        ) * 0.08 AS PROVFGTSMESANT,
        PROV.VALPROVFER AS PROVFERMES, PROV.VALPROVFER*0.288 AS PROVINSSMES,
        PROV.VALPROVFER*0.08 AS PROVFGTSMES,
 FROM PFUNC FUNC (NOLOCK)
 JOIN PFHSTPROV PROV (NOLOCK) ON FUNC.CHAPA = PROV.CHAPA 
    AND FUNC.CODCOLIGADA = PROV.CODCOLIGADA
WHERE FUNC.CODSITUACAO = 'A'
    AND ANO = @ANO
    AND MES = @MES
4

1 に答える 1

2
   SELECT FUNC.CHAPA, FUNC.CODSECAO,
            isnull(PFH.VALPROVFER,0) AS PROVFERMESANT,
            isnull(PFH.VALPROVFER,0)*0.288 AS PROVINSSMESANT,
            isnull(PFH.VALPROVFER,0) * 0.08 AS PROVFGTSMESANT,

            PROV.VALPROVFER AS PROVFERMES, PROV.VALPROVFER*0.288 AS PROVINSSMES,
            PROV.VALPROVFER*0.08 AS PROVFGTSMES,
     FROM PFUNC FUNC (NOLOCK)
     JOIN PFHSTPROV PROV (NOLOCK) ON FUNC.CHAPA = PROV.CHAPA 
        AND FUNC.CODCOLIGADA = PROV.CODCOLIGADA

     LEFT JOIN PFHSTPROV PFH (NOLOCK) on 
                 ( (FUNC.CHAPA = PFH.CHAPA) 
                            AND  PFH.ANO = CASE WHEN (PROV.MES-1 = 0) THEN PROV.ANO-1 ELSE PROV.ANO END
                            AND PFH.MES = CASE WHEN (PROV.MES-1 = 0) THEN 12 ELSE PROV.MES-1 END        
                 ) 



    WHERE FUNC.CODSITUACAO = 'A'
        AND ANO = @ANO
        AND MES = @MES
于 2012-08-01T09:21:50.473 に答える