【问题标题】:month wise headcount of the subcontractor for period Jan 14 to Dec 141 月 14 日至 12 月 14 日期间分包商的月度员工人数
【发布时间】:2015-04-11 15:12:15
【问题描述】:

我有一个表 SOW,其中包含 ASSOCIATE、SOW_START_DATE、SOW_END_DATE 列,我需要在其中获取 2014 年一个月的关联计数,例如,我有关联 Minal,其 sow_start_date 是 1-01-2014 和sow_end_date 是 1-04-2014 我想查询一月、二月、三月和四月的 minal 在哪里可用。 我试过这个查询,但结果是 minal 仅适用于一月而不是一月,二月,三月,四月。

select year([SOW Start Date]) as [year], month([SOW Start Date]) as   [months], Associate
from SOW
where month([SOW Start Date]) in (MONTH([SOW Start Date]), MONTH([SOW End Date]))
and month([SOW Start Date]) =2 and YEAR([SOW Start Date])=2014
group by year([SOW Start Date]), month([SOW Start Date]),Associate 
having count(*) >= 1
order by month([SOW Start Date])

【问题讨论】:

  • 为什么在您的查询中您只过滤 2 月的记录,即通过设置 month([SOW Start Date]) =2 。此外,请通过在 DB 中提供样本记录和您期望的样本 O/p 作为结果,键入您期望的样本准确 O/p。然后它将帮助人们快速回答您的问题
  • 你需要什么? Mimal 可用的月份数或实际月份列表?示例输入和预期输出数据将是最有帮助的。
  • 我想统计每月的雇员人数。
  • @TomT ,我需要具有各自 Start_date 和 End_date 的员工的月数,我的查询仅返回根据 start_date 的员工数,但我想要从 start_date 和 end_date 的员工月数.我有一张桌子 SOW。
  • @TomT 以下是 SOW 表: Associate SOW_Start_Date SOW_End_Date Abhishek 09-07-2013 07-10-2013 Abhishek 17-01-2015 13-03-2015 Tejaswini 03-12-2014 02-03 -2015 Tejaswini 03-12-2014 02-03-2015 Mohini 18-12-2014 16-06-2015 Mohini 18-12-2014 16-06-2015 Anushree 15-12-2014 15-06-2015

标签: sql-server-2008


【解决方案1】:

我了解到您想列出每月的员工人数。问题是您需要将母猪表加入到某个月份列表中。我猜你没有,所以你需要制造它。您需要将母猪表加入到基于开始/结束日期的月份列表中。这样,如果员工在该月工作,您将获得一个月-员工对。最后,您按月分组并计算员工人数:

-- identify the date range
declare @min date, @max date
select @min=min(sow_start_date), @max=max(sow_end_date) from sow

-- we will identify the month by its first day
declare @start date
select @start=DATEADD(month, DATEDIFF(month, 0, @min), 0)

-- generate all months in the min-max range
; with months as
(
  select @start as d

  union all

  select dateadd(mm, 1, d) as d from months where d < @max
)
-- move start dates to 1st day of month so the comparisson works
, sow2 as
(
  select associate, sow_end_date,
    DATEADD(month, DATEDIFF(month, 0, sow_start_date), 0) as sow_start_date
  from sow
)
-- link employees to months
, employee_month as
(
  select d, Associate from sow2 inner join months 
    on d >= sow_start_date and d < sow_end_date
)
-- count employees per month
select d, count(Associate) a from employee_month group by d

看看小提琴http://www.sqlfiddle.com/#!3/52c94b/3/0

【讨论】:

  • 感谢分配,但我需要显示员工的所有数据。即使我尝试修改此查询但无法这样做。请帮助我进行查询,以便我可以在同一查询中添加所有相应的数据。
  • 我不明白你需要什么。正如我在之前的评论中已经提到的,您需要使用示例输入数据和预期输出来更新您的问题。那应该解释它。此外,不要说你的问题很紧急。如果一个月后你仍然被阻止,那显然不是。
猜你喜欢
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 2020-12-26
相关资源
最近更新 更多