简体   繁体   English

SQL中子查询中列的求和运算

[英]Sum operation on a column from subquery in SQL

Might just be my approach is bad. 可能只是我的方法不好。 I have the following schema in my DB: 我的数据库中有以下架构:

work_instances_table(wk_id, wk_occurance_id, start_time, end_time)
time_table(time_id, hour, minute, period)

Multiple work_occurances can be under one wk_id . work_occurances可以下一个wk_id eg: 例如:

wk_id | wk_occurance_id | start_time | end_time

1     | wk_01           | 111        | 111
2     | wk_02           | 345        | 345
1     | wk_03           | 654        | 655
1     | wk_04           | 132        | 132
2     | wk_05           | 051        | 053

and in the time_table we can join start_time and end_time with time_id . time_table我们可以将start_timeend_timetime_id结合time_id

Suppose I want to get 假设我想得到

number of all work occurrences that started between two times say 0 to 3 AM 两次之间开始的所有工作发生的次数,例如0到3 AM

I tried 我试过了

select w1.wk_id,
      COUNT(w1.wk_occurance_id) as total_wk_occurances 
      from work_instances w1 
      inner join 
          time_dimension td1 on w1.start_time = td1.time_id  
      where 
          (td1.hour between 0 and 3  and td1.minute between 0 and 59 and td1.period = 'AM')
      group by w1.wk_id;

So this gives me 所以这给了我

    1  |  2
    2  |  1

1 has 2 occurrences and 2 has only 1. 1次出现2次,而2次出现1次。

Now how to sum this up? 现在如何总结呢?

Means how to extend the query to say total 2 works and 3 occurrences ran? 意味着如何扩展查询以说total 2 works and 3 occurrences ran?

Please explain and extend the originally used query itself maybe by writing it as subquery. 请通过将其编写为子查询来解释和扩展最初使用的查询本身。

use sub-query and aggregation 使用子查询和聚合

  select count(wk_id) as numberofwork,
  sum(total_wk_occurances) as occurrences   from   
   (  select w1.wk_id,
      COUNT(w1.wk_occurance_id) as total_wk_occurances 
      from work_instances w1 
      inner join 
          time_dimension td1 on w1.start_time = td1.time_id  
      where 
          (td1.hour between 0 and 3  and td1.minute between 0 and 59 and td1.period = 'AM')
      group by w1.wk_id
   ) as  t1

same thing you can write below way 你可以用下面的方式写同样的东西

with cte as
(
select w1.wk_id,
          COUNT(w1.wk_occurance_id) as total_wk_occurances 
          from work_instances w1 
          inner join 
              time_dimension td1 on w1.start_time = td1.time_id  
          where 
              (td1.hour between 0 and 3  and td1.minute between 0 and 59 and td1.period = 'AM')
          group by w1.wk_id
)  select count(wk_id),sum(total_wk_occurances) as occurrences from cte

I think this simpler solution should work, note that there is know need to look at the minute column if you are only interested in full hours. 我认为这个更简单的解决方案应该可以工作,请注意,如果您只对全职工作感兴趣,那么有必要查看分钟栏。

SELECT COUNT(distinct w.wk_id), COUNT(distinct w.wk_occurance_id)
FROM work_instances_table w
JOIN time_table t ON t.time_id = w.start_time 
WHERE t.hour >= 0 AND t.hour <= 3

In the question it says between 0 and 3 AM so if that is what you want then change the where clause to something similar to this 在问题中它说在0到3 AM之间,因此如果您要这样做,则将where子句更改为与此类似的内容

WHERE (t.hour >= 0 AND t.hour <= 2 AND t.minute >= 0 and t.minute <= 59)
   OR (t.hour = 3 AND t.minute = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM