1

Hello I am working on a dataset for a report in SSRS and I have a query which gives the total requests in the backlog :

SELECT 
COUNT(*) as NB
FROM p_rqt WITH (NOLOCK) 
INNER JOIN p_cpy WITH (NOLOCK) ON p_cpy.CpyInCde = p_rqt.OrigCpyInCde 
WHERE 
    CpyTypInCde IN (27, 31) 
    AND p_rqt.RqtNatInCde IN (74, 75, 76) 
    AND HeadRqtInCde = 0 
    AND p_rqt.OrigCpyInCde LIKE CASE WHEN @Client = 0 THEN '%' ELSE @Client END
    AND ((RcvDte < DATEADD(day, 1, @DateDeb) AND RqtEndDte IS NULL)   OR 
(RcvDte < DATEADD(day, 1, @DateDeb) AND RqtEndDte > DATEADD(day, 1, @DateDeb)))

and I want to retrieve the total amount left per day. I tried lot of things like this :

SELECT CONVERT(date,rcvdte,103), count(*) as nb
FROM p_rqt p WITH (NOLOCK) 
INNER JOIN p_cpy WITH (NOLOCK) ON p_cpy.CpyInCde = p.OrigCpyInCde 
WHERE 
    CpyTypInCde IN (27, 31) 
    AND p.RqtNatInCde IN (74, 75, 76) 
    AND HeadRqtInCde = 0 
    AND ((RcvDte < DATEADD(day, 1, '20170901') AND RqtEndDte IS NULL)   OR (RcvDte < DATEADD(day, 1, '20170901') AND RqtEndDte > DATEADD(day, 1, '20170901')))
    group by CONVERT(date,rcvdte,103)
    order by CONVERT(date,rcvdte,103)

I tried inner join subqueries, Sum and other stuff but all I can manage to do is to have the number of records added per day and I want something like this :

date:               NB:
01/01/2017        1950
02/01/2017        1954               (+4 items)
03/01/2017        1945               (-9 items) 

Thank you

shishir
  • 851
  • 3
  • 11
  • 27
loxod
  • 63
  • 1
  • 7
  • 1
    sample data that we can run your query against would be useful, take a look at this: http://www.sqlservercentral.com/blogs/spaghettidba/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ – Tanner Sep 13 '17 at 09:17
  • I would suggest to make the calculations inside SSRS and keep your query as minimal (and fast) as it can be. Inside SSRS you can use RunningValue for cumulative totals https://stackoverflow.com/questions/16462826/how-to-i-get-cumulative-monthly-subtotals-in-ssrs – niktrs Sep 13 '17 at 09:35
  • Well i would like to, but it is design like this it's a table with multiple UNION and they don't want me to do it in another way – loxod Sep 13 '17 at 09:59

2 Answers2

0

Use LAG:

WITH cte AS (
    SELECT
        CONVERT(date, rcvdte, 103) AS date,
        COUNT(*) AS nb
    FROM p_rqt p WITH (NOLOCK) 
    INNER JOIN p_cpy WITH (NOLOCK)
        ON p_cpy.CpyInCde = p.OrigCpyInCde 
    WHERE 
        CpyTypInCde IN (27, 31) AND
        p.RqtNatInCde IN (74, 75, 76) AND
        HeadRqtInCde = 0 AND
        ((RcvDte < DATEADD(day, 1, '20170901') AND RqtEndDte IS NULL)   OR (RcvDte < DATEADD(day, 1, '20170901') AND RqtEndDte > DATEADD(day, 1, '20170901')))
    GROUP BY CONVERT(date, rcvdte, 103)
    ORDER BY CONVERT(date, rcvdte, 103)
)

SELECT
    t1.date,
    (SELECT SUM(t2.nb) FROM cte t2 WHERE t2.date <= t1.date) AS nb,
    CASE WHEN t1.nb - LAG(t1.nb, 1, t1.nb) OVER (ORDER BY t1.date) > 0
         THEN '(+' + (t1.nb - LAG(t1.nb, 1, t1.nb) OVER (ORDER BY t1.date)) + ' items)'
         ELSE '('  + (t1.nb - LAG(t1.nb, 1, t1.nb) OVER (ORDER BY t1.date)) + ' items)'
    END AS difference
FROM cte t1
ORDER BY t1.date;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • It didn't work apart from the difference part, i still have the number of records added per day =/ – loxod Sep 13 '17 at 09:56
0

So i found a solution but it is really slow, i still post the answer anyway

            DECLARE @Tb TABLE (  Colonne1 Datetime, Colonne2 INT )
DECLARE @Debut Datetime = '01/09/2017'
WHILE @Debut < '13/09/2017'
BEGIN
   DECLARE @Compteur int = (
         SELECT 
                COUNT(1) NB 
                FROM p_rqt WITH (NOLOCK) 
                INNER JOIN p_cpy WITH (NOLOCK) ON p_cpy.CpyInCde = p_rqt.OrigCpyInCde 
                WHERE 
                       CpyTypInCde IN (27, 31) 
                       AND p_rqt.RqtNatInCde IN (74, 75, 76) 
                       AND HeadRqtInCde = 0 
                       AND p_rqt.OrigCpyInCde LIKE '%' 
                       AND (
                              (RcvDte < @Debut AND RqtEndDte IS NULL)   
                              OR 
                              (RcvDte < @Debut AND RqtEndDte > @Debut)
                       )
   )
   INSERT INTO @Tb (Colonne1, Colonne2) VALUES (@Debut, @Compteur)
   SET @Debut = DATEADD(day, 1, @Debut)
   IF @Debut > '13/09/2017'
    BREAK
   ELSE
  CONTINUE
  END
  SELECT * FROM @Tb
loxod
  • 63
  • 1
  • 7