根据行分组汇总或不汇总结果集
当复杂程度增加时,sql 可能会让我头晕。在 C# 中放置一个 for 循环并工作会更容易。
的查询。
select.field1,.field2, field3,field4
from table1
我有一个类似于假设返回行 1, 2, 3, 4, 5, 6
如果此结果具有相同的 field2 和 field3,我想返回汇总的一行。如果任何行不同,则返回所有行。
提前致谢。
这是示例数据。在此列表中,第 1 行和第 4 行是父项,其他是子项。 汇总时,第 1 行与所有子项一起汇总,但第 4 行未与子项一起汇总,因为第 6 行具有不同的值字段 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
这里我想返回
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
希望这更好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 SQL Server,您必须创建一个函数来协助连接,并创建一个 #temp 表来协助后处理(以避免在整个源表中重复调用该函数)。如果您迁移到过去 10 年发布的数据库平台,您将拥有更高效的解决方案。 :-)
设置:
功能:
查询:
结果:
清理:
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: