简体   繁体   English

检索不同类型的月度数据

[英]Retrieving monthly data for different types

I'm working on a report that needs to retrieve 我正在处理需要检索的报告

  • all employees 所有的员工
  • new additions this month 本月新增加
  • new additions this year 今年新增
  • terminations this month 本月终止
  • terminations this year 今年终止

All of these are split up by department (new marketing emps, new sales, etc). 所有这些都是按部门划分的(新的营销经验,新的销售等)。

In total, there'll be about 23 columns.. 总共大约有23列。

I'm using a temporary table by populating each field with an update. 我通过向每个字段填充更新来使用临时表。 Here's a simplified example of what I'm doing. 这是我正在做的简化示例。 Employee table has most of the values needed (hiredate, termdate, etc). 雇员表具有所需的大多数值(受雇者,任期等)。 There's actually a join in each update and some more conditions. 实际上,每个更新中都有一个联接,还有更多条件。

update #tmptbl
set MtdHiresSales = select count(empid) from emp e where e.dept = 012 
                           and hiredate between start_of_month() and getdate()
                           -- more predicates
set MtdHiresMkting = ...repeat... with e.dept=013

I'm sure there's a better way because there's a lot of code duplication. 我敢肯定有更好的方法,因为有很多代码重复。 Are temporary tables appropriate in this case? 在这种情况下,临时表是否合适? I'm not sure how it could be done without one. 我不确定如果没有它怎么办。 Any suggestions? 有什么建议么?

You can probably use a CASE for this. 您可能可以为此使用CASE For example: 例如:

SELECT 
SUM(CASE WHEN e.dept=012 
    AND Hiredate between Start_of_Month() and Getdate() 
    AND... 
    THEN 1 ELSE 0 END) AS 'MtdHiresSales',
...
FROM ...
WHERE ...

You could use a common table expression to encapsulate the employee counting code... 您可以使用公用表表达式来封装员工计数代码...

WITH
  employee_counts
AS
(
   SELECT
     dept         AS "dept",
     x            AS "x",
     COUNT(empid) AS "employees"
   FROM
     emp
   WHERE
     hiredate BETWEEN start_of_month() AND getdate()
     -- etc, etc
   GROUP BY
     dept,
     x
)
UPDATE
  myTable
SET
  MtdHiresSales  = (SELECT employees FROM employee_counts WHERE dept = 012 AND x = myTable.y),
  MtdHiresMkting = (SELECT employees FROM employee_counts WHERE dept = 013 AND x = myTable.y)

Maybe I'm confused or not seeing something, but why not just use aggregates? 也许我很困惑或看不到任何东西,但是为什么不只使用聚合呢?

SELECT count(*) as employeesPerDept, deptId
from mudkips
group by deptId

giving (for my sample data) 给予(用于我的示例数据)

employeesPerDept    deptId
6                   100
4                   200

and

select deptId, count(*) as empsHired, month(hiredate) as monthHired, 
year(hireDate) as yearHired
from mudkips
group by deptId, month(hireDate), year(hireDate)

which then is 那是

deptId  empsHired   monthHired  yearHired
100         4           1           2010
100         1           1           2011
100         1           2           2010
200         2           1           2010
200         1           2           2010
200         1           3           2010

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM