Skip to content
Advertisement

SQL STUFF FOR XML with specific grouping

I’m working in SQL Server 2014, and I have the following simple data, which tracks calling history of users:

I need to group the ACTIVITY values into one field, which effortlessly be done using the STUFF FOR XML function:

However I need to conditionally group these, on the condition that there was more than a 90 day between the activity date. Desired result:

Here’s the code/sample values I’m using, thanks!

Advertisement

Answer

Below query will able to generate the desired result. Please not I have addeed one more row in your input data to validate results

Please refer excellent article from Itzik Ben-Gan is on very similar problem for more details https://sqlperformance.com/2018/09/t-sql-queries/special-islands

Best!!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement