• candide (9/1/2015)


    Hi,

    I use SAP Crystal Report V.14 and SS2012, my task is to build a report/pivot with CR for the last 12 months. As I read in several forums it seems an efficient way to first pivot in SS2012 and then create the CR-report on this SP or view.

    The columns are like this:

    artnrcustomer custno status LastResponse

    10007 cust1 2544 St1 20150824

    33139 cust2 5173 St1 20150801

    33139 cust3 667 St3 20150717

    11764 cust4 1746 St4 20150901

    1. Pivot like this

    [font="Arial"]

    09 10 11 12 01 02 03 04 05 06 07 08

    St1 2

    St3 1

    [/font]

    rows: status

    columns: the last 12 months

    pivot: count

    2. The second step would be to

    * parameter the CR for current date

    * get the last 12 months

    As I'm new to CR has anyone a hint for me?

    Here's a dynamic PIVOT for you:

    DECLARE @TD AS date = GETDATE();

    DECLARE @SQL AS varchar(max);

    DECLARE @PIVOT_LIST AS varchar(200) = '';

    CREATE TABLE #DATA (

    artnr int,

    customer varchar(20),

    custno int,

    [status] char(3),

    LastResponse date

    );

    INSERT INTO #DATA (artnr, customer, custno, [status], LastResponse)

    VALUES (10007, 'cust1', 2544, 'St1', '20150824'),

    (33139, 'cust2', 5173, 'St1', '20150801'),

    (33139, 'cust3', 667, 'St3', '20150717'),

    (11764, 'cust4', 1746, 'St4', '20150901');

    CREATE TABLE #Tally (

    GROUPING_DATE date

    );

    WITH Tally AS (

    SELECT DATEADD(day, 1 - DATEPART(DAY, @TD), @TD) AS GROUPING_DATE

    UNION ALL

    SELECT DATEADD(month, -1, GROUPING_DATE)

    FROM Tally

    WHERE DATEADD(month, -1, GROUPING_DATE) >= DATEADD(month, -11, DATEADD(day, 1 - DATEPART(DAY, @TD), @TD))

    )

    INSERT INTO #Tally (GROUPING_DATE)

    SELECT *

    FROM Tally;

    SELECT @PIVOT_LIST = @PIVOT_LIST + '[' + LEFT(REPLACE(CONVERT(char(10), GROUPING_DATE, 112), '-', ''), 6) + '],'

    FROM #Tally

    ORDER BY GROUPING_DATE;

    --PRINT @PIVOT_LIST;

    --DROP TABLE #Tally

    SET @SQL =

    'WITH ALL_GROUPS AS (

    SELECT LEFT(REPLACE(CONVERT(char(10), T.GROUPING_DATE, 112), ''-'', ''''), 6) AS GROUPING_DATE, D.[status]

    FROM #Tally AS T,

    (SELECT DISTINCT [status] FROM #DATA) AS D

    ),

    SELECTED_DATA AS (

    SELECT X.[status], X.GROUPING_DATE, COUNT(*) AS ROW_COUNT

    FROM (

    SELECT D.[status], D.LastResponse, LEFT(REPLACE(CONVERT(char(8), DATEADD(day, 1 - DATEPART(day, D.LastResponse), D.LastResponse), 112), ''-'', ''''), 6) AS GROUPING_DATE

    FROM #DATA AS D

    ) AS X

    GROUP BY X.[status], X.GROUPING_DATE

    ),

    GROUPED_DATA AS (

    SELECT DISTINCT SD.[status], T.GROUPING_DATE, ISNULL(SD.ROW_COUNT, 0) AS ROW_COUNT

    FROM ALL_GROUPS AS T

    LEFT OUTER JOIN SELECTED_DATA AS SD

    ON T.GROUPING_DATE = SD.GROUPING_DATE

    )

    SELECT [status], ' + LEFT(@PIVOT_LIST, LEN(@PIVOT_LIST) - 1) + '

    FROM GROUPED_DATA

    PIVOT (SUM(ROW_COUNT) FOR GROUPING_DATE IN (' + LEFT(@PIVOT_LIST, LEN(@PIVOT_LIST) - 1) + ')) AS PVT

    WHERE [status] IS NOT NULL;';

    PRINT @SQL;

    EXEC (@SQL);

    DROP TABLE #Tally;

    DROP TABLE #DATA;

    Let me know if that works for you.

    EDIT: fixed the number of months of data this generates down to 12 from 13, which I discovered after doing the other fix. This edit just fixes the original post to at least do 12 months of data instead of 13.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)