calculating a measure in T-SQL

RogerBinny1234 41 Reputation points
2021-01-30T01:43:52.287+00:00

Hi,

I am required to create a measure for which I am struggling to come up with a query.

My requirement is:

"Find the number of users who had a transaction over the last running 7 days (for any type of account) and then divide it by all the users with at least one account open in that time period".

Here are my sample data sets.

This is the Accounts Table
62103-image.png

This is the Fact table which shows the transactions
62142-image.png

If I could give the same data sets in non picture format then this is how it looks like

accountid	accounttype	openedDT	ClosedDT	CurrentStatus	UserID  
1	Checkings	01/01/2020 19:33	12/01/2020 00:00	Closed	101  
1	Checkings	13/01/2020 13:11	15/01/2020 08:30	Closed	101  
1	Checkings	16/01/2020 22:11	Null	Open	101  
2	Checkings	13/01/2020 18:23	Null	Open	101  
3	Checkings	15/01/2020 22:13	Null	Open	102  
4	Savings	15/01/2020 13:13	19/01/2020 00:00	Closed	102  
5	Checkings	02/02/2020 09:33	Null	Open	103  
6	Checkings	22/01/2020 11:33	Null	Open	104  
7	Business	21/01/2020 00:13	Null	Open	105  
8	Checkings	02/02/2020 13:13	05/08/2020 07:42	Closed	106  
9	Checkings	08/09/2020 17:17	Null	Open	106  
10	Savings	06/11/2020 02:32	Null	Open	107  
  

accountid	transactionDate	transCount  
1	02/01/2020 11:12	2  
1	03/01/2020 10:12	5  
1	03/01/2020 12:15	7  
1	14/01/2020 16:18	1  
1	15/01/2020 07:30	2  
1	28/01/2020 06:06	22  
2	14/01/2020 17:17	2  
2	15/01/2020 01:01	1  
2	22/02/2020 06:30	1  
3	30/03/2020 07:11	4  
3	30/04/2020 08:12	4  
3	30/05/2020 07:02	4  
4	12/02/2020 06:06	3  
8	03/03/2020 07:07	1  
10	13/12/2020	22  
  

I require to worry about only open accounts...so we can easily ignore the closed accounts.

Also, this metric should be able to give the result for any given day of the year (but we don't have a time dimension).

Finally, the result should be consistent even if new transactions come in.

Azure SQL Database
SQL Server Integration Services
SQL Server Analysis Services
SQL Server Analysis Services
A Microsoft online analytical data engine used in decision support and business analytics, providing the analytical data for business reports and client applications such as Power BI, Excel, Reporting Services reports, and other data visualization tools.
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 129.4K Reputation points MVP Volunteer Moderator
    2021-01-30T18:23:55.737+00:00

    This is how the question was asked

    Asked by whom in which context?

    Anyway, I think you need to talk to those persons for clarification. I can't make heads or tails of that accounts table. There are multiple rows for the same accountid what does that mean? There are four rows for accountid 1, three for userid 101 and one for userid 107. I could get that accountid may be related to the user, but the we would need a userid in transactions.

    Your expected output is also very strange. Between Jan 1st and Jan 10th the number of users for the last 7 days varies between 0 and three, but there is only a single account that has transactions in this interval?

    It seems that you need to go back and investigate what the users are really looking for, but here is a query that you maybe can work from. The dates table is your date dimension.

    SELECT d.d, t.Last7DaysUserCnt, a.OpenAcc, isnull(t.Last7DaysUserCnt / nullif(a.OpenAcc, 0), 0)
    FROM   NorthNumbers..Dates d
    CROSS  APPLY (SELECT COUNT(DISTINCT a.userid)
                  FROM   transactions t
                  JOIN   accounts a ON t.accountid = a.accountid
                  WHERE  convert(date, t.tdate) BETWEEN dateadd(DAY, -6, d.d) AND d.d) AS t(Last7DaysUserCnt)
    CROSS  APPLY (SELECT COUNT(DISTINCT a.userid)
                  FROM   accounts a
                  WHERE  a.createddate <= d.d
                    AND  a.closeddate >= dateadd(DAY, -6, d.d)) AS a (OpenAcc)
    WHERE  d.d BETWEEN '20200101' AND '20200110'
    ORDER BY d.d
    

2 additional answers

Sort by: Most helpful
  1. RogerBinny1234 41 Reputation points
    2021-01-30T15:35:31.98+00:00

    Here are the Insert statments

    Insert Into accounts
    SELECT 1,'Checkings', '01/01/2020','12/01/2020','Closed',101
    UNION ALL
    SELECT 1,'Checkings', '13/01/2020','15/01/2020','Closed',101
    UNION ALL
    SELECT 1,'Checkings', '16/01/2020',Null,'Open',101
    UNION ALL
    SELECT 2,'Checkings', '13/01/2020',Null,'Open',101
    UNION ALL
    SELECT 3,'Checkings', '15/01/2020',Null,'Open',102
    UNION ALL
    SELECT 4,'Savings', '15/01/2020','19/01/2020','Closed',102
    UNION ALL
    SELECT 5,'Checkings', '02/02/2020',Null,'Open',103
    UNION ALL
    SELECT 6,'Checkings', '22/01/2020',Null,'Open',104
    UNION ALL
    SELECT 7,'Business', '21/01/2020',Null,'Open',105
    UNION ALL
    SELECT 8,'Checkings', '02/02/2020','05/08/2020,'Closed',106
    UNION ALL
    SELECT 9,'Checkings', '08/09/2020',Null,'Open',106
    UNION ALL
    SELECT 1,'Savings', '06/11/2020',Null,'Open',107

     Insert Into accounts
        SELECT 1,'Checkings', '01/01/2020','12/01/2020','Closed',101
        UNION ALL
        SELECT 1,'Checkings', '13/01/2020','15/01/2020','Closed',101
        UNION ALL
        SELECT 1,'Checkings', '16/01/2020',Null,'Open',101
        UNION ALL
        SELECT 2,'Checkings', '13/01/2020',Null,'Open',101
        UNION ALL
        SELECT 3,'Checkings', '15/01/2020',Null,'Open',102
        UNION ALL
        SELECT 4,'Savings', '15/01/2020','19/01/2020','Closed',102
        UNION ALL
        SELECT 5,'Checkings', '02/02/2020',Null,'Open',103
        UNION ALL
        SELECT 6,'Checkings', '22/01/2020',Null,'Open',104
        UNION ALL
        SELECT 7,'Business', '21/01/2020',Null,'Open',105
        UNION ALL
        SELECT 8,'Checkings', '02/02/2020','05/08/2020,'Closed',106
        UNION ALL
        SELECT 9,'Checkings', '08/09/2020',Null,'Open',106
        UNION ALL
        SELECT 1,'Savings', '06/11/2020',Null,'Open',107
    
    0 comments No comments

  2. Jim Young 6 Reputation points
    2021-01-30T16:34:30.337+00:00

    Shouldn't your transaction fact table be individual transactions and not an aggregate by date (if I'm understanding transcount properly)?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.