Sql 检查另一列中的所有值(NULL或NOTNULL)并返回唯一记录

Sql 检查另一列中的所有值(NULL或NOTNULL)并返回唯一记录,sql,Sql,如您所见,对于cid=1,所有日期字段均为NULL;对于cid=3,所有日期字段均不为NULL 我需要使用新字段获取唯一的CID,如果所有日期都为NULL,则使用“NULL”,如果所有日期都不为NULL,则使用“notnull” 您可以使用聚合和case: select cid, (case when count(datecol) = 0 then 'NULL' when count(datecol) = count(*) then 'NOT NULL'

如您所见,对于cid=1,所有日期字段均为NULL;对于cid=3,所有日期字段均不为NULL

我需要使用新字段获取唯一的CID,如果所有日期都为NULL,则使用“NULL”,如果所有日期都不为NULL,则使用“notnull”


您可以使用聚合和
case

select cid,
       (case when count(datecol) = 0 then 'NULL'
             when count(datecol) = count(*) then 'NOT NULL'
        end) as newField
from t
group by cid
having count(datecol) in (0, count(*));

您可以使用聚合和
case

select cid,
       (case when count(datecol) = 0 then 'NULL'
             when count(datecol) = count(*) then 'NOT NULL'
        end) as newField
from t
group by cid
having count(datecol) in (0, count(*));

请考虑在格式化文本中添加示例表数据和预期结果。也请告诉我们您当前的查询尝试使用哪一个数据库。我使用PASGRESQL.请考虑在格式化文本中添加示例表数据和预期结果。同时向我们展示您当前的查询尝试您正在使用哪个数据库。我使用PostgreSql非常感谢
select cid, case when min(c) = 0 AND max(c) = 0 then 'null' when min(c) = 1 and max(c) = 1 then 'not null' end from (
    select cid, case when dt is null then 0 else 1 end as c from your_table
) t
group by cid
having min(c) = max(c)