如果满足条件则计数不同
count distinct if a condition is satisfied
我有一个 table 可以显示 address_no 是否有 telephone。为了确定,我正在查看 cell_phone 和 house_phone 列,并且仅当 house_phone 和 cell_phone 为空时才想写 'no phone'。
每个 address_no 都有居住在该地址的人的记录。如果至少有一个人有 phone 或 house_phone 不为空,则 address_no 有 phone,否则 address_no 没有 phone 信息。
ADDRESS_NO------PERSON_ID------CELL_PHONE-------HOUSE_PHONE
11111-----------11-------------111000----------------------
11111-----------12-------------122000----------------------
11111-----------13----------------------------------1313000
22222-----------21----------------------------------2121000
33333-----------31-----------------------------------------
33333-----------32-----------------------------------------
44444-----------41-------------411000---------------4141000
55555-----------51-------------511000----------------------
55555-----------52-----------------------------------------
55555-----------53-----------------------------------------
如上所示,我想得到 5 个地址中有 4 个具有电话phone信息的结果。
我如何编写 sql 查询以在 oracle sql
中查找数字
您可以使用聚合和 case
表达式:
select address_id,
(case when max(cell_phone) is null and max(house_phone) is null
then 'No Phone' else 'Phone'
end) as phone_flag
from t
group by address_id;
如果要列出具有 phone 信息的地址,可以使用 having
子句过滤数据集:
select address_no
from mytable
group by address_no
having coalesce(max(cell_phone), max(house_phone)) is not null
另一方面,如果您想计算具有 phone 信息的地址的总体比率,您可以使用两个级别的聚合:
select avg(case when phone_no is null then 0 else 1 end) ratio
from (
select coalesce(max(cell_phone), max(house_phone)) phone_no
from mytable
group by address_no
) t
我有一个 table 可以显示 address_no 是否有 telephone。为了确定,我正在查看 cell_phone 和 house_phone 列,并且仅当 house_phone 和 cell_phone 为空时才想写 'no phone'。
每个 address_no 都有居住在该地址的人的记录。如果至少有一个人有 phone 或 house_phone 不为空,则 address_no 有 phone,否则 address_no 没有 phone 信息。
ADDRESS_NO------PERSON_ID------CELL_PHONE-------HOUSE_PHONE
11111-----------11-------------111000----------------------
11111-----------12-------------122000----------------------
11111-----------13----------------------------------1313000
22222-----------21----------------------------------2121000
33333-----------31-----------------------------------------
33333-----------32-----------------------------------------
44444-----------41-------------411000---------------4141000
55555-----------51-------------511000----------------------
55555-----------52-----------------------------------------
55555-----------53-----------------------------------------
如上所示,我想得到 5 个地址中有 4 个具有电话phone信息的结果。
我如何编写 sql 查询以在 oracle sql
中查找数字您可以使用聚合和 case
表达式:
select address_id,
(case when max(cell_phone) is null and max(house_phone) is null
then 'No Phone' else 'Phone'
end) as phone_flag
from t
group by address_id;
如果要列出具有 phone 信息的地址,可以使用 having
子句过滤数据集:
select address_no
from mytable
group by address_no
having coalesce(max(cell_phone), max(house_phone)) is not null
另一方面,如果您想计算具有 phone 信息的地址的总体比率,您可以使用两个级别的聚合:
select avg(case when phone_no is null then 0 else 1 end) ratio
from (
select coalesce(max(cell_phone), max(house_phone)) phone_no
from mytable
group by address_no
) t