SQL 查询:仅从组中选择 NULL 值?
SQL query: selecting only NULL values from a group?
我正在处理看起来像这样的数据 table:
Names City Date Color Shape
John Smith Baltimore 8/1/2015 Blue
John Smith Baltimore 8/1/2015 Green
John Smith Baltimore 8/1/2015 Rectangle
John Smith Baltimore 8/1/2015
John Smith Baltimore 8/1/2015 Square
John Smith Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015 Circle
Greg Jackson Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015 Red
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
我的查询目标是 SELECT 存在的所有五种数据类型,但要隔离名称字段中颜色和形状字段中具有 NULL 值的那些值。我在 MS Access 中写这个 SQL。到目前为止,我的查询如下所示:
SELECT [Names], [City], [Date], [Color], [Shape]
FROM [databasename]
WHERE
(
([Color] IS NULL)
AND
([Shape] IS NULL)
);
根据示例数据 table,我希望结果只包括 Rob Johnson,因为与该名称条目关联的所有行的颜色和形状字段都具有 NULL 值。但是,通过此查询,我也获得了所有其他名称,并返回了 Color 和 Shape 字段中具有 NULL 值的特定行。
因此,预期的输出将如下所示:
Names City Date Color Shape
Rob Johnson Baltimore 8/1/2015
我怀疑我需要在这里使用 GROUP 运算符,但我不太确定该怎么做。有什么想法吗?
我想你想要这个:
SELECT
DISTINCT [Names], [City], [Date], [Color], [Shape]
FROM [table]
WHERE [Names] NOT IN (
SELECT [Names] FROM [table] WHERE ([Color] IS NOT NULL) OR
([Shape] IS NOT NULL)
);
可以通过其他方式完成,但这应该接近您的原始查询。
您可以使用聚合和内部联接:
SELECT d1.* FROM [database-name] d1
INNER JOIN (
select Names,MAX(Color) as mc,MAX(Shape) as ms
from [database-name]
group by Names
) d2
ON d1.Names = d2.Names
WHERE mc IS NULL
AND ms IS NULL
我正在处理看起来像这样的数据 table:
Names City Date Color Shape
John Smith Baltimore 8/1/2015 Blue
John Smith Baltimore 8/1/2015 Green
John Smith Baltimore 8/1/2015 Rectangle
John Smith Baltimore 8/1/2015
John Smith Baltimore 8/1/2015 Square
John Smith Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015 Circle
Greg Jackson Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015 Red
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
我的查询目标是 SELECT 存在的所有五种数据类型,但要隔离名称字段中颜色和形状字段中具有 NULL 值的那些值。我在 MS Access 中写这个 SQL。到目前为止,我的查询如下所示:
SELECT [Names], [City], [Date], [Color], [Shape]
FROM [databasename]
WHERE
(
([Color] IS NULL)
AND
([Shape] IS NULL)
);
根据示例数据 table,我希望结果只包括 Rob Johnson,因为与该名称条目关联的所有行的颜色和形状字段都具有 NULL 值。但是,通过此查询,我也获得了所有其他名称,并返回了 Color 和 Shape 字段中具有 NULL 值的特定行。
因此,预期的输出将如下所示:
Names City Date Color Shape
Rob Johnson Baltimore 8/1/2015
我怀疑我需要在这里使用 GROUP 运算符,但我不太确定该怎么做。有什么想法吗?
我想你想要这个:
SELECT
DISTINCT [Names], [City], [Date], [Color], [Shape]
FROM [table]
WHERE [Names] NOT IN (
SELECT [Names] FROM [table] WHERE ([Color] IS NOT NULL) OR
([Shape] IS NOT NULL)
);
可以通过其他方式完成,但这应该接近您的原始查询。
您可以使用聚合和内部联接:
SELECT d1.* FROM [database-name] d1
INNER JOIN (
select Names,MAX(Color) as mc,MAX(Shape) as ms
from [database-name]
group by Names
) d2
ON d1.Names = d2.Names
WHERE mc IS NULL
AND ms IS NULL