简体   繁体   English

T-SQL 如何根据其他表中的列列出表中不存在的行

[英]T-SQL How to list rows not present in table based on columns from other tables

Looking for some help in what is likely quite a simple problem but the wording makes it hard for me to see if a similar question has been asked.在可能很简单的问题上寻找一些帮助,但措辞让我很难看到是否有人问过类似的问题。

In SQL Server I have 3 tables:在 SQL Server 中,我有 3 个表:

  • Colleague (ColleagueID (INT), Name (NVARCHAR), Branch ) Colleague (ColleagueID (INT), Name (NVARCHAR), Branch )
  • Module (ModuleID (INT), Course (NVARCHAR), DueDate)
  • ModuleCompletion (RecordID (INT), Colleague (NVARCHAR), Module (NVARCHAR)

This is a quickly constructed set of tables based on the data collected previously which the company stored in Excel, eg A Worksheet Matrix of (Colleagues Down Y) and (Modules Across X) and a 1 for completed and a 0 for not completed.这是一组基于之前收集的公司存储在 Excel 中的数据快速构建的表格,例如(同事向下 Y)和(跨 X 的模块)的工作表矩阵,1 表示已完成,0 表示未完成。

I grabbed all the 1's and uploaded them to a ModuleCompletion table to keep track of who completed what.我抓住了所有的 1 并将它们上传到 ModuleCompletion 表以跟踪谁完成了什么。
As it stands there are no Foreign Keys linking the tables and no UNIQUES etc.就目前而言,没有链接表的外键,也没有 UNIQUES 等。

Example tables:示例表:

Colleague同事

ID   Name  Branch  
-----------------
1    xyz    HO  
2    abc    HO  
3    tuv    HO  
4    efg    Branch1

Module模块

ID   Course  DueDate  
-----------------------
1    Co1     2019-12-31  
2    Co2     2020-01-30  

ModuleCompletion模块完成

ID   Colleague  Module
-----------------------
1    xyz        Co1
2    xyz        Co2
3    abc        Co1
4    tuv        Co2

What I am looking to have returned is:我希望返回的是:

Module  Branch   Colleague
--------------------------
Co1     HO       tuv
Co1     Branch1  efg
Co2     HO       abc
Co2     Branch1  efg

The result set should be each Branch and colleague combination not listed with a Module in the ModuleCompletion table.结果集应该是ModuleCompletion表中未与 Module 一起列出的每个 Branch 和同事组合。

Using:使用:

SELECT c.Branch, c.Name
FROM Colleague c
LEFT JOIN ModuleCompletion x ON x.Colleague = c.Name 
WHERE x.Colleague IS NULL
ORDER BY c.Branch, c.Name

Displays all branch and colleague names where they have completed no courses, eg the Colleague name never appears in the ModuleCompletion table, but I need it to check by module.显示他们没有完成任何课程的所有分支和同事姓名,例如,同事姓名从未出现在ModuleCompletion表中,但我需要它来按模块检查。

Any help will be appreciated.任何帮助将不胜感激。

Don't left join, but cross join.不要左连接,而是交叉连接。

from Colleague c
cross join Module m
where not exists (
  select *
  from ModuleCompletion mc
  where c.Name   = mc.Colleague
  and   m.Course = mc.Module
)

Basically you can play on from here基本上你可以从这里开始玩

Based on the previous solution and to avoid nested select you could do something like that too:基于先前的解决方案并避免嵌套选择,您也可以执行以下操作:

SELECT m.Course, c.Branch, c.Name 
  FROM Collegue     as c
 CROSS JOIN Module  as m
EXCEPT 
SELECT m.Course, c.Branch, c.Name
  FROM ModuleCompletion as mc
 INNER JOIN Collegue    as c 
         ON c.Name      = mc.Colleague
 INNER JOIN Module      as m
         ON m.Course    = mc.Module

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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