希望這是你在aksing什麼:
select
user_id,
sum
(case when uuid1 is not null and uuid2 is not null then 1 else 0 end) both_uuid_avlbl,
sum
(case when uuid1 is not null then 1 else 0 end) uuid1_avlbl,
sum
(case when uuid2 is not null then 1 else 0 end) uuid2_avlbl
from sample group by user_id;
下面是DDL腳本我用上面的查詢:
create table sample as (user_id number(10),uuid1 number(10),uuid2 number(10));
Insert into sample (USER_ID,UUID1,UUID2) values (1,2,2);
Insert into sample (USER_ID,UUID1,UUID2) values (2,3,2);
Insert into sample (USER_ID,UUID1,UUID2) values (3,1,1);
Insert into sample (USER_ID,UUID1,UUID2) values (4,1,2);
Insert into sample (USER_ID,UUID1,UUID2) values (5,2,0);
Insert into sample (USER_ID,UUID1,UUID2) values (8,null,2);
Insert into sample (USER_ID,UUID1,UUID2) values (7,null,null);
Insert into sample (USER_ID,UUID1,UUID2) values (6,null,2);
Insert into sample (USER_ID,UUID1,UUID2) values (3,null,1);
Insert into sample (USER_ID,UUID1,UUID2) values (1,3,2);
Insert into sample (USER_ID,UUID1,UUID2) values (1,null,2);
Insert into sample (USER_ID,UUID1,UUID2) values (5,3,0);
Insert into sample (USER_ID,UUID1,UUID2) values (5,3,null);
insert into sample (user_id,uuid1,uuid2) values (5,null,null);
我用的Oracle 11g。
添加一些樣本表數據,和預期的結果。 – jarlh
樣本數據請參閱 – theDbGuy
另請參見:您正在使用哪些DBMS? –