-1

我想根据参数值有条件地选择列。如果@TypePerf = 0 则选择a、b、c 列,如果@TypePerf = 1 则选择a、c 列...等我想要一种:

 if (@typePerf = 1 or @typePerf = 0) then
 select ...

-- 0,1 Begin
        (vlbench1.Price-isnull(vlbench1D.Price,vlbenchFirstDispo.Price))/isnull(vlbench1D.Price,vlbenchFirstDispo.Price) as VLPerf1Day,
        (vlbench1.Price-isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price) as VLPerfWTD,
        (vlbench1.Price-isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price) as VLPerfMTD,
        (vlbench1.Price-isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price) as VLPerfQTD,
        (vlbench1.Price-isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price) as VLPerfYTOD,
        (vlbench1.bench-isnull(vlbench1D.bench,vlbenchFirstDispo.bench))/isnull(vlbench1D.bench,vlbenchFirstDispo.bench) as BenchPerf1Day,
        (vlbench1.bench-isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench) as BenchPerfWTD,
        (vlbench1.bench-isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench) as BenchPerfMTD,
        (vlbench1.bench-isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench) as BenchPerfQTD,
        (vlbench1.bench-isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench) as BenchPerfYTOD,
        -- 0,1 End
if (if (@typePerf = 2 or @typePerf = 0) then

select ...      
            -- 0,2 Begin
            (vlbench1.Price-isnull(vlbench1D.Price,vlbenchFirstDispo.Price))/isnull(vlbench1D.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbench1D.bench,vlbenchFirstDispo.bench))/isnull(vlbench1D.bench,vlbenchFirstDispo.bench) as RelativePerf1D,
            (vlbench1.Price-isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench) as RelativePefYTOD,
            (vlbench1.Price-isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench) as RelativePerfMTD,
            (vlbench1.Price-isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench) as RelativePerfWTD,
            (vlbench1.Price-isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench) as RelativePerfQTD,
            -- 0,2 End

if(@typeperf = 2 or 0) then select


    USE [F2B_VOM_TST]
    GO
    /****** Object:  StoredProcedure [p_data].[P_Moa_GetAumNavBenchOnPtsId]    Script Date: 11/10/2013 10:36:28 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [p_data].[P_Moa_GetAumNavBenchOnPtsId]
    @DateDebut dateTime,@DateFin datetime,@Part integer,@Devise varchar(10),@PtfId integer,@Frequence varchar(1),@TypePerf integer=0

    WITH EXEC AS CALLER
    AS

    Declare @AumQuery AS NVarchar(max)


    -- 0,1,2,3 Begin

    create table #TEMP_VL1
        ( [PtsId] int,
          [Date]  datetime
        )

    -- on calcule le dernier jour de l'année précedente à la date de debut. 
    declare @FinAnneePrecedente datetime 

    select @FinAnneePrecedente=Max(Date) from TD_Time where Date<Cast((Year(@DateDebut)) as varchar)+'0101' and 
          WeekDayLib not in ('Sunday','Saturday') 


    --CASE 
    --    WHEN @selectField1 THEN Field1
    --    WHEN @selectField2 THEN Field2
    --    ELSE someDefaultField
    --END

    select 

    pp.Dat as Date,ptf.Lib as PtfLib, pt.Lib ,Isin,DevCot,pp.Price,
    -- 0,1,2,3 End



    -- 0,3 Begin
    --case 
    --  when @TypePerf=0 or @TypePerf=3 then
     pp.Aum,pp.AumEur, 
    ---end
    -- 0,3 End

    -- 0,1,2,3 Begin 
    bp.Price bench,
    -- 0,1,2,3 End

    -- 0 Begin
    ptf.Cod,
    pt.PtsId,ptf.PtfId,
    ptf.Gest,ptf.IsDedicatedFund, 
    case 
      when ptf.Gest='Actions' and ptf.IsDedicatedFund=0 then 1
      when ptf.Gest='Diversifiée' and ptf.IsDedicatedFund=0 then 2 
      when ptf.Gest='Taux' and ptf.IsDedicatedFund=0 then 3
      when ptf.Gest='Profilée' and ptf.IsDedicatedFund=0 then 4 
      when ptf.IsDedicatedFund=1 then 5
    end Classe,
    ti.WeekDayLib,ti.MonthYear_Lib,ti.Annee
    -- 0 End

    -- 0,1,2,3 Begin
    into #VL_BENCHMARK
    from TF_PartPrice pp  
    inner join TD_Time ti on ti.Date=pp.Dat
    inner join TD_Part pt on pp.PtsId=pt.PtsId 
    left join TF_BenchmarkPrice bp on pp.Dat=bp.Dat and bp.TypPosId=3 and bp.IsCurCtb=1  and bp.PtsId=pp.PtsId 
    inner join TD_Portefeuille ptf on pt.PtfId=ptf.PtfId 
    where 
      pp.IsCurCtb=1 and pp.TypPosId=3 and 
      ptf.Statut=1 and 
      pp.Dat between @FinAnneePrecedente and @DateFin and
      ptf.IsTraded=1


    if (@Frequence='q') 
    begin
        insert into #TEMP_VL1 (PtsId,Date ) 
        select PtsId,max(Date) 
        from #VL_BENCHMARK
        group by  PtsId,Date
    end 

    -- dans le cas d'un rapport monthly, 
    if (@Frequence='m') 
      begin
        insert into #TEMP_VL1 (PtsId,Date ) 
        select PtsId,max(Date) 
        from #VL_BENCHMARK
        group by  PtsId,MonthYear_Lib
      end

    if (@Frequence='a') 
    begin
        insert into #TEMP_VL1 (PtsId,Date ) 
        select PtsId,max(Date) 
        from #VL_BENCHMARK
        group by  PtsId,Annee
    end 

    if (@Frequence='w') 
    begin
        insert into #TEMP_VL1 (PtsId,Date ) 
        select PtsId,max(Date) 
        from #VL_BENCHMARK
        where DATEPART(weekday,Date) in (2,3,4,5)
        group by  PtsId,DatePart(week,Date),DATEPART(year,Date)
    end 

    -- 0,1,2,3 End


    -- 0,3 Begin
    -- Calcul AumEur par Ptf 
    select Date, PtfId,sum(AumEur) PtfAumEur
    into #SOMME_AUM_EUR
    from #VL_BENCHMARK  
    group by Date,PtfId 
    -- 0,3 End  

    select  
            -- 0,1,2,3 Begin
            vlbench1.Date,
            vlbench1.PtfLib,
            vlbench1.Lib,
            vlbench1.Isin,
            vlbench1.DevCot,
            vlbench1.Price ,
            -- 0,1,2,3 End

            -- 0,3 Begin
            vlbench1.Aum,
            vlbench1.AumEur,
            -- 0,3 End

            -- 0,1,2,3 Begin
            isnull(vlbench1.bench,0)bench,
            --0,1,2,3 End

            -- 0 Begin
            vlbench1.Cod,
            vlbench1.PtsId,
            vlbench1D.Price as PreviousPrice,
            vlbench1D.bench as PreviousBench,
            vlbenchYTD.Price as PreviousYearPrice,
            vlbenchYTD.bench as PreviousYearBench,
            vlbenchMTD.Price as PreviousMonthPrice,
            vlbenchMTD.bench as PreviousMonthBench,
            vlbenchWTD.Price as PreviousWeekPrice,
            vlbenchWTD.bench as PreviousWeekBench,
            vlbenchQTD.Price  as PreviousQuaterPrice,
            vlbenchQTD.bench as PreviousQuaterBench,
            vlbenchFirstDispo.Price as FirstPriceDispo,
            vlbenchFirstDispo.bench as FirstBenchDispo,
            -- 0 End

            -- 0,1 Begin
            (vlbench1.Price-isnull(vlbench1D.Price,vlbenchFirstDispo.Price))/isnull(vlbench1D.Price,vlbenchFirstDispo.Price) as VLPerf1Day,
            (vlbench1.Price-isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price) as VLPerfWTD,
            (vlbench1.Price-isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price) as VLPerfMTD,
            (vlbench1.Price-isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price) as VLPerfQTD,
            (vlbench1.Price-isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price) as VLPerfYTOD,
            (vlbench1.bench-isnull(vlbench1D.bench,vlbenchFirstDispo.bench))/isnull(vlbench1D.bench,vlbenchFirstDispo.bench) as BenchPerf1Day,
            (vlbench1.bench-isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench) as BenchPerfWTD,
            (vlbench1.bench-isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench) as BenchPerfMTD,
            (vlbench1.bench-isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench) as BenchPerfQTD,
            (vlbench1.bench-isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench) as BenchPerfYTOD,
            -- 0,1 End

            -- 0,2 Begin
            (vlbench1.Price-isnull(vlbench1D.Price,vlbenchFirstDispo.Price))/isnull(vlbench1D.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbench1D.bench,vlbenchFirstDispo.bench))/isnull(vlbench1D.bench,vlbenchFirstDispo.bench) as RelativePerf1D,
            (vlbench1.Price-isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchYTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchYTD.bench,vlbenchFirstDispo.bench) as RelativePefYTOD,
            (vlbench1.Price-isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchMTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchMTD.bench,vlbenchFirstDispo.bench) as RelativePerfMTD,
            (vlbench1.Price-isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchWTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchWTD.bench,vlbenchFirstDispo.bench) as RelativePerfWTD,
            (vlbench1.Price-isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price))/isnull(vlbenchQTD.Price,vlbenchFirstDispo.Price) - (vlbench1.bench-isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench))/isnull(vlbenchQTD.bench,vlbenchFirstDispo.bench) as RelativePerfQTD,
            -- 0,2 End

            -- 0,3 Begin
            sumAumEur.PtfAumEur,
            -- 0,3 End

            -- 0 Begin
            vlbench1.Gest,
            vlbench1.IsDedicatedFund,
            vlbench1.Classe
            -- 0 End

    -- 0,1,2,3 Begin
    from #VL_BENCHMARK vlbench1 
    left join #VL_BENCHMARK vlbench1D on vlbench1D.PtsId=vlbench1.PtsId and vlbench1D.Date=(select max(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date<vlbench1.Date)
    left join #VL_BENCHMARK vlbenchYTD on vlbenchYTD.PtsId=vlbench1.PtsId and vlbenchYTD.Date=(select max(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date<DATEADD(yy,DATEDIFF(yy,0,vlbench1.Date),0) ) 
    left join #VL_BENCHMARK vlbenchMTD on vlbenchMTD.PtsId=vlbench1.PtsId and vlbenchMTD.Date=(select max(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date<DATEADD(mm,DATEDIFF(mm,0,vlbench1.Date),0))
    left join #VL_BENCHMARK vlbenchWTD on vlbenchWTD.PtsId=vlbench1.PtsId and vlbenchWTD.Date=(select max(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date<vlbench1.Date and datename(dw,Date)='Thursday')
    left join #VL_BENCHMARK vlbenchQTD on vlbenchQTD.PtsId=vlbench1.PtsId and vlbenchQTD.Date=(select max(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date<DateAdd(qq,DATEPART(Quarter ,vlbench1.Date)-1 ,DATEADD(yy,DATEDIFF(yy,0,vlbench1.Date),0)))
    -- 0,1,2,3 End

    -- 0,1,2,3 Begin
    left join #VL_BENCHMARK vlbenchFirstDispo on vlbenchFirstDispo.PtsId=vlbench1.PtsId and vlbenchFirstDispo.Date=(select min(Date) from #VL_BENCHMARK where PtsId=vlbench1.PtsId and Date>DATEADD(yy,DATEDIFF(yy,0,vlbench1.Date),0) )
    -- 0,1,2,3 End

    -- 0,3 Begin 
    left join #SOMME_AUM_EUR sumAumEur on sumAumEur.PtfId=vlbench1.PtfId and sumAumEur.Date=vlbench1.Date
    -- 0,3 End

    -- 0,1,2,3 Begin
    inner join #TEMP_VL1 tpvl1 on vlbench1.Date=tpvl1.Date and vlbench1.PtsId=tpvl1.PtsId
    -- 0,1,2,3 End

    -- 0,1,2,3 Begin
    where vlbench1.Date between @DateDebut and  @DateFin
      and (@Part=-1 or (@Part<>-1 and vlbench1.PtsId=@Part))
      and (@Devise='All' or (@Devise<>'All' and vlbench1.DevCot=@Devise))
      and (@PtfId=-1 or (@PtfId<>-1 and vlbench1.PtfId=@PtfId))
    order by vlbench1.Date,vlbench1.Classe,vlbench1.PtfLib,vlbench1.Lib
    drop table #VL_BENCHMARK 
    -- 0,1,2,3 End

    -- 0,3 Begin 
    drop table #SOMME_AUM_EUR
    -- 0,3 End

    -- 0,1,2,3 Begin 
    drop table #TEMP_VL1 
    -- 0,1,2,3 End

    exec P_Moa_GetAumNavBenchOnPtsId 
    @DateDebut ='1-10-2012',@DateFin ='1-10-2013',@Part=-1,@Devise ='All',@PtfId =-1,@Frequence ='w', @TypePerf =1
4

1 回答 1

0
CASE
    WHEN @typePerf = 2 THEN
        SELECT ColA, ColB From MyTable
    WHEN @typePerf = 3 THEN
        SELECT ColB, ColC From MyTable
    ELSE
        SELECT ColC, ColD From MyTable
END

或者

DECLARE @MyWhere varchar(75)    
DECLARE @MySQL varchar(1000)
SELECT @MySQL "SELECT "
CASE
    WHEN @typePerf = 2 THEN
        SELECT @MySQL = @MySQL + 'ColA, ColB '
    WHEN @typePerf = 3 THEN
        SELECT @MySQL = @MySQL + 'ColB, ColC '
    ELSE
        SELECT @MySQL = @MySQL + 'ColC, ColD '
END

SELECT @MySQL = @MySQL + 'From MyTable WHERE MyName = @MyWhere'


EXECUTE sp_executesql @MySQL, N'@MyWhere nvarchar(75)', @MyWhere = @MyWhere
于 2013-10-14T10:27:46.583 回答