probably sql makes me dizzy when complexity level increases. It is easier to put a for loop and work in c#.
I have a query like
select.field1,.field2, field3,field4
from table1
Suppose this returns rows 1, 2, 3, 4, 5, 6.
I want to return summarized one row if this result has same field2 and field3. if ANY of the rows is different then return all the rows.
Thanks in advance.
Here is Sample data. In this lis row number 1 and row 4 are parent items and others child items.
When Summarizing, row 1 is summarized with all the child items but row number 4 is not summarized with children rows since row number 6 is has a different value field 2.
Field1 Field2 Field3 Field4(parentid)
1 paper cash null
2 Paper cash 1
3 paper cash 1
4 paper cash null
5 paper cash 4
6 pen cash 4
Here I want to return
field1 Field2 Field3 field4(all the child's id)
1 paper cash (2,3)
4 paper cash null
5 paper cash null
6 pen cash null
Hope this is better.
With SQL Server you’ll have to create a function to assist with the concatenation and a #temp table to assist with post-processing (to avoid repeated calls to the function across the whole source table). If you move to a database platform released in the last 10 years, you’ll have much more efficient solutions at your fingertips. 🙂
Setup:
Function:
The query:
Results:
Cleanup: