score:3
okay - contrary to what i thought, you can do this with a single recursive cte.
not that it's pretty...
please note that all testing was done on db2, which does not have a way to return arrays in-row. i don't know if sql server can (so you may have to parse results). i do not recommend attempting to do this with linq - if anything, this should be run on the database directly.
with mappings(beginid, storedid, groupid, storedpath, grouppath) as (
select a.storedmessageid, a.storedmessageid, a.messagegroupid,
a.storedmessageid + "|", a.messagegroupid + "|"
from messagegrouping as a
where not exists (select 1
from messagegrouping as b
where b.storedmessageid < a.storedmessageid
and b.messagegroupid = a.messagegroupid)
and not exists (select 1
from messagegrouping as b
where b.messagegroupid < a.messagegroupid
and b.storedmessageid = a.storedmessageid)
union all
select a.beginid, a.storedid, b.messagegroupid,
a.storedpath, a.grouppath + b.messagegroupid + "|"
from mappings as a
join messagegrouping as b
on b.storedmessageid = a.storedid
and b.messagegroupid <> a.groupid
and a.grouppath not like "%" + b.messagegroupid + "%"
union all
select a.beginid, a.storedmessageid, a.groupid,
a.storedpath + b.storedmessageid + "|", a.grouppath
from mappings as a
join messagegrouping as b
on b.messagegroupid = a.groupid
and b.storedmessageid <> a.storedid
and a.storedpath not like "%" + b.storedmessageid + "%"),
result_rows (ids, num) as (
select storedpath,
row_number() over(partition by beginid
order by length(storedpath) desc)
from mappings)
select ids
from result_rows
where num = 1
yields ids
of:
1|2|6|
3|7|
4|
Source: stackoverflow.com
Related Query
- Group By that chains back and forth across two fields
- How do I group by two fields and return the original objects that match?
- How to retrieve an audit trail across two SQL Server tables and put them back in the right order?
- Possible to add two columns together and group by that column using linq on IQueryable?
- Linq to group by two fields and average
- Join and Group Two Tables and Get Sum of Fields - Linq C#
- C# LINQ NET 3.5 SP1: Using LINQ to group by two fields and assigning a correlative unique ID (integer number) to all member of the group
- Select multiple fields group by and sum
- Access all of the data after joining two tables and group them using linq
- Linq group by multiple fields across different tables
- LINQ Union between two tables with the same fields and then returned in a collection
- How do I group on one of two possible fields using LINQ?
- Compare id's of objects in two lists and get the list of objects that includes objects with ids occurring in both of them
- Is there a way to speed up this code that finds data changes in two XML files?
- LINQ to Entities, join two tables, then group and take sums of columns from both tables
- Grouping and Sum Datatable by two fields with different Where conditions
- How to group by multiple fields and get the count of another field
- Not able to access fields with group by and multiple joins with linq
- Linq query that does left join and group by having count()
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- Need a LINQ code example to link two tables that have no foreign key
- How to join two tables using group by and do the calculation?
- Joining data that exists in two tables and aggregating non joined data
- how can i get the right fields into a group and sum using linq?
- combine two list and linq query to order by two different fields from different lists
- Linq group objects by unique values and make two lists
- LINQ: Compare two list based on their properties and return items that match certain conditions
- Group and Select First From Two Tables Linq
- Linq query for two tables using SUM and Group By
- Group by two columns and do a ToDictionary with a Tuple as Key C# Linq
More Query from same tag
- VB.NET Group by two columns and write results to an array
- Converting System.Drawing.Image to System.Data.Linq.Binary
- LINQ select items from a list within a list
- Linq question about grouping something that can change?
- Order By on specific column
- Efficient implementation of a "ThenBy" sort
- How to join three tables in linq with child table sum
- Linq query on the same table with difference between rows
- Filter data with LINQ from List or DataSet in C#
- Auto mapper to map conditional entity which doesn't work without using After map
- Complex Object mapping using Automapper
- Generic hierarchy filter of nested classes in Linq
- orderby with LINQ
- Bind nested XML to DataGridView
- LINQ Timestamp - Automatically updating a timestamp column?
- LINQ grouping to more than one group
- Unable to read specific fields from XML string in C#?
- How can I bind DataGrid Column to a property within an anonymous type as part of LINQ GroupBy?
- MVC5 Linq to ViewModel to Razor View code improvement?
- EF4 Linq - Get top items using criteria from related tables
- C# lambda extract a single row string value
- Convert SQL Server query to LINQ lambda query for two different cols from two different table that are related
- LINQ and DataTable query
- How can i compare date without time part in linq?
- LINQ DynamicLibrary: How to extract count and list from IQueryable
- Searching names by each word in search string
- Advanced multiple join in subquery using LINQ
- Error when get data from xml file
- Linq include children from collection
- What is the Java 8 Stream API equivalent for LINQ Join?