简体   繁体   中英

Joining rows from 2 tables when value is both available and not available

I have 2 tables 'JobCount' & 'JobSchedule' with respective rows as shown below :

JOBID COUNT
1     3
2     2

JOBID EMPID
1     Rahul
1     Mohit
2     Madhu

The goal is to find out which JobId requirement is fulfilled and which are not. A JobId requirement is said to be fulfilled when we have the same number of rows in JobSchedule as defined by Count column in JobCount table for a particular JobId. So my output should be something like :

OUTPUT

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

I am looking for a single query which can be used as dataset query in my jrxml. So procedures or insert statements for temp table are not helpful. Is there any other way ? Please help.

Let me know if need any more information required.

Personally, I think it would be easier to show if the job requirement is fulfilled on the JobCount table. This would eliminate the propagated NULL rows. Or, just show the requirement, and the currently filled employees. Here is how you would do a few versions of this

--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

You can expand the available jobs to do this, and assign numbers to them. Once you do that a LEFT OUTER JOIN will easily do what you want, as in:

With the data:

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');

The query would be:

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

Result:

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

You'll need to add an extra row in the n CTE if you have job positions with more than 999 openings each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM