SQL 聚合函数 : Sum of some rows in another row

标签 sql sql-server database oracle sql-server-2008-r2

我有以下使用 sql server 2008 的场景

           **** Original Result ****
================================================
Year   month  Category               Count_days
================================================
2001    09     Leave                   03
2001    09     Worked Below 8hrs       18
2001    09     Worked Above 8hrs       05
2001    09     Present                  0  <----- current value
2001    10     Leave                   01
2001    10     Worked Below 8hrs       10
2001    10     Worked Above 8hrs       09
2001    10     Present                  0   <------ current value

以下是标准

criteria
===========
Present Count of 'x'th Month = SUM(Worked Below 8hrs count of 'x'th month) + 
                               SUM(Worked Above 8hrs count of 'x'th month )

                                    ;where x is the month

我想要满足上述条件的以下结果

           **** Expected Result ****
===============================================
Year   month  Category               Count_days
================================================
2001    09     Leave                   03
2001    09     Worked Below 8hrs       18
2001    09     Worked Above 8hrs       05
2001    09     Present                 23  <-----(expecting  sum 18+05 =23)
2001    10     Leave                   01
2001    10     Worked Below 8hrs       10
2001    10     Worked Above 8hrs       09
2001    10     Present                 19  <-----(expecting sum 10+09 = 19) 

问题是原始结果是由非常复杂的查询生成的,因此不能再次调用相同的集合,即 不能使用这个(这会影响我的应用程序的性能。) =================

select * from original (some join) select * from original

可能需要使用单查询也可以是子查询,使用聚合函数等

期待任何聚合技巧来生成我预期的结果????

请大家帮帮我....

最佳答案

你可以用sum作为解析函数

SELECT 
year, month, cat, count_days as count_days_orig,
case cat 
  when 'Present' 
  then 
     sum (
           case 
             when cat in ('Worked Below 8hrs', 'Worked Above 8hrs') 
             then count_days 
             else 0 
           end
          ) 
     over (partition by year, month) 
  else count_days 
end                                    as count_days_calc
FROM 
(
SELECT 2001    as year, 09 as month ,   'Leave            ' as cat ,      03 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 09 as month ,   'Worked Below 8hrs' as cat ,      18 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 09 as month ,   'Worked Above 8hrs' as cat ,      05 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 09 as month ,   'Present'           as cat ,      0 as count_days FROM dual 
UNION all                                                           
SELECT 2001    as year, 10 as month ,   'Leave            ' as cat ,      01 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 10 as month ,   'Worked Below 8hrs' as cat ,      10 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 10 as month ,   'Worked Above 8hrs' as cat ,      09 as count_days FROM dual
UNION all                                                           
SELECT 2001    as year, 10 as month ,   'Present'            as cat ,      0 as count_days FROM dual  
)
;

     year     month     cat    count_days_orig  count_days_calc
--------------------------------------------------------------------------
    2001    9   Leave               3           3
    2001    9   Worked Below 8hrs   18          18
    2001    9   Worked Above 8hrs   5           5
    2001    9   Present             0           23
    2001    10  Leave               1           1
    2001    10  Worked Below 8hrs   10          10
    2001    10  Worked Above 8hrs   9           9
    2001    10  Present             0           19

关于SQL 聚合函数 : Sum of some rows in another row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17042528/

相关文章:

sql - 将 group by 和 join 组合成一个查询 SQL

mysql - INSERT INTO table IF table exists 否则创建表

sql-server - 无法安装数据库图支持对象...没有有效的所有者

database - 绑定(bind)到一个数据库列的多个 UI 控件的聚合

php - 动态使用从获取的数据查询数组中获取的列名

php - 数据库中具有共同属性和独立属性的元素

sql - 将sql server连接设置为只读?

java - UCanAccess 打开数据库时出现 "User lacks privilege or object not found"错误

mysql - SQL:While 和 Union 统一 SELECT

sql-server - SQL 服务器 2008 : Unique constraint for values non-related with columns