Home Forums SQL Server 2008 T-SQL (SS2K8) Restart runningtotal when predetermined value is reached RE: Restart runningtotal when predetermined value is reached

  • matak (9/26/2012)


    I will try and explain a little better. Im trying to mark each row based on having a something_runtot up to or equal to 25 - it cant go over this number and i am required to use the marked rows elsewhere (this is where runningCount comes in).

    Tables are NOT magnetic tapes than can be run forward and backwards; what dose that mean?

    My terminology was a little off in places 🙂

    It means I need something_runtot up to or equal to 50 and the current business logic requires half from ascending and half from descending. So i need to get a running total up to or equal to 25, order by orderCol ascending then mark the records and i need to do the same for order by orderCol descending.

    So in my current code the first time through the loop it will give the value of 1 to all rows with a runningTotal up to or equal to 25 ordered by orderCol asc and a value of 1 to all rows with a runningTotal up to or equal to 25 ordered by orderCol desc. This needs to be repeated until no more rows are left and no rows are allowed to be used more than once.

    Do you have a true sequence number for the something_values you want to total?

    Yes i do but when i insert the values i use the identity column to hold the ordering. (Im also not too sure why IDENTITY shouldnt be used).

    Can you keep changing it?

    I can change it as much as necessary to get better performance, however id prefer not to change from a float currently as thats what the base table is using.

    I will give your code a go and see what happens - im in a totally different timezone so it will be a while before i can test it.

    As always if my explanations dont make sense please let me know and i will try to rewrite it.

    Thanks for the help.

    Be aware that the SUM.. OVER(ORDER BY ...) sub-clause requires SQL Server 2012

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537