Question

I am using SSRS develop a report via Matrix.

I sum the product sales qty every month but if some month sale qty is 0, the cell will be Nothing.

I'm trying to using Previous function to solve problem but this function seems not work for Matrix object.

Is there any way to do this?

1st table is currently report result. 2nd table is that i want.

Report

Was it helpful?

Solution

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):

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top