SQL Server:合并同一行中的两个值

发布于 2024-11-03 09:21:03 字数 722 浏览 1 评论 0原文

我有以下联合所有结果,

ID  Name     Date         Team         Total#1     Total#2
1   Test_1   4/25/2011    Team_1         110          0
1   Test_1   4/25/2011    Team_2         20           0
1   Test_1   4/25/2011    Team_3         170          0
1   Test_1   4/25/2011    Team_1         0            151
1   Test_1   4/25/2011    Team_3         0            98

我希望将每个团队的结果合并起来。有一些重复的球队名称具有不同的总价值,并且有一些独特的球队名称只有一个总价值。

ID  Name     Date         Team         Total#1     Total#2
1   Test_1   4/25/2011    Team_1         110          151
1   Test_1   4/25/2011    Team_2         20           0
1   Test_1   4/25/2011    Team_3         170          98

有什么想法吗?

I have the following union all results

ID  Name     Date         Team         Total#1     Total#2
1   Test_1   4/25/2011    Team_1         110          0
1   Test_1   4/25/2011    Team_2         20           0
1   Test_1   4/25/2011    Team_3         170          0
1   Test_1   4/25/2011    Team_1         0            151
1   Test_1   4/25/2011    Team_3         0            98

I am looking to combine the results for each team. There are some duplicate team names with different totals and there are some unique teams names with only one total value.

ID  Name     Date         Team         Total#1     Total#2
1   Test_1   4/25/2011    Team_1         110          151
1   Test_1   4/25/2011    Team_2         20           0
1   Test_1   4/25/2011    Team_3         170          98

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

⒈起吃苦の倖褔 2024-11-10 09:21:03
SELECT Id, Name, Date, Team, SUM([Total#1]) [Total#1], SUM([Total#2]) [Total#2]
FROM YourUnionResult
GROUP BY Id, Name, Date, Team
SELECT Id, Name, Date, Team, SUM([Total#1]) [Total#1], SUM([Total#2]) [Total#2]
FROM YourUnionResult
GROUP BY Id, Name, Date, Team
╰つ倒转 2024-11-10 09:21:03

只需使用聚合函数和GROUP BY

SELECT ID, Name, Date, Team, SUM(Total#1) as Total1, Sum(Total#2) as Total2
FROM Mytable
WHERE ...
GROUP BY ID, Name, Date, Team

Just use aggregate functions and GROUP BY:

SELECT ID, Name, Date, Team, SUM(Total#1) as Total1, Sum(Total#2) as Total2
FROM Mytable
WHERE ...
GROUP BY ID, Name, Date, Team
美男兮 2024-11-10 09:21:03

这行不通?

select id,name,date,team,sum(Total#1) as Total#1,sum(Total#2) as Total#2
from Your table
group by id,name,date,team

doesn't this work?

select id,name,date,team,sum(Total#1) as Total#1,sum(Total#2) as Total#2
from Your table
group by id,name,date,team
紧拥背影 2024-11-10 09:21:03

select... union select... 设为子查询,“外部”查询针对它发出一个 group by 。

Make the select... union select... a subquery, and the "outer" query issues a group by against it.

萌面超妹 2024-11-10 09:21:03

假设由于某种原因标准分组不适合您可以这样做

SELECT t1.id, 
       t1.name, 
       t1.DATE, 
       t1.team, 
       t1.total_1, 
       t2.total_2 
FROM   (SELECT id, 
               name, 
               DATE, 
               team, 
               somecomplicatedcalculation total_1 
        FROM   your table) t1 
       LEFT JOIN (SELECT id, 
                         name, 
                         DATE, 
                         team, 
                         somecomplicatedcalculation total_2 
                  FROM   your table) t2 
         ON t1.id = t2.id
            and t1.team = t2.team

您也可以对 CTE 执行相同的操作

WITH t1 
     AS (SELECT id, 
                name, 
                DATE, 
                team, 
                somecomplicatedcalculation total_1 
         FROM   your table), 
     t2 
     AS (SELECT id, 
                name, 
                DATE, 
                team, 
                somecomplicatedcalculation total_2 
         FROM   your table) 
SELECT * 
FROM   t1 
       INNER JOIN t2 
         ON t1.id = t2.id 
            and t1.team = t2.team

或联合上的分组将起作用

SELECT id, 
       name, 
       DATE, 
       team, 
       SUM(total_1), 
       SUM(total_2) 
FROM   (SELECT id, 
               name, 
               DATE, 
               team, 
               somecomplicatedcalculation total_1, 
               0                          AS total_2 
        FROM   yourtable 
        UNION ALL 
        SELECT id, 
               name, 
               DATE, 
               team, 
               0                          AS total_1, 
               somecomplicatedcalculation total_2 
        FROM   yourtable) total 
GROUP BY id, 
       name, 
       DATE, 
       team

Assuming for some reason standard Grouping isn't working for you could do this

SELECT t1.id, 
       t1.name, 
       t1.DATE, 
       t1.team, 
       t1.total_1, 
       t2.total_2 
FROM   (SELECT id, 
               name, 
               DATE, 
               team, 
               somecomplicatedcalculation total_1 
        FROM   your table) t1 
       LEFT JOIN (SELECT id, 
                         name, 
                         DATE, 
                         team, 
                         somecomplicatedcalculation total_2 
                  FROM   your table) t2 
         ON t1.id = t2.id
            and t1.team = t2.team

You could also do the same with CTE's

WITH t1 
     AS (SELECT id, 
                name, 
                DATE, 
                team, 
                somecomplicatedcalculation total_1 
         FROM   your table), 
     t2 
     AS (SELECT id, 
                name, 
                DATE, 
                team, 
                somecomplicatedcalculation total_2 
         FROM   your table) 
SELECT * 
FROM   t1 
       INNER JOIN t2 
         ON t1.id = t2.id 
            and t1.team = t2.team

OR grouping on the union would work to

SELECT id, 
       name, 
       DATE, 
       team, 
       SUM(total_1), 
       SUM(total_2) 
FROM   (SELECT id, 
               name, 
               DATE, 
               team, 
               somecomplicatedcalculation total_1, 
               0                          AS total_2 
        FROM   yourtable 
        UNION ALL 
        SELECT id, 
               name, 
               DATE, 
               team, 
               0                          AS total_1, 
               somecomplicatedcalculation total_2 
        FROM   yourtable) total 
GROUP BY id, 
       name, 
       DATE, 
       team
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文