简体   繁体   English

如何从一个表中选择特定年份不存在的所有记录?

[英]How to select all records from one table that do not exist in particular year?

the table looks like this 桌子看起来像这样

num  Year
 1 | 2014
 2 | 2014
 3 | 2014
 2 | 2015
 4 | 2015
 5 | 2015
 6 | 2015

I would like my query to return 我希望查询返回

 4 | 2014
 5 | 2014
 6 | 2014
 1 | 2015
 3 | 2015

from 1 to 6, the number that is not used in particular year. 从1到6,特定年份未使用的数字。

Generate the all the combinations and then take out the ones that exist: 生成所有组合,然后取出存在的组合:

select n.num, y.year
from (select distinct num from t) n cross join
     (select distinct year from t) y left join
     t
     on t.num = n.num and t.year = y.year
where t.num is null;

Note that year is a bad name for a column in SQL Server because it is the name of a function and a keyword (think datepart() ). 请注意, year是SQL Server中列的错误名称,因为year是函数和关键字的名称(请考虑datepart() )。

暂无
暂无

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

相关问题 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table? 如何从一个表中选择另一张表中不存在的所有记录? - How to select all records from one table that do not exist in another table? sql-从一个表中选择另一个表中不存在的记录 - sql - Select records from one table that do not exist in the other Select 针对给定年份的所有月份的记录形成一个表,其中记录来自第二个表 - Select Records against all months of a given year form one table with records from second table 如何显示一个表中不存在于另一表中的记录 - How to show records from one table that do not exist in another table Select 一个表中的所有记录和另一个表中的匹配记录或 Null(如果不存在) - Select All Records From One Table And Matching Record From Other Table or Null (if not exist) 如何从所有记录中选择特定的记录集 - how to select particular set of records from all the records mysqli_fetch_all不能从一个特定表中检索记录 - mysqli_fetch_all not retrieving records from one particular table 如何从一个表中获取相关表中不存在的记录? - How can I get records from one table which do not exist in a related table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM