我有两个SQL Server表,我试图比较第二个表中缺少的记录。
第一个表是主表,它只包含职称(例如Web Developer,DBA等)和级别(例如Manager,Expert等)
第二张表中列出了具有潜在头寸和利率的公司。每个公司都应该拥有主表中的每个职位和级别组合,而许多公司都没有。
Master
表 - Title | Level
Positions
表 - Employer | Title | Level | Rate
我尝试过使用JOINS(所有类型),NOT IN
和NOT EXISTS
。
我想出的所有人都没有数据或误报。
我需要知道哪些雇主错过了什么标题和级别组合,所以我可以加载它们。
感谢您的投入!
答案 0 :(得分:1)
FIXED ...下面的代码示例将输出一行"雇主B / CFO" (在INSERT期间注释掉的一个位置)。如果您没有雇主表(我认为您应该这样做),那么您可以随时SELECT DISTINCT Employer FROM PositionsTable
代替。
DECLARE @MasterTable TABLE
(
Title varchar(100),
Level int
)
DECLARE @PositionsTable TABLE
(
Employer varchar(100),
Title varchar(100),
Level int,
Rate int
)
DECLARE @Employers TABLE
(
Employer varchar(100)
)
INSERT INTO @Employers VALUES ('EmployerA')
INSERT INTO @Employers VALUES ('EmployerB')
INSERT INTO @MasterTable VALUES ('CEO', 1)
INSERT INTO @MasterTable VALUES ('CFO', 2)
INSERT INTO @MasterTable VALUES ('CTO', 2)
INSERT INTO @PositionsTable VALUES ('EmployerA', 'CEO', 1, 100)
INSERT INTO @PositionsTable VALUES ('EmployerA', 'CFO', 2, 100)
INSERT INTO @PositionsTable VALUES ('EmployerA', 'CTO', 2, 100)
INSERT INTO @PositionsTable VALUES ('EmployerB', 'CEO', 1, 100)
--INSERT INTO @PositionsTable VALUES ('EmployerB', 'CFO', 2, 100)
INSERT INTO @PositionsTable VALUES ('EmployerB', 'CTO', 2, 100)
SELECT *
FROM @Employers e
CROSS JOIN @MasterTable mt
LEFT JOIN @PositionsTable pt ON e.Employer = pt.Employer AND mt.Title = pt.Title AND mt.Level = pt.Level
WHERE pt.Employer IS NULL
CROSS JOIN
用于获取雇主和预期职位的完整列表,然后LEFT JOIN
可以确定缺少哪些职位。您可以将结果集简化为您感兴趣的列,但如果这样做,请确保选择e.Employer
而不是pt.Employer
(将为NULL)。
答案 1 :(得分:0)
检查此查询示例
create table #master(title varchar(100),level varchar(100))
create table #position(employer varchar(100),title varchar(100),level varchar(100))
insert into #master values('A','A1')
insert into #master values('B','B1')
insert into #master values('C','C1')
insert into #master values('D','D1')
insert into #position values('E1','A','A1')
insert into #position values('E2','A','A1')
insert into #position values('E3','A','A1')
insert into #position values('E4','A','A1')
insert into #position values('E5','X','X1')
select * from
(select m.title,m.level,p.employer from #master m cross join #position p
except
select m.title,m.level,p.employer from #master m inner join #position p
on m.title=p.title and m.level=p.level) a
order by employer
答案 2 :(得分:0)
我的回答:
declare @Master table (
Title varchar(50),
Level varchar(50)
)
declare @Positions table (
Employer varchar(50),
Title varchar(50),
Level varchar(50),
Rate varchar(50)
)
INSERT INTO @Master (Title, Level) VALUES ('Web Developer', 'Manager')
INSERT INTO @Master (Title, Level) VALUES ('Web Developer', 'Senior')
INSERT INTO @Master (Title, Level) VALUES ('Web Developer', 'Junior')
INSERT INTO @Master (Title, Level) VALUES ('DBA', 'Manager')
INSERT INTO @Master (Title, Level) VALUES ('DBA', 'Senior')
INSERT INTO @Master (Title, Level) VALUES ('DBA', 'Junior')
INSERT INTO @Positions (Employer, Title, Level, Rate) VALUES
('ABC Corp', 'Web Developer', 'Manager', ''),
('ABC Corp', 'Web Developer', 'Senior', ''),
('ABC Corp', 'DBA', 'Manager', ''),
('ABC Corp', 'DBA', 'Junior', ''),
('DEF Corp', 'Web Developer', 'Manager', ''),
('DEF Corp', 'Web Developer', 'Senior', ''),
('DEF Corp', 'Web Developer', 'Junior', '')
-- expect difference to return ABC missing WD-Junior, DBA-Senior, and DEF all DBA
; with cte as
(select distinct
m.Title as master_title,
m.Level as master_level,
p.Employer
from
@Master m CROSS JOIN @Positions p
)
select
c.Employer,
c.master_title,
c.master_level,
p.*
from cte c LEFT outer join @Positions p on p.Employer = c.Employer and p.Level = c.master_level and p.Title = c.master_title
where
p.Employer IS NULL
order by
c.Employer,
c.master_title,
c.master_level