根据行分组汇总或不汇总结果集

发布于 2024-12-04 17:48:25 字数 782 浏览 0 评论 0原文

当复杂程度增加时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2024-12-11 17:48:25

使用 SQL Server,您必须创建一个函数来协助连接,并创建一个 #temp 表来协助后处理(以避免在整个源表中重复调用该函数)。如果您迁移到过去 10 年发布的数据库平台,您将拥有更高效的解决方案。 :-)

设置:

USE tempdb;
GO

CREATE TABLE dbo.[Sample]
(
    Field1 INT, 
    Field2 VARCHAR(32),
    Field3 VARCHAR(32), 
    Field4 INT
);

INSERT dbo.[Sample] SELECT 1,'paper','cash', NULL
UNION ALL   SELECT 2,'Paper','cash', 1
UNION ALL   SELECT 3,'paper','cash', 1
UNION ALL   SELECT 4,'paper','cash', NULL
UNION ALL   SELECT 5,'paper','cash', 4
UNION ALL   SELECT 6,'pen',  'cash', 4;
GO

功能:

CREATE FUNCTION dbo.ConcatIDs
(
  @Field1 INT
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @s VARCHAR(8000);
    SELECT @s = COALESCE(@s + ',', '')
        + CONVERT(VARCHAR(12), Field1)
      FROM dbo.[Sample] AS s
      WHERE Field4 = @Field1 
      AND NOT EXISTS
      (
        SELECT 1
          FROM dbo.[Sample]
          WHERE Field4 = s.Field4
          AND Field1 <> s.Field1
          AND (Field2 <> s.Field2 OR Field3 <> s.Field3)
      );
    RETURN @s;
END
GO

查询:

SELECT Field1, Field2, Field3, Field4, f4 = dbo.ConcatIDs(Field1) 
  INTO #x
  FROM dbo.[Sample];

SELECT Field1, Field2, Field3, 
  [field4(all the child's id)] = '(' + f4 + ')'
FROM #x AS x1
WHERE NOT EXISTS 
(
    SELECT 1 FROM #x AS x2
    WHERE x2.Field1 = x1.Field4
    AND x2.f4 IS NOT NULL
);

DROP TABLE #x;

结果:

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

清理:

DROP TABLE dbo.[Sample];
DROP FUNCTION dbo.ConcatIDs;

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:

USE tempdb;
GO

CREATE TABLE dbo.[Sample]
(
    Field1 INT, 
    Field2 VARCHAR(32),
    Field3 VARCHAR(32), 
    Field4 INT
);

INSERT dbo.[Sample] SELECT 1,'paper','cash', NULL
UNION ALL   SELECT 2,'Paper','cash', 1
UNION ALL   SELECT 3,'paper','cash', 1
UNION ALL   SELECT 4,'paper','cash', NULL
UNION ALL   SELECT 5,'paper','cash', 4
UNION ALL   SELECT 6,'pen',  'cash', 4;
GO

Function:

CREATE FUNCTION dbo.ConcatIDs
(
  @Field1 INT
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @s VARCHAR(8000);
    SELECT @s = COALESCE(@s + ',', '')
        + CONVERT(VARCHAR(12), Field1)
      FROM dbo.[Sample] AS s
      WHERE Field4 = @Field1 
      AND NOT EXISTS
      (
        SELECT 1
          FROM dbo.[Sample]
          WHERE Field4 = s.Field4
          AND Field1 <> s.Field1
          AND (Field2 <> s.Field2 OR Field3 <> s.Field3)
      );
    RETURN @s;
END
GO

The query:

SELECT Field1, Field2, Field3, Field4, f4 = dbo.ConcatIDs(Field1) 
  INTO #x
  FROM dbo.[Sample];

SELECT Field1, Field2, Field3, 
  [field4(all the child's id)] = '(' + f4 + ')'
FROM #x AS x1
WHERE NOT EXISTS 
(
    SELECT 1 FROM #x AS x2
    WHERE x2.Field1 = x1.Field4
    AND x2.f4 IS NOT NULL
);

DROP TABLE #x;

Results:

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

Cleanup:

DROP TABLE dbo.[Sample];
DROP FUNCTION dbo.ConcatIDs;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文