当值同时可用和不可用时,连接2个表中的行

时间:2019-01-22 21:28:44

标签: sql sql-server

我有2个表“ JobCount”和“ JobSchedule”,分别具有如下所示的行:

JOBID COUNT
1     3
2     2

JOBID EMPID
1     Rahul
1     Mohit
2     Madhu

目标是找出哪些JobId要求得到满足,哪些不满足。当JobSchedule中的行数与特定JobId的JobCount表中Count列所定义的行数相同时,就可以满足JobId要求。 所以我的输出应该是这样的:

OUTPUT

JOBID EMPID REQUIREMENT
1     Rahul T
1     Mohit T
1     NULL  F
2     Madhu T
2     NULL  F  

我正在寻找一个可以在我的jrxml中用作数据集查询的查询。 因此,临时表的过程或插入语句无济于事。还有其他办法吗?请帮忙。

让我知道是否需要更多信息。

2 个答案:

答案 0 :(得分:0)

我个人认为,在JobCount表上显示工作要求是否满足会更容易。这将消除传播的NULL行。或者,仅显示需求,以及当前填补的员工。这是您将如何做的一些版本

--sample data
declare @JobCount table (JOBID int, [COUNT] int)
insert into @JobCount
values
(1,3),
(2,2)


declare @JobSchedule table (JOBID int, EMPID varchar(16))
insert into @JobSchedule
values
(1,'Rahul'),
(1,'Mohit'),
(2,'Madhu'),
(2,'Rahmad')


--clean version, just showing the job
select distinct
    c.JobID
    ,c.COUNT
    ,REQUIREMENT = case when count(s.EMPID) over (partition by s.JOBID order by (select null)) = c.COUNT then 'T' else 'F' end
from @JobCount c
inner join @JobSchedule s on s.JOBID = c.JOBID


--clean version, adding in how many emps are needed
select distinct
    c.JobID
    ,c.COUNT
    ,NEEDED = c.COUNT - count(s.EMPID) over (partition by s.JOBID order by (select null))
    ,REQUIREMENT = case when count(s.EMPID) over (partition by s.JOBID order by (select null)) = c.COUNT then 'T' else 'F' end
from @JobCount c
inner join @JobSchedule s on s.JOBID = c.JOBID

--version showing the current emps in the job
select 
    c.JobID
    ,c.COUNT
    ,s.EMPID
    ,REQUIREMENT = case when count(s.EMPID) over (partition by s.JOBID order by (select null)) = c.COUNT then 'T' else 'F' end
from @JobCount c
inner join @JobSchedule s on s.JOBID = c.JOBID

答案 1 :(得分:0)

您可以展开可用的作业来执行此操作,并为其分配编号。完成此操作后,LEFT OUTER JOIN可以轻松完成您想要的操作,如:

数据:

create table jobcount (
  jobid int,
  count int
);

insert into jobcount (jobid, count) values (1, 3);
insert into jobcount (jobid, count) values (2, 2);

create table jobschedule (
  jobid int,
  empid varchar(20)
);

insert into jobschedule (jobid, empid) values (1, 'Rahul');
insert into jobschedule (jobid, empid) values (1, 'Mohit');
insert into jobschedule (jobid, empid) values (2, 'Madhu');

查询为:

with
d as ( -- produces 10 digits
  select 0 as x union select 1 union select 2 union select 3 union select 4
  union select 5 union select 6 union select 7 union select 8 union select 9
),
n as ( -- generates numbers from 0 to 999
  select d1.x + d2.x * 10 + d3.x * 100 as x
  from d as d1
  cross join d as d2
  cross join d as d3
),
available as ( -- expand available jobs and assign numbers to them
  select
    c.jobid, n.x as rn
  from jobcount c
  join n on n.x > 0 and n.x <= c.count
),
filled as ( -- assigns numbers to filled jobs
  select
    jobid,
    empid,
    row_number() over(partition by jobid order by empid) as rn
  from jobschedule
)
select -- main query that matches available against filled jobs
  a.jobid, 
  b.empid,
  case when b.empid is not null then 'T' else 'F' end as requirement
from available a
left join filled b on a.jobid = b.jobid and a.rn = b.rn
order by a.jobid, requirement desc

结果:

jobid        empid                 requirement  
-----------  --------------------  -----------
1            Mohit                 T            
1            Rahul                 T            
1            <null>                F            
2            Madhu                 T            
2            <null>                F            

如果您的职位空缺超过999个,则需要在n CTE中添加一行。