我如何计算有多少项目超出预算?

时间:2013-09-26 16:37:35

标签: mysql view

我需要确定每个部门的每小时预算中有多少项目。我已经设置好了,因此我减去了最大分配小时数的总工作小时数,如果数字是正数,则项目超出预算。

显示为:

| 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

我现在想要从这个视图创建另一个视图,列出Marking有2个超支项目,Finance有0,Accounting有0。

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

2 个答案:

答案 0 :(得分:0)

create view overspent as
  select department, count(*) as num_over_projects
  from hours
  where balance > 0
  group by department

答案 1 :(得分: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;