SQL, Date, Daily Row Count and Total Accumulated Row Count

I need a Stored Procedure to return 'Date, Daily Row Count and Total Accumulated Row Count'?
So I thought this must be a simple common functionality and I could just find something on Google to modify???

I have a table with new signups daily, capturing the CID and CreateTime.  Of course the boss wants to know how many each day and how many in total.  I have not been able to get "Group By Date" to work, I think, however I Cast, Convert, Trim, or Format SQL sees each smallDateTime as a "new different Group" due to the time portion of smallDateTime, I think I got smallDateTime Converted to just a Date, but then I can't use the alias in the Group By clause.  Hmmm?

Table-
CID I smallDateTime
1   I 2015-04-28 06:33:00
2   I 2015-04-28 06:34:00
3   I 2015-04-28 06:36:00
4   I 2015-04-29 06:00:00
5   I 2015-04-29 06:02:00
6   I 2015-04-30 05:00:00

Result(desired)
Date        I Count I Total
2015-04-28  I     3 I  3
2015-04-29  I     2 I  5
2015-04-30  I     1 I  6      

Thanks for the assistance.
Sam
SamCashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UnifiedISCommented:
You have to use the same cast/convert syntax in the group by as in the select clause:
SELECT CAST(value as datetime) AS columname,
COUNT(CID) AS RecordCount
...
GROUP BY CAST(value as datetime)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you need something like this:

SELECT CAST(smallDateTimeas date) AS columname,
COUNT(CID) AS RecordCount
, (SELECT COUNT(*) FROM yourtable x
    WHERE x.smallDateTime <= CAST(t.smallDateTimeas date) + 1
 ) running_count
FROM yourtable t
GROUP BY CAST(smallDateTimeas date)
ORDER BY CAST(smallDateTimeas date)
0
SamCashAuthor Commented:
Thank you very much!  

I hope it is OK to share the final solution, as it may help other newbees like me?

SELECT CONVERT(date, CreateDateTime) as myDate,
            COUNT(ContactID) as myCount
            , (SELECT COUNT(*)FROM Contacts x
                  WHERE x.CreateDateTime <= DATEADD(day, 1, CONVERT(date, t.CreateDateTime))
                  ) mySum
FROM Contacts t
GROUP BY Convert(date, CreateDateTime)
ORDER by Convert(date, CreateDateTime)
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Mark ElySenior Coldfusion DeveloperCommented:
SELECT CAST(N.SDT AS DATE) AS D, COUNT(*) AS DaysInGroup, (SELECT COUNT(*) AS RunningTotal FROM dbo.NameOfYourTable AS N2 WHERE CAST(N2.SDT AS DATE) <= CAST(N.SDT AS DATE)) AS RunningTotal
FROM dbo.NameOfYourTable AS N		
GROUP BY CAST(SDT AS DATE)

Open in new window

0
SamCashAuthor Commented:
Thanks again.
Sam
0
Mike EghtebasDatabase and Application DeveloperCommented:
This is for future readers with temp table:
Select Convert(Date, smallDateTime) As Dates 
   , Count(*) As 'Count'
   , (Select Count(*) From #t tt
     Where Convert(Date, tt.smallDateTime)<=Convert(Date, t.smallDateTime)
) AS total 
From #t t
Group By Convert(Date, smallDateTime)
Order By Convert(Date, smallDateTime)

CREATE TABLE #t
(
    CID   int         
    ,smallDateTime   datetime
);

Insert #t(CID , smallDateTime) values
(1 ,   '2015-04-28 06:33:00')
,(2 ,   '2015-04-28 06:34:00')
,(3 ,   '2015-04-28 06:36:00')
,(4 ,   '2015-04-29 06:00:00')
,(5 ,   '2015-04-29 06:02:00')
,(6 ,   '2015-04-30 05:00:00')

Open in new window

0
PortletPaulEE Topic AdvisorCommented:
for SQL Server versions that support SUM() OVER() you can achieve the running sum without the correlated subquery:

select
       myDate
     , myCount
     , SUM(MyCount) OVER(order by MyDate) as mySum
from (
      select
             cast(CreateDateTime as date) as myDate
           , count(*) as myCount
      from Contacts
      group by
             cast(CreateDateTime as date)  
      ) g
;

e.g. http://sqlfiddle.com/#!6/f30a9/5
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.