SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with Count over days (but not per day) ... the total up until that day through the date range.


Help with Count over days (but not per day) ... the total up until that day through the date range.

Author
Message
Kevlarmpowered
Kevlarmpowered
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 138
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.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7557 Visits: 6431
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/


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sony Francis @EY
Sony Francis @EY
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 293
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
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7557 Visits: 6431
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.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
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?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
diamondgm
diamondgm
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 941
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.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89339 Visits: 41143
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 Row-By-Agonizing-Row.
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Kevlarmpowered
Kevlarmpowered
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 138
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search