Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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 - Fatal编程技术网

Sql 显示每周总数

Sql 显示每周总数,sql,sql-server,Sql,Sql Server,我正在尝试创建一个报告,显示一周内使用的总时间。我仅成功地使用以下公式显示了每日使用的总量: select left(report_date, 11) as week_start, case when id is null then 'Enterprise' else id end id, case when id in ('CN','NQ','AR','DA'

我正在尝试创建一个报告,显示一周内使用的总时间。我仅成功地使用以下公式显示了每日使用的总量:

select left(report_date, 11) as week_start,
       case
         when id is null then
          'Enterprise'
         else
          id
       end id,
       case
         when id in ('CN','NQ','AR','DA','PR','FM') then
          'West'
         when id = 'Enterprise' then
          'ALL'
         else
          'East'
       end as Region,
       sum(actual_seconds) / 3600 as Actual_Hours,
       sum(goal_seconds) / 3600 as Goal_Hours,
       sum(goal_seconds * 1.0) / nullif(sum(actual_seconds * 1.0), 0) * 100 as Performance
  from summary_table
 where report_date > '2019-01-01 00:00:00.000'
 group by rollup(id), report_date
 order by report_date
如果可能的话,我还想知道“东”和“西”的总数


任何帮助都将不胜感激,提前谢谢

我能够按周进行总结,但按地区添加总数仍然存在问题

Select
    dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date) as week_start, 
   case
         when id is null then
          'Enterprise'
         else
          id
       end id,
       case
         when id in ('CN','NQ','AR','DA','PR','FM') then
          'West'
         when id = 'Enterprise' then
          'ALL'
         else
          'East'
       end as Region,  
        sum(actual_seconds)/3600.0 as Actual_Hours,
        sum(goal_seconds)/3600.0 as Goal_Hours,
        sum(goal_seconds*1.0)/NULLIF(sum(actual_seconds*1.0),0)*100 as performance 
    from     summary_table
    where        dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date) >= 
                      dateadd(day, -365 - datepart(dw, getdate()), getdate())
    group by    dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date), rollup(id)

我能够按周进行总结,但在按地区添加总数时仍然存在问题

Select
    dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date) as week_start, 
   case
         when id is null then
          'Enterprise'
         else
          id
       end id,
       case
         when id in ('CN','NQ','AR','DA','PR','FM') then
          'West'
         when id = 'Enterprise' then
          'ALL'
         else
          'East'
       end as Region,  
        sum(actual_seconds)/3600.0 as Actual_Hours,
        sum(goal_seconds)/3600.0 as Goal_Hours,
        sum(goal_seconds*1.0)/NULLIF(sum(actual_seconds*1.0),0)*100 as performance 
    from     summary_table
    where        dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date) >= 
                      dateadd(day, -365 - datepart(dw, getdate()), getdate())
    group by    dateadd(day, -(datepart(weekday, report_date) + 6 ) % 7, report_date), rollup(id)

将您的
区域表达式添加到
分组依据
?我尝试过,但似乎没有任何效果…按周数分组可以工作->日期部分(周,报告日期)作为周否?我会将区域(还没有做任何数学)添加到子查询中,然后在外部查询中进行数学运算,然后您应该能够按区域分组。但奇怪的是,现在按它分组是如何不起作用的呢?欢迎来到堆栈溢出!一些示例数据和基于该示例的预期结果将非常有用。将您的
区域
表达式添加到
分组依据
?我尝试了此方法,但似乎没有任何效果…按周数分组可以工作->日期部分(周,报告日期)作为周号?我会添加区域(还没有做任何数学计算)到子查询,然后在外部查询中进行计算,然后您应该能够按区域分组。但奇怪的是,现在按它分组是如何不起作用的呢?欢迎来到堆栈溢出!一些样本数据和基于该样本的预期结果将非常有用。