Get consolidated results with the following tables

I have a script. Here is my table structure:

ID          LoginDate             RemovalDate
----------------------------------------
1           2009/08/01            NULL
2           2009/09/12            2010/01/02
3           2009/08/31            2009/10/29
4           2010/02/17            NULL
5           2009/10/18            2009/11/22

I want the consolidated results of how many identifiers were not deleted in a given month. So the result set should be

Date         NotRemoved_ID
--------------------------
2009/08      2
2009/09      3
2009/10      3 [One ID got removed in 2009/10]
2010/02      2 [Two got removed in 2009/11 and 2010/01]

Please, help.

+3
source share
3 answers
WITH a AS (SELECT CAST('20090801' AS datetime) LoginDate,
                  CAST(NULL AS datetime) RemovalDate
           UNION ALL SELECT '20090912', '20100102'
           UNION ALL SELECT '20090831', '20091029'
           UNION ALL SELECT '20100217', NULL
           UNION ALL SELECT '20091018', '20091122'),
b AS (SELECT LoginDate [Date], 1 [Amount] FROM a
      UNION ALL SELECT RemovalDate, -1 FROM a),
c AS (SELECT DATEADD(month, DATEDIFF(month, 0, [Date]), 0) [Month], [Amount]
      FROM b),
d AS (SELECT [Month], SUM([Amount]) [Amount] FROM c GROUP BY [Month]),
e AS (SELECT d.[Month], SUM(d2.[Amount]) [Amount]
      FROM d JOIN d d2 ON d2.[Month] <= d.[Month] GROUP BY d.[Month])
SELECT [Month], [Amount] FROM e
+1
source

Away from the beautiful and probably a lot left for optimization, but it is tested and works with SQL Server.

Be sure to replace all @Table links with the actual tab name.

DECLARE @Table TABLE (ID INTEGER, LoginDate DATETIME, RemovalDate DATETIME)

INSERT INTO @Table
SELECT           1, '2009/08/01', NULL 
UNION ALL SELECT 2, '2009/09/12', '2010/01/02'
UNION ALL SELECT 3, '2009/08/31', '2009/10/29' 
UNION ALL SELECT 4, '2010/02/17', NULL 
UNION ALL SELECT 5, '2009/10/18', '2009/11/22' 

SELECT  CAST(ld.yr AS VARCHAR(4)) + '/' + RIGHT('0' + CAST(ld.mnth AS VARCHAR(2)), 2), ld.RunningTotal - ISNULL(rd.RunningTotal, 0)
FROM    (
          SELECT    yr, mnth, RunningTotal = MAX(RunningTotal)
          FROM      (
                      SELECT    yr = YEAR(t1.LoginDate), mnth = MONTH(t1.LoginDate), RunningTotal = COUNT(*)
                      FROM      @Table t1
                                CROSS JOIN @Table t2 
                      WHERE     t1.LoginDate >= t2.LoginDate
                      GROUP BY  t1.LoginDate
                    ) ld
          GROUP BY  ld.yr, ld.mnth
        ) ld
        LEFT OUTER JOIN (
          SELECT    yr, mnth, RunningTotal = MAX(RunningTotal)
          FROM      (
                      SELECT    yr = YEAR(t1.RemovalDate), mnth = MONTH(t1.RemovalDate), RunningTotal = COUNT(*)
                      FROM      @Table t1
                                CROSS JOIN @Table t2 
                      WHERE     t1.RemovalDate >= t2.RemovalDate
                      GROUP BY  t1.RemovalDate
                    ) ld
          GROUP BY  ld.yr, ld.mnth
        ) rd ON rd.yr <= ld.yr AND rd.mnth <= ld.mnth
ORDER BY  
        1, 2                      
+1
source

, - :

Select LoginDate , dbo.fn_NotRemoved( LoginDate ) From YourTable

, fn_NotRemoved . .

hope this helps

0
source

Source: https://habr.com/ru/post/1746317/


All Articles