Home Forums SQL Server 2008 T-SQL (SS2K8) roll up the data as per period between start and enddate RE: roll up the data as per period between start and enddate

  • Your code was close to what I needed except for the eventstartdate need to be in date format(Not just year or month or quarter number)

    BEGIN

    ;with Vol_Aggregatedata as

    (Select CASE WHEN @ipperiod = 'day' THEN CONVERT(char(10),EventStartDate,126)

    WHEN @ipperiod = 'week' THEN dateadd(week, datediff(week, 0, Agg.EventStartDate), 0)

    WHEN @ipPeriod = 'quarter' THEN dateadd(qq, datediff(qq, 0, Agg.EventStartDate), 0)

    WHEN @ipPeriod = 'month' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, Agg.EventStartDate), 0)

    END as Eventstartdate,

    Agg.ProductTypeID,

    Agg.ProductTypeName,

    Agg.LoanProgramTypeId,

    MIN(MetricValue) AS MetricValall ,

    SUM(SampleSize) as SampleSizeall

    FROM [Reporting].[AggregatedRatesforWidgets] Agg with (NOLOCK)

    WHERE Agg.EventStartDate >= @ipstartdate

    and Agg.EventStartDate <=@ipenddate

    and Agg.LoanProgramTypeID= @ipProgramtypeID

    GROUP BY CASE WHEN @ipperiod = 'day' THEN CONVERT(char(10),EventStartDate,126)

    WHEN @ipperiod = 'week' THEN dateadd(week, datediff(week, 0, Agg.EventStartDate), 0)

    WHEN @ipPeriod = 'quarter' THEN dateadd(qq, datediff(qq, 0, Agg.EventStartDate), 0)

    WHEN @ipPeriod = 'month' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, Agg.EventStartDate), 0)

    END

    ,Agg.ProductTypeID,Agg.ProductTypeName,Agg.LoanProgramTypeId

    )

    Select

    vcy.EventStartDate as EventStartDate,

    vcy.ProductTypeID as ProductTypeID,

    vcy.ProductTypeName as ProductTypeName,

    vcy.LoanProgramTypeID AS LoanProgramTypeId,

    'ALL' asLoanstate,

    vcy.MetricValall as MetricValue,

    vcy.SampleSizeall as SampleSize

    FROM Vol_Aggregatedata vcy

    order by Eventstartdate ,loanstate

    END