使用SQL中的内部联接计算表值

时间:2015-07-13 04:17:34

标签: sql-server

我有两张桌子:

  1. application_info
  2. application_movement
  3. 当用户申请该职位时,详细信息将被插入application_info表,并且application_movement表中将更新人员活动,例如暂停候选人,拒绝,候选名单和时间表。

    application_info

    applicationId    appliedjobID    emailID               status
    -------------------------------------------------------------------
         1                3          usercandi@gmail.com   pending
         2                9          user2@gmail.com       pending
         3                2          user3@gmail.com       pending
    

    applicaiton_movement

    ApplicationId  Fromstatus  toStatus         movementemailID
    --------------------------------------------------------
    1                pending      hold             hr@gmail.com
    1                hold        **decline**       hr@gmail.com
    2                pending      shortlist        hr1@gmail.com
    2                shortlist   **scheduled**     hr1@gmail.com
    3                pending     **scheduled**     hr@gmail.com
    

    人力资源部门将持有,拒绝,入围名单和预定,我需要计算hr@gmail.com和hr1@gmail.com(她做了多少活动,如持有,拒绝,入围名单和{{{ 1}}列)

    tostatus中,application_movement是两次或更多次。所以只需要计算上次更新的ApplicationID次数。

    tostatus

    只需要在 i.e hr@gmail.com decline(1) scheduled(1) hr1@gmail.com scheduled(1) 的两个表中选择查询基数来获取计数。

3 个答案:

答案 0 :(得分:0)

select count(distinct(toStatus)))  ,toStatus         , movementemailID
 where tostatus in ('decline' , 'shortlist' , 'scheduled')
group by toStatus         , movementemailID

希望它足以满足您的要求,它会为您提供这样的输出,然后您可以根据您的喜好在toStatus或movementemailID上下订单。

Count            toStatus         movementemailID
--------------------------------------------------------
1                decline       hr@gmail.com
1                 shortlist        hr1@gmail.com
1                scheduled     hr1@gmail.com
1                scheduled     hr@gmail.com

答案 1 :(得分:0)

您可以使用PIVOT来获取自己列中最终动作的数量

SELECT movementemailID
, [**decline**]
, [**scheduled**]
FROM (SELECT movementemailID, applicationId, tostatus
     FROM applicaiton_movement
     WHERE tostatus IN ('**decline**', '**scheduled**')) AS app_mov
PIVOT (COUNT(applicationId)
      FOR tostatus IN ([**decline**]
                      , [**scheduled**])
      ) AS info

SQL Fiddle

答案 2 :(得分:0)

你正在寻找这样的东西吗?

Select Count(*) as numberOfHolds from applicaiton_movement Where movementemailID = hr@gmail.com and toStatus = 'hold'

如果是这样,您可以轻松编写一个程序,对所有用户应用的所有状态标记进行计数,并以此格式返回。

CountHold  | CountDecline |    movementemailID
--------------------------------------------------------
   1       |        2     |       me@yahoo.com
   3       |        3     |       them@xbc.com
   2       |       8000   |       hr1@gmail.com
   1       |        4     |       hr@gmail.com

像这样: 创建函数,

CREATE FUNCTION [dbo].[CountDeclines]
(
    @user nvarchar(50)
)
RETURNS @result int

AS
BEGIN
    Select @result = Count(*)  from applicaiton_movement    Where movementemailID = @user and toStatus = '**decline**'
END

现在调用函数

Select CountDecliens(movementemailID) as [countdeclines], movementemailID from applicaiton_movement