Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL聚合函数:另一行中某些行的总和_Sql_Sql Server_Database_Oracle_Sql Server 2008 R2 - Fatal编程技术网

SQL聚合函数:另一行中某些行的总和

SQL聚合函数:另一行中某些行的总和,sql,sql-server,database,oracle,sql-server-2008-r2,Sql,Sql Server,Database,Oracle,Sql Server 2008 R2,我对SQLServer2008有以下场景 **** Original Result **** ================================================ Year month Category Count_days ================================================ 2001 09 Leave 03 2001

我对SQLServer2008有以下场景

           **** 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 Year, month, category, 
   CASE Category
       WHEN 'Present'
          THEN (
          SELECT Sum(T2.Count_days)
          FROM table T2
          WHERE T2.year = T.year
          AND T2.month = T.month
          AND T2.Category NOT IN ('Present', 'Leave')
          )
       ELSE Count_days
   END
FROM table T

但这真的感觉像是一个错误的设计…

您可以使用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  
)
;
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