我有一个如下表。表格中有id
个观测值缺失的值,其他观测值具有实际值。我想得到的是只有id
丢失的地方value
的计数。在这种情况下,它仅为id
4,因此计数为1。如何使用SQL做到这一点?
id | value
---+-------
1 | home
2 | out
3 | home
1 |
2 |
4 |
答案 0 :(得分:2)
您可以进行聚合:
select id
from table t
group by id
having count(value) = 0;
答案 1 :(得分:2)
试试这个-
@RunWith(SpringJUnit4ClassRunner.class)
public class PatientRestControllerTest {
@Resource(name = "PatientRestController")
private PatientRestController patientRestController;
@Resource(name = "PatientService")
private PatientService patientService;
@Test
public void getPatientTest() throws Exception {
}
}
答案 2 :(得分:2)
要获取计数,您可以执行以下操作:
select count(distinct id)
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.value is not null);
或者,您可以使用两种聚合级别:
select count(*)
from (select id
from t
group by id
having count(value) = 0
) x;
或者,如果您的数据库支持except
:
select count(*)
from (select id from t where value is null
except
select id from t where value is not null
) x;
except
删除重复项,因此distinct
是不必要的。