通过特定的过滤器sql server从特定表中查找计数

时间:2014-05-29 09:50:37

标签: sql sql-server-2008

我有这样的表:  

id  candid  candname        status      date       time     location    jobcode
1   12      hhhhhhhhhh      Introduce   2014-05-21 14:0     NewYork     10JN 
3   12      hhhhhhhhhh      Reject      2014-05-21 15:0 AM  London      10JN
4   12      hhhhhhhhhh      Interview   2014-05-21 15:0 PM  Chicago     10JN
5   11      Pinky Bare      Introduce   2014-05-21 65:6     India       10JN 
6   11      Pinky Bare      Interview   2014-05-21  4:56 AM             10JN
7   13      chetan Tae      Introduce   2014-05-21  4:54 AM Nagpur      faOl
8   13      chetan Tae      Interview   2014-05-21  3:45    Pune        faOl
9   14      manisha mane    Introduce   2014-05-21  3:33 PM Pune        faOl
10  18      ranju gondane   Introduce   2014-05-28  3:44    Nagpur      AQW-06
12  18      ranju gondane   Interview   2014-05-28  5:45    45454       AQW-06
13  18      ranju gondane   Reject      2014-05-28 43:43    rsds        AQW-06
14  19      vandanna rai    Introduce   2014-05-28  7:7     yyyr        AQW-06
 
如果我使用查询

SELECT COUNT(*) FROM [tablename] 
WHERE 
  (jobcode='AQW-06') 
AND
  ([status] <> 'Interview' AND [status] <> 'Reject' 
AND
  [status] <> 'ON-Hold' AND [status] <> 'Hire')

我因为介绍候选人而获得2分。

如果候选人在介绍后接受采访,则不会算作介绍

我想要介绍,采访,拒绝具体的工作代码候选人

请帮助我。

3 个答案:

答案 0 :(得分:2)

你可以尝试

select status, count(*)
from [tablename]
where jobcode = 'AQW-06'
group by status

编辑:你可以尝试使用这样的东西

select count(x.candid) numofcandidates, x.statusnum
from
(select candid, max(case when status = 'Reject' then 3 
                         when status = 'Interview' then 2
                         when status = 'Introduce' then 1 end) statusnum
from [tablename] t 
where jobcode = 'AQW-06' 
group by candid) x
group by x.statusnum;

我实际做的是&#34;翻译&#34;状态为数字,所以我可以先使用最高状态。你需要做的就是&#34;翻译&#34;将statusnum返回到表的值。在我看来,我会直接在我的表中使用statusnum

答案 1 :(得分:0)

试试这个:

;with reftable as
(select 1 'key', 'Introduce' 'val'
 union 
 select 2 'key', 'Interview' 'val'
 union
 select 3 'key', 'Rejected' 'val'
 ),
cte as
(select e.candid, e.[status], row_number() over (partition by e.candid order by r.[key] desc) rn 
 from yourtable e
 inner join reftable r on e.[status] = r.val
 where e.[status] in ('Introduce','Interview','Rejected')
 and e,jobcode = 'AQW-06')

select [status], count([status])
from cte
where rn = 1
group by [status]

基本上,我们为您的文本状态指定一个数值以允许排序。在over子句中,我们按降序排列此数值,以获得所描述的候选者的最高状态。然后,我们只计算每个状态的出现次数。

请注意,您可以将其扩展为包含“Hire”等状态的值。为此,您需要使用适当的数值将其添加到reftable的列表中,并将其添加到cte中的过滤器中。

答案 2 :(得分:0)

  

我想要介绍,采访,拒绝具体工作代码的候选人

以下查询将返回您需要的结果:

 SELECT SUM(t.IsIntroduction) AS CountOfIntroductions,
        SUM(t.IsInterview) AS CountOfInterviews,
        SUM(t.IsRejected) AS CountOfRejections
   FROM (
         SELECT id,
                CASE WHEN Status = 'Introduce' THEN 1 ELSE 0 END AS IsIntroduction,
                CASE WHEN Status = 'Interview' THEN 1 ELSE 0 END AS IsInterview,
                CASE WHEN Status = 'Reject' THEN 1 ELSE 0 END AS IsRejected
           FROM [Tablename]
          WHERE JobCode = 'AQW-06'
        ) AS t

SQL Fiddle的示例。