简体   繁体   English

如果所有值都为null,则SQL select列具有相同的键

[英]SQL select column with same key if all values are null

I have two columns , ColumnA and ColumnB, occassionally columnB doesnt gets populated and it should e. 我有两列ColumnA和ColumnB,有时columnB不会被填充,所以应该e。 I'm looking for a query that will only select if all ColumnA is unpopulated. 我正在寻找仅在所有ColumnA未填充的情况下才会选择的查询。

ColumnA|ColumnB
Apples|
Apples|
Apples|Orange

This is what i'm but this is incorrect because it says ColumnA is null with the same value and ColumnB is populated. 这就是我的意思,但这是不正确的,因为它说ColumnA为null且具有相同的值,并且已填充ColumnB。 I want the query only to return rows if all of columnB is unpopulated. 如果所有columnB都未填充,我只希望查询返回行。

SELECT ColumnA
 FROM tblMyTable
WHERE ColumnA IN
      (SELECT ColumnA 
         FROM tblMyTableB
        WHERE ColumnB IS NULL)

You current query gives you too many results. 您当前的查询给您太多的结果。 The ones you want to eliminate are those where there is a ColumnB value: 您要消除的具有ColumnB值的那些:

SELECT ColumnA
 FROM tblMyTable
WHERE ColumnA IN
      (SELECT ColumnA 
         FROM tblMyTableB
        WHERE ColumnB IS NULL)
AND NOT ColumnA IN
      (SELECT ColumnA 
         FROM tblMyTableB
        WHERE ColumnB IS NOT NULL)

Or, smarter is: 或者,更聪明的是:

select ColumnA,COUNT(ColumnB) from tblMyTable
group by ColumnA having COUNT(ColumnB) = 0

Because COUNT(Expression) only counts non-null expression values 因为COUNT(Expression)仅计算非空表达式值

It looks like your logic is backwards: 看来您的逻辑倒退了:

  • Your query finds values in column A where there is a NULL value in column B. 您的查询在A列值,其中 B列NULL值
  • I think you want the values in column A where there isn't a non-NULL value in column B. 我认为您想要B列中没有 非NULL值的A列中的值。

Try adding NOT in two places and add DISTINCT to avoid getting duplicate results: 尝试在两个位置添加NOT并添加DISTINCT以避免重复的结果:

SELECT DISTINCT ColumnA
FROM tblMyTable
WHERE ColumnA NOT IN
      (SELECT ColumnA 
       FROM tblMyTableB
       WHERE ColumnB IS NOT NULL)

In addition, if ColumnA can be NULL then you'll have to exclude those NULL values from your inner query otherwise the NOT IN expression will return NULL instead of True and so no results will be returned: 另外,如果ColumnA可以为NULL,则必须从内部查询中排除这些NULL值,否则NOT IN表达式将返回NULL而不是True ,因此将不返回任何结果:

SELECT DISTINCT ColumnA
FROM tblMyTable
WHERE ColumnA NOT IN
      (SELECT ColumnA 
       FROM tblMyTableB
       WHERE ColumnA IS NOT NULL
       AND ColumnB IS NOT NULL)

Using EXCEPT. 使用EXCEPT。 This can be expressed as 这可以表示为

Get Column A, EXCEPT where some non-null in Column B for that column A 获取A列,但该列A的B列中的一些非空值除外

DECLARE @MyTable TABLE (ColumnA varchar(20) NOT NULL, ColumnB varchar(20) NULL);

INSERT @MyTable VALUES
  ('Apple', NULL),('Apple', NULL),('Apple', 'Orange'),
  ('Banana', NULL),('Banana', NULL), 
  ('Strawberry', 'Pie'), ('Strawberry', 'Pie')

SELECT ColumnA FROM @MyTable
EXCEPT
SELECT ColumnA FROM @MyTable WHERE ColumnB IS NOT NULL

More on EXCEPT: Why does EXCEPT exist in T-SQL? 有关EXCEPT的更多信息: 为什么T-SQL中存在EXCEPT?

暂无
暂无

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

相关问题 SQL 选择具有相同列值的所有项目 - SQL select all items with the same column values SQL - 选择列中所有值相同的行 - SQL - Select rows where all values in a column are the same 为什么 Hive SQL 返回 NULL 值对于 ZE0626222614BDEE 语句中的特定列具有所有双值时的 NULL 值? - Why Hive SQL returning NULL values for a particular column in Select statement when that column has all double values? 选择计数同一列但没有NULL值 - Select count same column but no NULL values ORACLE SQL:根据另一列中的相同键值选择基于一列中不同值的多行 - ORACLE SQL: select multiple rows basd on different values in one column for a same key value in another column oracle sql-如果该列中没有空值,则仅选择 - oracle sql - Select ONLY if there are NO null values in that column 在 SQL 中获取列 A 中具有相同值的所有行,这些行在列 B 中只有非空值 - In SQL get all rows with same value in column A that have only non-null values in column B 将带有键列的表扩展到带有 null 值的完整表 - SQL 服务器 select 查询 - Expand table with key column to full table with null values - SQL Server select query SQL - 如何 select 行具有相同的 ID 值,其中所有其他列值也相同 - SQL - How to select rows with the same ID values where all other column values are also identical SQL选择同一列中具有不同值的记录 - SQL select records with different values in same column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM