September 13, 2017 at 5:23 am
Talvin Singh - Wednesday, September 13, 2017 2:32 AMThis works very well!
i was able to test it on a large dataset, works blindingly fast!
really appreciate this
Thank you for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 13, 2017 at 8:37 am
Please mark Jeff's answer as correct, if you wouldn't mind.
September 13, 2017 at 4:48 pm
Steve Jones - SSC Editor - Wednesday, September 13, 2017 8:37 AMPlease mark Jeff's answer as correct, if you wouldn't mind.
Thank you both.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 12, 2018 at 9:52 pm
with data
as (SELECT *
FROM (VALUES (101,10,10)
,(102,20,30)
,(103,30,30)
,(104,12,12)
,(105,18,30)
,(106,10,10)
,(107,10,20)) v(cat,value,running_total)
)
,cte(cat1,value1,running_total1,grp1,grpid1)
as (select cat,value,running_total
,value as grp
,cast(1 as int) as grpid
from data
where cat=101
UNION ALL
select a.cat,a.value,a.running_total
,case when a.value+b.grp1<=30 then
a.value+b.grp1
else a.value
end as grp
,case when a.value+b.grp1<=30 then
b.grpid1
else b.grpid1+1
end as grpid
from data a
join cte b
on a.cat=b.cat1+1
)
select *
from cte
February 28, 2022 at 2:07 am
Please mark Jeff's answer as correct, if you wouldn't mind.
BWAAA-HAAA! Well that didn't work. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply