0

我需要确定每个部门有多少项目超出了他们的每小时预算。我已经设置好了,所以我减去了最大分配小时数的总工作时间,如果这个数字是正数,那么项目超出了预算。

它显示为:

| Department | ProjectMaxHours | TotalHoursWorked | Balance |

| Marketing  |          135.00 |           160.00 |   25.00 |
| Finance    |          120.00 |            85.00 |  -35.00 |
| Accounting |          145.00 |           130.00 |  -15.00 |
| Marketing  |          150.00 |           165.00 |   15.00 |
| Finance    |          140.00 |            52.50 |  -87.50

我现在想从这个视图创建另一个视图,列出标记有 2 个超支项目,财务有 0,会计有 0。

关于我如何做到这一点的任何想法?

4

2 回答 2

0
create view overspent as
  select department, count(*) as num_over_projects
  from hours
  where balance > 0
  group by department
于 2013-09-26T17:23:30.433 回答
0
select   sum(cnt),
         Department
from     (select 0 as cnt,
                 Department
          from   [hours]
          where  Balance < 0
          union all
          select 1,
                 Department
          from   [hours]
          where  Balance > 0) as g
group by Department;
于 2013-09-26T17:27:37.420 回答