I'm using SQL Server (2012), and using the two (simplified) tables below, how do I create 3 separate reports (daily, weekly and monthly) and include the following calculated fields:
1. new users created in this period
2. total number of users at this time
**Users**
userID int
name varchar(80)
userCreated datetime
**Orders**
orderID int
userID int
orderCreated datetime
I've been messing around with this code:
SELECT CAST(DATEPART(dd,userCreated) as VARCHAR(2)) + '/' + CAST(DATEPART(mm,userCreated) AS VARCHAR(2)) + '/' + CAST(DATEPART(yyyy,userCreated) AS VARCHAR(4)) [Date],
count(*) newSignUps,
(select count(*) from users u2 WHERE u2.userCreated < u1.userCreated)
FROM users u1
WHERE userCreated BETWEEN '05/01/2014 00:00:00.000' and '05/31/2014 23:59:59.000'
GROUP BY DATEPART(dd,userCreated), DATEPART(mm,userCreated), DATEPART(yyyy,userCreated),userCreated
But to show anything, it needs the "userCreated" field added to the grouping...
For the reports I need to show:
Daily:
date new sign ups users in system
17/03/2013 10 100
18/03/2013 4 104
19/03/2013 8 112
Weekly:
week
13 8 40
14 2 42
15 5 47
Monthly:
Jan 3 54
Feb 9 63
Mar 2 65
I hope this makes sense? Thank you...
I'm not sure if I understood your question correctly, but this gives you all the users created per day:
SELECT year(userCreated), month(userCreated), day(userCreated), count(*)
FROM Users
GROUP BY year(userCreated), month(userCreated), day(userCreated)
this one by month:
SELECT year(userCreated), month(userCreated), count(*)
FROM Users
GROUP BY year(userCreated), month(userCreated)
and this one by week:
SELECT year(userCreated), datepart(week, userCreated), count(*)
FROM Users
GROUP BY year(userCreated), datepart(week, userCreated)
Edit according to you the missing total field I give you here the example for the month query:
SELECT year(userCreated), month(userCreated), count(*) AS NewCount,
(SELECT COUNT(*) FROM Users u2 WHERE
CAST(CAST(year(u1.userCreated) AS VARCHAR(4)) + RIGHT('0' + CAST(month(u1.userCreated) AS VARCHAR(2)), 2) + '01' AS DATETIME) > u2.userCreated) AS TotalCount
FROM Users u1
GROUP BY year(userCreated), month(userCreated)
Hope this helps for the other two queries.
DAILY
select count(UserId),userCreated
from User WHERE CONDITION group by CreatedOn
MONTHLY
select count(UserId),
LEFT(CONVERT(varchar, userCreated ,112),6) from User
WHERE CONDITION group by LEFT(CONVERT(varchar, userCreated ,112),6)
OR
select count(UserId),
month(userCreated )
from User group by month(userCreated )
WEEKLY
select count(UserId),
DATEPART( wk, userCreated)
from User WHERE CONDITION group by DATEPART( wk, userCreated )
FOR BOTH NEW and EXISTING USER COUNT (i did for monthly)
select new,
(select count(UserId) from User where month(userCreated)<=monthwise) as total,
monthwise
FROM (
select count(UserId) as new,
month(userCreated)as monthwise from User group by month(userCreated)
)tmp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With