简体   繁体   English

SSRS查询设计器中的SQL SQL语句

[英]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. 因此,目前,我有一个SSRS报告,该报告使用查找功能比较不同的数据集,以比较2013年基准年(起始年)的当前年份。现在,我想更改这些SQL数据集,以生成另一个动态报告一年又一年。 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. 因此,对于第一年,它会给我与去年相同的结果(因为它仍将2014年与2013年进行比较),然后第二年它将对每个月的2015年至2014年进行比较。 I am doing this in the SSRS SQL Query Designer. 我正在SSRS SQL查询设计器中执行此操作。 My SQL statement that shows the current year is: 我显示当前年份的SQL语句是:

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). 现在,我需要编写另一个SQL作为单独的数据集,该数据集将动态查找过去的信息(而不是像我现在已有超过一年的基本报告那样,仅能查找2013信息的静态数据集)。 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. 我认为SQL将与我上面写的类似,但是我不确定如何使它像这样动态。 Essentially for 2014 it should find the exact same information. 从本质上来说,2014年应该找到完全相同的信息。 Its when 2015 comes is when this report will be different than the other. 2015年到来时,该报告将与其他报告有所不同。

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. 我希望您不是UPSET,因为我使用了自己的代码……..我的机器上没有安装ORACLE,因此您必须修改一点(但愿不是)。

I hope this will help you. 我希望这能帮到您。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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