【问题标题】:data according to date根据日期的数据
【发布时间】:2018-01-09 05:04:08
【问题描述】:

我有这样的数据

compnay         employee    service_start_Date  service_end_Date   service_status
abc company     david       01/01/2017          1/1/1900            in_service
abc comapny     john        12/02/2016          1/1/1900            in_service
defcompany      abc_1       05/12/2011          12/02/2017          regisned
ghgcompany      rock        02/11/2009          04/03/2017          terminated
abc company1    david1      01/01/2017          1/1/1900            in_service
abc company     david2      01/01/2017          1/1/1900            in_service
abc company2    david3      01/01/2017          1/1/1900            in_service
abc company     david4      01/01/2015          1/1/2017            in_service
abc company2    david5      01/01/2017          1/1/1900            in_service

……还有很多这样的数据

当员工没有辞职/终止/过期时,默认日期设置如下 1/1/1900 当员工辞职/终止/过期时,service_end_date 被标记,所以我想要数据 比如按月和按年有多少员工加入和离开 像这样

如果我选择参数月份 1 和 2017 年,那么这个显示如下

company          joiners  leavers
abc company         3        1
abc company1        3        0

当我选择像第 5 个月和 2011 年时,就会显示这样

company    joiners  leavers
defcompany  1        0

我试过了

select  e.company,count(e.EmployeeIndex) joiners,count(e.EmployeeIndex) leavers
            from    emp_table e 
            where                   

                     service_end_Date between 
                    convert(datetime,rtrim(convert(char,@FromDate,101)),101) 
                     and  convert(datetime,rtrim(convert(char,@ToDate,101)),101) 

                     and service_start_Date between 
                    convert(datetime,rtrim(convert(char,@FromDate,101)),101) 
                     and  convert(datetime,rtrim(convert(char,@ToDate,101)),101) 
        group by e.company

    this query did not show correct data..

【问题讨论】:

  • 数据库中的service_start_date和service_end_date是什么数据类型?另外,您在失败的查询中作为 FromDate 和 ToDate 参数传递了什么?
  • datetime...如果我只想从 jan 然后从日期 '01/01/2017' 到日期 '01/30/2017' 获取数据,我会这样通过

标签: sql-server sql-server-2008 date


【解决方案1】:

您当前的查询为加入者和离开者计算了两次相同的事物。您需要将检查移动到这些列的选择区域。这是基本思路

select  e.company,
    SUM(case when service_start_date between @FromDate and @ToDate then 1 else 0 end) as joiners
    , SUM(case when service_end_date between @FromDate and @ToDAte then 1 else 0 end) as leavers

from    emp_table e 
        where                   
            service_end_Date between @FromDate and @ToDate
        OR --not AND
            service_start_Date between @FromDate and @ToDate
group by e.company

您可以关闭整个底部 where 子句,它仍然可以工作,但即使两个数字都为零,它也会返回所有公司(这可能需要也可能不需要)。

【讨论】:

  • 因为我们使用 case 将有效行的标志设置为 1,无效行设置为零,所以您需要对数字求和。如果你使用count,你只会得到你检查的行数,而不是有效的数字
  • 当我在 e.employee 上使用 count 时会怎样?
  • 当我在表中随机检查时显示出一些错误,例如当我使用 select * from emp_table where company_id=850 and ServiceStatus=1 and service_start_date between '01/01/2017' 和 '01/30 /2017' 那么这仅显示 1 名 in_Service 员工,当我使用您的查询并检查时,这显示 2 名加入者表示该日期期间的 in_Service 员工
  • ServiceStatus 跟它有什么关系?您之前从未在查询中提及或使用过它
  • 好的,如果使用 servicestatus='in_Service' 那么这显示与提到的相同..
【解决方案2】:

您可以使用以下查询来实现此目的:

SELECT *
 FROM (
  SELECT employee, compnay
  ,Case when year(service_end_Date) = 1900 then
  'joiners'
  else 
  'leavers' end as Company_Year
  FROM [emp_table] 
  -- where  year(service_start_Date) = 2011  and month(service_start_Date) = 5
 ) as s
 PIVOT
 (
  count(employee)
  FOR [Company_Year] IN (joiners,leavers)
 )AS pvt;

【讨论】:

  • 这显示了总..根据公司我想要的地方
  • 以及为什么要依靠 companyname.. 我需要依靠 e.employee.. 我指的是员工而不是公司..
  • @see sharp,我已更新查询以显示公司员工总数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 2012-07-14
  • 2023-03-23
  • 2017-02-15
  • 2018-02-24
  • 1970-01-01
相关资源
最近更新 更多