正在组合“具有聚合和分组依据的两个查询”(结果集)可能的?

发布于 2024-09-24 08:22:00 字数 1422 浏览 3 评论 0原文

1

Select id,count(*) as totalX FROM my_table WHERE x_factor = 1 GROUP BY id

ResultSet:

 id        totalX
 --------- --------------
         9             34
        10              6
        11             21
        12              3  

2

Select id,count(*) as totalY FROM my_table WHERE y_factor = 1 GROUP BY id

ResultSet 2:

 id        totalY
 --------- --------------
         9             334
        10              56
        11             251
        12              93 

有办法实现这一点吗:

 id        totalX        totalY         
 --------- --------------  --------------  
         9             34              334 
        10              6               56 
        11             21              251 
        12              3               93 

我也想要 RHEL 5 上的 Sybase 12.5 的解决方案我想知道这在任何其他数据库系统中是否可行。

---感谢您的回答--

Comparing EXECUTION TIME: (For a certain query) 
 Query 1:
Execution Time 61.
SQL Server cpu time: 6100 ms.  SQL Server elapsed time: 12133 ms.

Query 2:
Execution Time 53.
SQL Server cpu time: 5300 ms.  SQL Server elapsed time: 12090 ms.

Query X(1+2):
Execution Time 84.
SQL Server cpu time: 8400 ms.  SQL Server elapsed time: 21456 ms.

1

Select id,count(*) as totalX FROM my_table WHERE x_factor = 1 GROUP BY id

ResultSet:

 id        totalX
 --------- --------------
         9             34
        10              6
        11             21
        12              3  

2

Select id,count(*) as totalY FROM my_table WHERE y_factor = 1 GROUP BY id

ResultSet 2:

 id        totalY
 --------- --------------
         9             334
        10              56
        11             251
        12              93 

Is there a way I can achieve this:

 id        totalX        totalY         
 --------- --------------  --------------  
         9             34              334 
        10              6               56 
        11             21              251 
        12              3               93 

I would like the solution for Sybase 12.5 on RHEL 5, also I would like to know if this is possible in any other database system.

---thanks for the answer(s)--

Comparing EXECUTION TIME: (For a certain query) 
 Query 1:
Execution Time 61.
SQL Server cpu time: 6100 ms.  SQL Server elapsed time: 12133 ms.

Query 2:
Execution Time 53.
SQL Server cpu time: 5300 ms.  SQL Server elapsed time: 12090 ms.

Query X(1+2):
Execution Time 84.
SQL Server cpu time: 8400 ms.  SQL Server elapsed time: 21456 ms.

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

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

发布评论

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

评论(3

指尖微凉心微凉 2024-10-01 08:22:00

通过对列使用 CASE/WHEN 并根据 true/false 对 1 或 0 求和,您可以在同一个查询中获得这两个值...此外,如果您想要另一个值中某个值的总和,您可以执行相同的操作列...只需将其替换为真实值而不是 1。

select 
      id,
      sum( CASE WHEN x_factor = 1 THEN 1 ELSE 0 END ) as X_Count, 
      sum( CASE WHEN y_factor = 1 THEN 1 ELSE 0 END ) as Y_Count
  from
      yourTable
  group by
      id

By using a CASE/WHEN for the column and summing a 1 or 0 based on true/false, you can get both in the same query... Additionally, you could do the same thing if you wanted the sum of some value in another column... just substitute that for the true value instead of 1.

select 
      id,
      sum( CASE WHEN x_factor = 1 THEN 1 ELSE 0 END ) as X_Count, 
      sum( CASE WHEN y_factor = 1 THEN 1 ELSE 0 END ) as Y_Count
  from
      yourTable
  group by
      id
揽清风入怀 2024-10-01 08:22:00

这应该有效:

SELECT id, 
       sum(case when x_factor = 1 then 1 else 0 end) as totalX,
       sum(case when y_factor = 1 then 1 else 0 end) as totalY
    FROM my_table 
    WHERE x_factor = 1 
        OR y_factor = 1
    GROUP BY id

This should work:

SELECT id, 
       sum(case when x_factor = 1 then 1 else 0 end) as totalX,
       sum(case when y_factor = 1 then 1 else 0 end) as totalY
    FROM my_table 
    WHERE x_factor = 1 
        OR y_factor = 1
    GROUP BY id
那些过往 2024-10-01 08:22:00

CASE ... {0|1} 对于人们来说是一个很好的技巧,但我认为问题可能比这更简单。您是否尝试过:

SELECT id,
COUNT(x_factor) AS count_x,
COUNT(y_factor) AS count_y
来自我的表
按 ID 分组

The CASE ... {0|1} is a nice trick for people to know, but I think the issue may be simpler than that. Have you tried:

SELECT id,
COUNT(x_factor) AS count_x,
COUNT(y_factor) AS count_y
FROM my_table
GROUP BY id

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