简体   繁体   English

如何用上个月的值替换“无”单元格

[英]how to replace “Nothing” cell by last month value

I am using SSRS develop a report via Matrix. 我正在使用SSRS通过Matrix开发报告。

I sum the product sales qty every month but if some month sale qty is 0, the cell will be Nothing . 我总结每个月的产品销售数量,但是如果某个月的销售数量为0,则该单元将为Nothing

I'm trying to using Previous function to solve problem but this function seems not work for Matrix object. 我正在尝试使用Previous函数解决问题,但是此函数似乎不适用于Matrix对象。

Is there any way to do this? 有什么办法吗?

1st table is currently report result. 第一个表是当前报告结果。 2nd table is that i want. 第二张桌子是我想要的。

报告

Assuming that you have a table with the structure and content like this: 假设您有一个具有如下结构和内容的表:

CREATE TABLE a (date Date, product_name NVarchar(20), sale_qty Decimal(10,2))
INSERT INTO a (date, product_name, sale_qty) VALUES ('20130510','product A',1)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20130601','product A',0)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20130501','product B',5)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20140205','product A',1)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20140215','product A',1)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20140202','product B',2)
INSERT INTO a (date, product_name, sale_qty) VALUES ('20140301','product A',0)

Then the below sql-statement will work for you (as far as I understood the requirement): 然后下面的sql语句将为您工作(据我了解的要求):

SELECT
      y.Date2 AS 'yyyy/mm'
    , y.product_name
    , ISNULL(z.Month_sale_QTY,0) AS Month_sale_QTY

FROM
    (SELECT
          DATEADD(MONTH, x.number, y.StartDate) AS Date2
        , (SELECT TOP 1 DATEADD(d,1-DATEPART(d,a.date),a.date) FROM a WHERE DATEADD(d,1-DATEPART(d,a.date),a.date) <= DATEADD(MONTH, x.number, y.StartDate) AND a.product_name = p.product_name AND ISNULL(a.sale_qty,0)<>0 ORDER BY 1 DESC) AS Date3
        , p.product_name
    FROM
        master.dbo.spt_values x
    INNER JOIN 
        (SELECT Dateadd(d,1-DATEPART(d, MIN(Date)), MIN(Date)) AS StartDate, MAX(Date) AS EndDate  FROM a) AS y
        ON x.number <= DATEDIFF(MONTH, y.StartDate, y.EndDate)
        ,(SELECT DISTINCT product_name FROM a) AS p WHERE x.type = 'P') y
LEFT JOIN
    (SELECT
          DATEADD(d,1-DATEPART(d,a.date),a.date) AS Date2
        , a.product_name
        , SUM(a.sale_qty) as Month_sale_QTY
    FROM a
    GROUP BY
          DATEADD(d,1-DATEPART(d,a.date),a.date)
        , a.product_name
    ) z ON y.Date3 = z.Date2 AND y.product_name = z.product_name

It returns the following: 它返回以下内容:

yyyy/mm    product_name         Month_sale_QTY
---------- -------------------- ---------------------------------------
2013-05-01 product A            1.00
2013-06-01 product A            1.00
2013-07-01 product A            1.00
2013-08-01 product A            1.00
2013-09-01 product A            1.00
2013-10-01 product A            1.00
2013-11-01 product A            1.00
2013-12-01 product A            1.00
2014-01-01 product A            1.00
2014-02-01 product A            2.00
2014-03-01 product A            2.00
2013-05-01 product B            5.00
2013-06-01 product B            5.00
2013-07-01 product B            5.00
2013-08-01 product B            5.00
2013-09-01 product B            5.00
2013-10-01 product B            5.00
2013-11-01 product B            5.00
2013-12-01 product B            5.00
2014-01-01 product B            5.00
2014-02-01 product B            2.00
2014-03-01 product B            2.00

I'm sure it can be optimized using windowing functions and/or CTE, but as a quick solution this one will work too. 我确信可以使用开窗功能和/或CTE对其进行优化,但是作为一种快速解决方案,它也可以使用。

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

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