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..
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
CASEfor this. For example: