Author |
Topic |
beemd
Starting Member
14 Posts |
Posted - 2013-12-02 : 04:33:57
|
Hi,I've got a simple table with two fieldsdt(datetime)tphone(varchar)Each time a record is inserted the dt column is set to the current date/time.I'm trying to write a query to display a list of dates in one column and the record count up to and including that date in the next column.Any help appreciated.Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-02 : 04:40:17
|
depending on your condition you need one of the counts belowSELECT DATEADD(dd,DATEDIFF(dd,0,dt),0) AS DateVal,COUNT(*) AS CntTotal,COUNT(tphone) AS CntNonNullPhoneFROM TableGROUP BY DATEADD(dd,DATEDIFF(dd,0,dt),0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
beemd
Starting Member
14 Posts |
Posted - 2013-12-02 : 04:45:23
|
Hi,Thanks for that, this seems to count the total for each day, I need the cumulative total, i.e. the count should increase over time.ThanksBen |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-02 : 04:53:10
|
quote: Originally posted by beemd Hi,Thanks for that, this seems to count the total for each day, I need the cumulative total, i.e. the count should increase over time.ThanksBen
Sorry that was not quite clearly from your original problem statementAnyways here you goSELECT DateVal,CummCntFROM (SELECT DISTINCT DATEADD(dd,DATEDIFF(dd,0,dt),0) AS dateVal FROM Table)tCROSS APPLY (SELECT COUNT(1) AS CummCnt FROM Table WHERE DateVal < t.DateVal + 1 )t1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
beemd
Starting Member
14 Posts |
Posted - 2013-12-02 : 05:00:02
|
Thanks for your help.But, that gives the final total for every date.Ben |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-02 : 07:29:36
|
quote: Originally posted by beemd Thanks for your help.But, that gives the final total for every date.Ben
then whats it that you're expecting?Atleast show us some sample data and output you desire out of it.thats much easier for getting quick and accurate solution to your issue.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-02 : 07:31:33
|
one more attemptSELECT dt,CummCntFROM Table tCROSS APPLY (SELECT COUNT(1) AS CummCnt FROM Table WHERE dt <= t.dt )t1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
beemd
Starting Member
14 Posts |
Posted - 2013-12-02 : 08:01:15
|
A sample would bedt tphone01/01/1900 dfd01/01/1900 dfd01/01/1900 dfd02/01/1900 dfd02/01/1900 dfd03/01/1900 dfd04/01/1900 dfd04/01/1900 dfd04/01/1900 dfd04/01/1900 dfdAnd the result from that should bedt count01/01/1900 302/01/1900 503/01/1900 604/01/1900 10Hope that makes sense :)Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-02 : 08:08:40
|
[code]declare @t table(dt datetime,tphone varchar(10))insert @tvalues('01/01/1900', 'dfd'),('01/01/1900', 'dfd'),('01/01/1900', 'dfd'),('02/01/1900', 'dfd'),('02/01/1900', 'dfd'),('03/01/1900', ' dfd'),('04/01/1900', ' dfd'),('04/01/1900', ' dfd'),('04/01/1900', ' dfd'),('04/01/1900', ' dfd')SELECT DateVal,CummCntFROM (SELECT DISTINCT DATEADD(dd,DATEDIFF(dd,0,dt),0) AS dateVal FROM @t)tCROSS APPLY (SELECT COUNT(1) AS CummCnt FROM @t WHERE dt <= t.DateVal )t1output--------------------------------DateVal CummCnt---------------------------------1900-01-01 00:00:00.000 31900-02-01 00:00:00.000 51900-03-01 00:00:00.000 61900-04-01 00:00:00.000 10[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
beemd
Starting Member
14 Posts |
Posted - 2013-12-02 : 08:12:52
|
Now it's perfect. Thanks! :) |
|
|
|