简体   繁体   中英

Year Over Year SQL Statement in SSRS Query Designer

So at the moment I have an SSRS report that uses lookup functions to compare different datasets to compare the current year over the base (the starting year) of 2013. Now I want to change these SQL datasets to make another report that will do a dynamic year over year. So for the first year it would give me the same results as year over year (since it would be comparing 2014 to 2013 still) and then next year it would compare 2015 to 2014 for each month. I am doing this in the SSRS SQL Query Designer. My SQL statement that shows the current year is:

SELECT        PLN.ALN, TO_CHAR(PLN.FT_DTE, 'YY/MM') AS "MM-YY", COUNT(*) AS ALT, 
                     trim(CONCAT(PLN.ALN, to_char(PLN.FT_DTE, 'YY/MM'))) AS MONTHKEY, 
                     PERFORMANCE.STATUS
FROM            PLN, PERFORMANCE
WHERE        PLN.OG = PERFORMANCE.OG AND 
                         PLN.DST = PERFORMANCE.ACT_DST AND 
                         PLN.FT_N = PERFORMANCE.FT AND 
                         PLN.FT_DTE = PERFORMANCE.FT_DT AND 
                         PLN.TIL = PERFORMANCE.ACT_TIL AND (PLN.FT_DTE BETWEEN 
                         :P_Start_Date AND :P_End_Date) AND (PLN.DST = 'FDS') AND (PERFORMANCE.STUS = 'RAR') AND 
                         (PLN.ALN IS NOT NULL)
GROUP BY PLAN.ALN, TO_CHAR(PLN.FT_DTE, 'YY/MM'), 
                         PERFORMANCE.STUS
ORDER BY PLN.ALN, "MM-YY"

This allows me to specify a date and it will count the number of occurrences per month of a certain field.

Now I need to write another SQL as a separate dataset that will find last years information dynamically (instead of a static dataset that just finds 2013 information like I have now for a year over base report). I assume the SQL will be similar to the one I wrote above but I'm not sure how to make it dynamic like that. Essentially for 2014 it should find the exact same information. Its when 2015 comes is when this report will be different than the other.

I think i have the solution for you :

/*  YEAR_N      :   Refers the DataSet of the year N        */
/*  YEAR_N_M_1  :   Refers the DataSet of the year N-1  */

SELECT

         YEAR_N.MONTHKEY
        ,YEAR_N.ALN
        ,YEAR_N."MM-YY"
        ,YEAR_N.ALT
        ,YEAR_N.STATUS
        ,'***' AS SEP
        ,YEAR_N_M_1.MONTHKEY
        ,YEAR_N_M_1.ALN
        ,YEAR_N_M_1."MM-YY"
        ,YEAR_N_M_1.ALT
        ,YEAR_N_M_1.STATUS
FROM
(SELECT
         PLN.ALN
        ,TO_CHAR(PLN.FT_DTE, 'YY/MM') AS "MM-YY"
        ,COUNT(*) AS ALT
        ,TRIM(CONCAT(PLN.ALN, to_char(PLN.FT_DTE, 'YY/MM'))) AS MONTHKEY
        ,PERFORMANCE.STATUS
        ,TO_NUMBER(to_char(PLN.FT_DTE, 'YY')) _YEAR
        ,to_char(PLN.FT_DTE, 'MM') _MONTH
FROM
     PLN
    ,PERFORMANCE
WHERE
        PLN.OG = PERFORMANCE.OG
AND     PLN.DST = PERFORMANCE.ACT_DST 
AND     PLN.FT_N = PERFORMANCE.FT 
AND     PLN.FT_DTE = PERFORMANCE.FT_DT
AND     PLN.TIL = PERFORMANCE.ACT_TIL
AND     (PLN.FT_DTE BETWEEN :P_Start_Date AND :P_End_Date)
AND     (PLN.DST = 'FDS')
AND     (PERFORMANCE.STUS = 'RAR')
AND     (PLN.ALN IS NOT NULL)
GROUP BY
        PLAN.ALN, TO_CHAR(PLN.FT_DTE, 'YY/MM'),PERFORMANCE.STUS) AS YEAR_N
LEFT JOIN

(SELECT
         PLN.ALN
        ,TO_CHAR(PLN.FT_DTE, 'YY/MM') AS "MM-YY"
        ,COUNT(*) AS ALT
        ,TRIM(CONCAT(PLN.ALN, to_char(PLN.FT_DTE, 'YY/MM'))) AS MONTHKEY
        ,PERFORMANCE.STATUS
        ,TO_NUMBER(to_char(PLN.FT_DTE, 'YY')) _YEAR
        ,to_char(PLN.FT_DTE, 'MM') _MONTH
FROM
     PLN
    ,PERFORMANCE
WHERE
        PLN.OG = PERFORMANCE.OG
AND     PLN.DST = PERFORMANCE.ACT_DST 
AND     PLN.FT_N = PERFORMANCE.FT 
AND     PLN.FT_DTE = PERFORMANCE.FT_DT
AND     PLN.TIL = PERFORMANCE.ACT_TIL
AND     (PLN.FT_DTE BETWEEN :P_Start_Date AND :P_End_Date)
AND     (PLN.DST = 'FDS')
AND     (PERFORMANCE.STUS = 'RAR')
AND     (PLN.ALN IS NOT NULL)
GROUP BY
        PLAN.ALN, TO_CHAR(PLN.FT_DTE, 'YY/MM'),PERFORMANCE.STUS) AS YEAR_N_M_1
    ON 
        YEAR_N._YEAR    = YEAR_N_M_1._YEAR-1
    AND YEAR_N._MONTH   = YEAR_N_M_1._MONTH

ORDER BY PLN.ALN, "MM-YY"

I hope you are not UPSET because i used your own code hhh.. I don't have ORACLE installed on my machine so you have to modify a little bit (i hope not) the code.

I hope this will help you.

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM