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


running total max value by group


running total max value by group

Author
Message
Talvin Singh
Talvin Singh
Right there with Babe
Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)

Group: General Forum Members
Points: 762 Visits: 254
Hi all,

i want a running total but with 2 clauses for the counter, the counter restarts when the total is 10, and it restarts to 1 when a new group starts:

ID Value Ref
1 4 1
1 4 1
1 8 2
1 10 3
2 5 1
2 5 1
3 1 1
3 5 1
3 5 2
3 6 3


as you can see above the Ref column increments when goes over 10, but also resets when there is a new ID.

the Value column will be in ascending order for each group.


SELECT *
INTO #TestTable
FROM (VALUES
(1, 4)
,(1,4)
,(1, 8)
,(1,10)
,(2,5)
,(2,5)
,(3,1)
,(3,5)
,(3,5)
,(3,6)
)v([ID],[Value]);

Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149594 Visits: 22104
Why is this different from your previous post?
https://www.sqlservercentral.com/Forums/1922239/Increament-id-if-running-total-for-group-exceeds-value


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Talvin Singh
Talvin Singh
Right there with Babe
Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)Right there with Babe (762 reputation)

Group: General Forum Members
Points: 762 Visits: 254
Luis Cazares - Thursday, February 15, 2018 1:52 PM

Similar but I need the basket ID to restart. I have played around with that code but not getting it to work

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)SSC Guru (799K reputation)

Group: General Forum Members
Points: 799537 Visits: 45960
Talvin Singh - Thursday, February 15, 2018 2:00 PM
Luis Cazares - Thursday, February 15, 2018 1:52 PM

Similar but I need the basket ID to restart. I have played around with that code but not getting it to work


There is nothing in your table to guarantee the processing order within an ID. Without such a thing, this is doomed to failure.

If the goal is to process in order by both the ascending order of both the ID and the Value, then such a thing can be done quite easily but if you're trying for something like load balancing, then that won't work.

What is this for? Load Balancing? Packing Boxes/Bins? Assigning tasks by duration? What?

--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
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