## Increament id if running total for group exceeds value

 Author Message Talvin Singh SSChasing Mays Group: General Forum Members Points: 612 Visits: 254 hi all,as you can see below, i want to add an incrementing basket id if running total exceeds 10 for that group.order 1 , 2 and 3 are for group id 3 but order 3 would exceed the running total of 10, therefore it goes into the next basket id.order 4 is for a new group therefore next basket id.order 5 is for group 7 by running total would exceed 10 therefore assigned next basket id.orders 6 and 7 are for same group but different from previous group therefore will get next basket id.and so on`SELECT * INTO #TestTable FROM (VALUES ('Order1',4,3) ,('Order2',3,3) ,('Order3',4,3) ,('Order4',5,7) ,('Order5',3,8) ,('Order6',4,8) ,('Order7',5,8) )v(OrderID,items,groupID);-- RESULTSOrderID, items, groupID, RunningTotalInGroup, BasketIDOrder1, 4, 3, 4, 1Order2, 3, 3, 7, 1Order3, 4, 3, 4, 2Order4, 5, 7, 5, 3Order5, 6, 7, 6, 4Order6, 4, 8, 4, 5Order7, 5, 8, 9, 5`i am looking at the following, but i am unable to add the >= 10 clause and increment to next basketID.`SUM(items) OVER(PARTITION BY groupID ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)` ZZartin SSCoach Group: General Forum Members Points: 18188 Visits: 15681 Well your sample data doesn't seem to match the data you described in your explanation order 5 is actually a new group and doesn't sum out to more than 10 until order 7. But based on your sample data this might work, although it's fairly messy and i'm sure someone can do better. Also what do you expect to happen if you jump more than 10 in between 2 orders, is that two baskets?`WITH TEMP_CTE AS(SELECT *, (SUM(items) OVER(PARTITION BY groupID ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) / 10) + 1 AS BASKET_IDFROM #TestTable), TEMP_CTE_TWO AS(SELECT groupID, MAX(BASKET_ID) AS MAX_BASKET FROM TEMP_CTEGROUP BY groupID)SELECT *, TEMP_CTE.BASKET_ID + PREV_BASKET_ID AS RUNNING_BUCKET FROM TEMP_CTECROSS APPLY(SELECT groupID AS groupID_SUM, SUM(MAX_BASKET) OVER(ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) - MAX_BASKET AS PREV_BASKET_ID FROM TEMP_CTE_TWO) PREVIOUS_BASKETWHERETEMP_CTE.groupID = PREVIOUS_BASKET.groupID_SUM` Luis Cazares SSC Guru Group: General Forum Members Points: 127470 Visits: 21917 I actually used the Quirky Update which is great for this sort of things. I would test it as I'm not sure about updating 2 columns at once. Check the full explanation (with tests and alternatives) in here: Solving the Running Total and Ordinal Rank Problems (Rewritten) - SQLServerCentral`SELECT IDENTITY(INT,1,1) AS RowID, v.OrderID, v.items, v.groupID, 0 AS RunningTotalInGroup, 0 AS BasketID INTO #TestTable FROM (VALUES ('Order1',4,3) ,('Order2',3,3) ,('Order3',4,3) ,('Order4',5,7) ,('Order5',6,8) ,('Order6',4,8) ,('Order7',5,8) )v(OrderID,items,groupID)ORDER BY v.groupID, v.OrderID;CREATE CLUSTERED INDEX ci_testTable ON #TestTable (groupID, OrderID)DECLARE @RunningTotal INT = 0, @GroupID INT = 0, @BasketID INT = 0;UPDATE t WITH(TABLOCKX)SET @RunningTotal = RunningTotalInGroup = items + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN @RunningTotal ELSE 0 END, @BasketID = BasketID = @BasketID + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END, @GroupID = groupIDFROM #TestTable AS tOPTION (MAXDOP 1);SELECT *FROM #TestTable AS ttGODROP TABLE #TestTable` 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 SSChasing Mays Group: General Forum Members Points: 612 Visits: 254 +xZZartin - Tuesday, February 6, 2018 10:38 AMWell your sample data doesn't seem to match the data you described explanation order 5 is actually a new group and doesn't sum out to more than 10 until order 7. But based on your sample data this might work, although it's fairly messy and i'm sure someone can do better. Also what do you expect to happen if you jump more than 10 in between 2 orders, is that two baskets?`WITH TEMP_CTE AS(SELECT *, (SUM(items) OVER(PARTITION BY groupID ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) / 10) + 1 AS BASKET_IDFROM #TestTable), TEMP_CTE_TWO AS(SELECT groupID, MAX(BASKET_ID) AS MAX_BASKET FROM TEMP_CTEGROUP BY groupID)SELECT *, TEMP_CTE.BASKET_ID + PREV_BASKET_ID AS RUNNING_BUCKET FROM TEMP_CTECROSS APPLY(SELECT groupID AS groupID_SUM, SUM(MAX_BASKET) OVER(ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) - MAX_BASKET AS PREV_BASKET_ID FROM TEMP_CTE_TWO) PREVIOUS_BASKETWHERETEMP_CTE.groupID = PREVIOUS_BASKET.groupID_SUM`I will give this a go when I have my laptop.Inhave pre conditioned the order, therefore if an order has >10 items it splits the order into two, or if greater than 30 it's three groups. I have assigned a letter code to that order for example order1_A order1_B and so on. So in that list you won't find an order greater than 10.Sorry about the data being incorrect .I am working with a large dataset and quickly typed that up.On another note. If a new wave of orders come through, is there a way to add the groupid and current number of items in groupid to a temp table so on the new wave of orders, it checks for the current number of items against if new wave groupid is same as groupid in temp table and brings back the value, so let's say groupid 3 currently has 8 items, the first order in new list happens to be for groupid 3, that order has 3 items therefore current total is now 8 and so on... Talvin Singh SSChasing Mays Group: General Forum Members Points: 612 Visits: 254 +xLuis Cazares - Tuesday, February 6, 2018 11:27 AMI actually used the Quirky Update which is great for this sort of things. I would test it as I'm not sure about updating 2 columns at once. Check the full explanation (with tests and alternatives) in here: Solving the Running Total and Ordinal Rank Problems (Rewritten) - SQLServerCentral`SELECT IDENTITY(INT,1,1) AS RowID, v.OrderID, v.items, v.groupID, 0 AS RunningTotalInGroup, 0 AS BasketID INTO #TestTable FROM (VALUES ('Order1',4,3) ,('Order2',3,3) ,('Order3',4,3) ,('Order4',5,7) ,('Order5',6,8) ,('Order6',4,8) ,('Order7',5,8) )v(OrderID,items,groupID)ORDER BY v.groupID, v.OrderID;CREATE CLUSTERED INDEX ci_testTable ON #TestTable (groupID, OrderID)DECLARE @RunningTotal INT = 0, @GroupID INT = 0, @BasketID INT = 0;UPDATE t WITH(TABLOCKX)SET @RunningTotal = RunningTotalInGroup = items + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN @RunningTotal ELSE 0 END, @BasketID = BasketID = @BasketID + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END, @GroupID = groupIDFROM #TestTable AS tOPTION (MAXDOP 1);SELECT *FROM #TestTable AS ttGODROP TABLE #TestTable`Not heard of quirky update. I will try this too. I have posted a reply to another reply above, that is the next step I would like to look into .will quirky update support that? Bert-701015 SSC Eights! Group: General Forum Members Points: 806 Visits: 1001 select OrderID, Items, GroupID, RunningTotalInGroup, SUM(IncrBasket) OVER(ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as BasketIDfrom ( select OrderID, Items, GroupID, case lag(groupid) over (order by groupid) when GroupID then case when SUM(items) OVER(PARTITION BY groupID ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) >= 10 then 1 else 0 end else 1 end as IncrBasket, SUM(items) OVER(PARTITION BY groupID ORDER BY groupID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as RunningTotalInGroup from #TestTable) Rslt Talvin Singh SSChasing Mays Group: General Forum Members Points: 612 Visits: 254 +xLuis Cazares - Tuesday, February 6, 2018 11:27 AMI actually used the Quirky Update which is great for this sort of things. I would test it as I'm not sure about updating 2 columns at once. Check the full explanation (with tests and alternatives) in here: Solving the Running Total and Ordinal Rank Problems (Rewritten) - SQLServerCentral`SELECT IDENTITY(INT,1,1) AS RowID, v.OrderID, v.items, v.groupID, 0 AS RunningTotalInGroup, 0 AS BasketID INTO #TestTable FROM (VALUES ('Order1',4,3) ,('Order2',3,3) ,('Order3',4,3) ,('Order4',5,7) ,('Order5',6,8) ,('Order6',4,8) ,('Order7',5,8) )v(OrderID,items,groupID)ORDER BY v.groupID, v.OrderID;CREATE CLUSTERED INDEX ci_testTable ON #TestTable (groupID, OrderID)DECLARE @RunningTotal INT = 0, @GroupID INT = 0, @BasketID INT = 0;UPDATE t WITH(TABLOCKX)SET @RunningTotal = RunningTotalInGroup = items + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN @RunningTotal ELSE 0 END, @BasketID = BasketID = @BasketID + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END, @GroupID = groupIDFROM #TestTable AS tOPTION (MAXDOP 1);SELECT *FROM #TestTable AS ttGODROP TABLE #TestTable`How would you restart the basketID after each group? Talvin Singh SSChasing Mays Group: General Forum Members Points: 612 Visits: 254 +xTalvin Singh - Friday, February 16, 2018 3:16 AM+xLuis Cazares - Tuesday, February 6, 2018 11:27 AMI actually used the Quirky Update which is great for this sort of things. I would test it as I'm not sure about updating 2 columns at once. Check the full explanation (with tests and alternatives) in here: Solving the Running Total and Ordinal Rank Problems (Rewritten) - SQLServerCentral`SELECT IDENTITY(INT,1,1) AS RowID, v.OrderID, v.items, v.groupID, 0 AS RunningTotalInGroup, 0 AS BasketID INTO #TestTable FROM (VALUES ('Order1',4,3) ,('Order2',3,3) ,('Order3',4,3) ,('Order4',5,7) ,('Order5',6,8) ,('Order6',4,8) ,('Order7',5,8) )v(OrderID,items,groupID)ORDER BY v.groupID, v.OrderID;CREATE CLUSTERED INDEX ci_testTable ON #TestTable (groupID, OrderID)DECLARE @RunningTotal INT = 0, @GroupID INT = 0, @BasketID INT = 0;UPDATE t WITH(TABLOCKX)SET @RunningTotal = RunningTotalInGroup = items + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN @RunningTotal ELSE 0 END, @BasketID = BasketID = @BasketID + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END, @GroupID = groupIDFROM #TestTable AS tOPTION (MAXDOP 1);SELECT *FROM #TestTable AS ttGODROP TABLE #TestTable`How would you restart the basketID after each group?I added an outer case statement which achieves the result!from:`@BasketID = BasketID = @BasketID + CASE WHEN items + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END,`to:`@BasketID = BasketID = CASE WHEN groupID = @GroupID THEN (@BasketID + CASE WHEN item + @RunningTotal < 10 AND groupID = @GroupID THEN 0 ELSE 1 END) ELSE 1 END,`Many thanks george_at_sql SSC-Addicted Group: General Forum Members Points: 445 Visits: 1165 You can make use of recursive cte to get the output. The expected results as mentioned by you is slightly incorrect.`with dataas (select row_number() over(order by orderid) as rnk ,* from #testtable ) ,cte(orderid1,items1,groupid1,prev_groupid1,runningtotal1,basketid1,rnk1) as (select a.orderid,a.items,a.groupid,a.groupid as prev_groupid,a.items as runningtotal,1 as basketid,a.rnk from data a where a.rnk=1 union all select a.orderid,a.items,a.groupid ,a.groupid as prev_groupid ,case when b.runningtotal1+a.items<=10 and a.groupid=b.prev_groupid1 then b.runningtotal1+a.items else a.items end as runningtotal ,case when b.runningtotal1+a.items<=10 and a.groupid=b.prev_groupid1 then b.basketid1 else b.basketid1+1 end as baskedid ,a.rnk from data a join cte b on a.rnk=b.rnk1+1 )select orderid1,items1,groupid1,basketid1 from cte`

## 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
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         Quest Software         ApexSQL         SQL Sentry      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss Data Warehousing Books          Discuss T-SQL Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12