Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Help with Count over days (but not per day)...
Help with Count over days (but not per day) ... the total up until that day through the date range.
Rate Topic
Display Mode
Topic Options
Author
Message
Kevlarmpowered
Kevlarmpowered
Posted Sunday, September 30, 2012 8:06 AM
Forum Newbie
Group: General Forum Members
Last Login: Today @ 3:32 PM
Points: 7,
Visits: 38
I am having an issue trying to wrap my head around this.
I am trying to get the count of something over the days in a date range, but I don't need a per day count, I need the count of items for each day through the date range... does that make sense?
For example... the output should look something like this.
Day1 - 12
Day2 - 18
Day3 - 24
Day4 - 32
Day5 - 80
So, on Day1, there were 12 items. On Day2 there were 6 additional, so 12+6. On Day3 there were 6, so 12+6+6, Day4 there were 8, so 12+6+6+8, Day5 there were 48, so 12+6+6+8+48.
I am sure there is a way, but I can't wrap my head around it.
Post #1366254
dwain.c
dwain.c
Posted Sunday, September 30, 2012 11:51 PM
SSCrazy
Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 2,340,
Visits: 3,167
This article by Jeff Moden gives you arguably the best way to approach the running totals problem:
http://www.sqlservercentral.com/articles/T-SQL/68467/
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1366323
Sony Francis @EY
Sony Francis @EY
Posted Monday, October 01, 2012 2:48 AM
Valued Member
Group: General Forum Members
Last Login: Friday, May 17, 2013 6:07 AM
Points: 62,
Visits: 227
May be this query will help your problem
CREATE TABLE #Temp (Name varchar(100), value int)
INSERT INTO #Temp
SELECT 'Day1', 12
UNION
SELECT 'Day2' ,6
UNION
SELECT 'Day3' , 10
UNION
SELECT 'Day4' ,8
UNION
SELECT 'Day5' ,4
;with cte as
(
select *, ROW_NUMBER() over(PARTITION by null order by Name ) as Id
FROM #Temp
)
SELECT b.Name , SUM(A.value ), B.Id
FROM cte A
INNER JOIN cte B on A.Id <= B.Id
GROUP BY B.Name , b.id
Post #1366396
dwain.c
dwain.c
Posted Monday, October 01, 2012 2:52 AM
SSCrazy
Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 2,340,
Visits: 3,167
Sony Francis @EY (10/1/2012)
May be this query will help your problem
CREATE TABLE #Temp (Name varchar(100), value int)
INSERT INTO #Temp
SELECT 'Day1', 12
UNION
SELECT 'Day2' ,6
UNION
SELECT 'Day3' , 10
UNION
SELECT 'Day4' ,8
UNION
SELECT 'Day5' ,4
;with cte as
(
select *, ROW_NUMBER() over(PARTITION by null order by Name ) as Id
FROM #Temp
)
SELECT b.Name , SUM(A.value ), B.Id
FROM cte A
INNER JOIN cte B on A.Id <= B.Id
GROUP BY B.Name , b.id
I believe Jeff talks about this "triangular join" in the article I referenced.
No loops! No CURSORs! No RBAR! Hoo-uh!
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1366401
diamondgm
diamondgm
Posted Monday, October 01, 2012 6:37 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Today @ 1:32 AM
Points: 136,
Visits: 770
I believe Jeff talks about this "triangular join" in the article I referenced.
I don't think that its a triangular join - no subquery really.
It looks like a moficifation of the CROSS JOIN method of running total computation.
Instead of a filtered CROSS JOIN, the INNER JOIN condition is expanded.
This is something that Jeff did not cover in his article as far as I can remember.
This is my understanding; I'm open to correction.
Post #1366527
Jeff Moden
Jeff Moden
Posted Monday, October 01, 2012 6:54 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 1:51 PM
Points: 32,906,
Visits: 26,789
diamondgm (10/1/2012)
I believe Jeff talks about this "triangular join" in the article I referenced.
I don't think that its a triangular join - no subquery really.
It looks like a moficifation of the CROSS JOIN method of running total computation.
Instead of a filtered CROSS JOIN, the INNER JOIN condition is expanded.
This is something that Jeff did not cover in his article as far as I can remember.
This is my understanding; I'm open to correction.
You're correct. It's not a "triagular join". It's worse. It's a full accidental cross join. Look at the execution plan. You have 5 rows of data but one of the arrows coming off the table has 25 rows. If you add one more row to the data, that arrow jumps to 36, as expected with a cross join.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1366538
Kevlarmpowered
Kevlarmpowered
Posted Monday, October 01, 2012 9:15 AM
Forum Newbie
Group: General Forum Members
Last Login: Today @ 3:32 PM
Points: 7,
Visits: 38
Great thanks... now that I understand what I am looking for (the running total part), I see that there is a bunch of information out there.
Thanks again.
Post #1366616
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.