仅在列Element完全为空时才进行检索

时间:2017-01-06 20:03:05

标签: sql-server

我有两张由ID_Person

加入的表格

   ID_Person    DateOfEntry Status
    1           2010             Ok 
    2           2009             Ok 
    3          2008          Ok 
    4          2010          Ok 

logingTable

ID_Log  YearOfcalculation   Element
1   2008    50
1   2009    60
1   2010    70
1   2011    80
1   2012    90
1   2013    100
1   2014    110
2   1982    NULL
2   1983    50
2   1984    60
2   1985    NULL
2   1986    90
2   1987    100
2   1988    NULL
2   1989    110
3   2000    NULL
3   2001    NULL
3   2002    NULL
3   2003    NULL
3   2004    NULL
3   2005    NULL
3   2006    NULL
4   1978    NULL
4   1979    NULL
4   1980    NULL
4   1981    NULL
4   1982    NULL
4   1983    NULL
4   1984    NULL

我想只检索所有ID_Person哪个元素列完全为null(我的意思是没有任何值) ,在这种情况下,只显示带有3和4的ID_Person

select ID_Person, Yearofcalculation,Element
from Person
Inner Join LogingTable 
ON ID_Person=ID_log
where Element is null

但结果并不是我所期望的,因为它包含ID_log = 2 其中一些值不为空

任何人都可以帮忙 非常感谢

4 个答案:

答案 0 :(得分:1)

试试这个

select ID_Person, Yearofcalculation,Element
from Person
Inner Join LogingTable 
ON ID_Person=ID_log
where Element is null
and ID_log not in (select distinct ID_log from LogingTable where Element is not null  )

答案 1 :(得分:1)

试试这个

select ID_Person, Yearofcalculation,Element
from Person
Inner Join LogingTable 
ON ID_Person=ID_log
where ID_Person NOT IN (
    SELECT a.ID_Log 
    FROM
        logingTable a
    GROUP BY a.ID_Log
    HAVING COUNT(1) = COUNT(a.Element)
)

仅限ID版

select DISTINCT ID_Person
from Person
Inner Join LogingTable 
ON ID_Person=ID_log
where ID_Person NOT IN (
    SELECT a.ID_Log 
    FROM
        logingTable a
    GROUP BY a.ID_Log
    HAVING COUNT(1) = COUNT(a.Element)
)

答案 2 :(得分:0)

此解决方案的线索是使用ISNULL功能,因此只需将where条件更改为select ,只有具有NULL的ID 然后才能使用正常通过使用标准进行选择,如下: -

select ID_Person,Yearofcalculation,Element
from Person
Inner Join LogingTable 
ON ID_Person=ID_log
where ID_Log in (
    select ID_Log  
    from logingTable
    group by ID_Log 
    having isnull(sum (Element),0) = 0
)

<强>演示: -

Create Table #Person (ID_Person int  , DateOfEntry int , status varchar(3))
go
insert into #Person values (1,2010,'Ok')
insert into #Person values (2,2009,'Ok')
insert into #Person values(3,2008,'Ok')
insert into #Person values(4,2010,'Ok')
go

Create table #logingTable (ID_Log int ,  YearOfcalculation int ,  Element int)
go

insert into #logingTable values (1,2008, 50)
insert into #logingTable values (1,2009, 60)
insert into #logingTable values (1,2010, 70)
insert into #logingTable values (1,2011, 80)
insert into #logingTable values (1,2012, 90)
insert into #logingTable values (1,2013, 100)
insert into #logingTable values (1,2014, 110)
insert into #logingTable values (2,1982, NULL)
insert into #logingTable values (2,1983, 50)
insert into #logingTable values (2,1984, 60)
insert into #logingTable values (2,1985, NULL)
insert into #logingTable values (2,1986, 90)
insert into #logingTable values (2,1987, 100)
insert into #logingTable values (2,1988, NULL)
insert into #logingTable values (2,1989, 110)
insert into #logingTable values (3,2000, NULL)
insert into #logingTable values (3,2001, NULL)
insert into #logingTable values (3,2002, NULL)
insert into #logingTable values (3,2003, NULL)
insert into #logingTable values (3,2004, NULL)
insert into #logingTable values (3,2005, NULL)
insert into #logingTable values (3,2006, NULL)
insert into #logingTable values (4,1978, NULL)
insert into #logingTable values (4,1979, NULL)
insert into #logingTable values (4,1980, NULL)
insert into #logingTable values (4,1981, NULL)
insert into #logingTable values (4,1982, NULL)
insert into #logingTable values (4,1983, NULL)
insert into #logingTable values (4,1984, NULL)

go

select ID_Person,Yearofcalculation,Element
from #Person
Inner Join #logingTable 
ON ID_Person=ID_log
where ID_Log in (
    select ID_Log  
    from #logingTable
    group by ID_Log 
    having isnull(sum (Element),0) = 0
)

<强>结果: -

enter image description here

答案 3 :(得分:0)

[1]第一个解决方案:

SELECT ID_Person
FROM Person
INNER JOIN LogingTable ON ID_Person=ID_log
GROUP BY ID_Person
HAVING SUM(Element) is null

[2]第二种解决方案:

SELECT ... list of columns ...
FROM LogingTable 
WHERE ID_log IN (
    SELECT ID_Person
    FROM Person
    INNER JOIN LogingTable ON ID_Person=ID_log
    GROUP BY ID_Person
    HAVING SUM(Element) is null
)

[3] Third solution:

SELECT ... list of columns ...
FROM (
    SELECT lt.*, SUM(lt.Element) OVER(PARTITION BY lt.ID_Log) AS SumOfElement
    FROM LogingTable lt
) x
WHERE x.SumOfElement = 0