Sql server 一种搜索SQL以仅提取只有空值的数据行的方法
这是我的疑问:Sql server 一种搜索SQL以仅提取只有空值的数据行的方法,sql-server,optimization,null,case,row,Sql Server,Optimization,Null,Case,Row,这是我的疑问: SELECT patientname, patientID, (CASE WHEN patientID is not null THEN 'yes' WHEN CpatientID is null then 'No' END) as submittedintohospital FROM dbo].[database](nolock) and patientname = "John Smith" 这是数据
SELECT patientname, patientID,
(CASE WHEN patientID is not null THEN 'yes'
WHEN CpatientID is null then 'No'
END) as submittedintohospital
FROM dbo].[database](nolock)
and patientname = "John Smith"
这是数据结果:
PatientName PatientID
John Smith 12345488999 NULL
John Smith 12880889976 NULL
如您所见,一个患者有两行结果,我想设置一个条件,其中只有所有行
的patientID为空,则它将进入“是”的案例
因为我编写的当前查询将显示具有空值和非空值的患者数据:
例如:
John Wick 1895
John Wick NULL
我想消除这一点,只提取所有行都必须有null patientID的患者名称,而不仅仅是一行
我想设置一个条件,其中只有所有行的patientID为null,然后它将变成yes的case
您可以使用窗口功能:
select patientname, patientid,
case when max(patientid) over(partition by patientname) is null
then 'yes'
else 'no'
end as submittedintohospital
from [dbo].[database]
如果要实际筛选结果,请执行以下操作:
select patientname, patientid
from (
select d.*,
max(patientid) over(partition by patientname) max_patientid
from [dbo].[database] d
) d
where max_patientid is null
不存在
也可用于筛选:
select d.*
from [dbo].[database] d
where not exists (
select 1
from [dbo].[database] d1
where d1.patientname = d.patientname and d1.patientid is not null
)
站住。嘿,我正在加入另一个数据库,现在有一些语法错误,你能看一下吗?@GBM嘿,cna你看我发布的新问题吗?