概念性问题:我们如何使用SQL提取在多个条件下的特定值?

概念性问题:我们如何使用SQL提取在多个条件下的特定值?,sql,r,Sql,R,我试图理解如何从两个以上的表中得出具体属于多个类别的值的百分比。例如,在一个样本场景中,只有少数感染流感的成年人死亡。你如何找到成人流感患者中死亡的百分比和未死亡的百分比 下面共粘贴了三张表格供您参考。第一个表格列出了一组人,显示了该人的一般信息,如姓名、年龄、收入等。第二个表格列出了一组人,显示了各种人的疾病,如流感。最后一张表列出了死亡人数及其死因 如何使用SQL派生所需的输出?还可以使用R函数的组合(例如合并函数),因为SQL代码将在R环境中运行 样本表1 Name Age Gen

我试图理解如何从两个以上的表中得出具体属于多个类别的值的百分比。例如,在一个样本场景中,只有少数感染流感的成年人死亡。你如何找到成人流感患者中死亡的百分比和未死亡的百分比

下面共粘贴了三张表格供您参考。第一个表格列出了一组人,显示了该人的一般信息,如姓名、年龄、收入等。第二个表格列出了一组人,显示了各种人的疾病,如流感。最后一张表列出了死亡人数及其死因

如何使用SQL派生所需的输出?还可以使用R函数的组合(例如合并函数),因为SQL代码将在R环境中运行

样本表1

Name   Age   Gender
Andrew 25    Male
Lisa   21    Female
Conor  35    Male
John   51    Male
Linda  29    Female
样本表2

Name    Illness
Andrew  Flu
Conor   Flu
Lisa    Flu
John    Smallpox
样本表3

Name    Cause of Death
Andrew  Flu
期望输出

Fatality(Death from Flu)   Percentage of people with flu
Yes                        1/3
No                         2/3
在SQL中(仅限)

假设您使用left(用于包含表之间不匹配的行)加入表,并在

select  t1.Name, t1.Age, t1.Gender, t2.Illness
 , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
from table1 t1
INNER join table2 t2 on t1.name  = t2.name 
  AND t2.Illness ='Flu'
left join table3 t3 on t1.name  = t3.name 
您可以使用

select count(*) from  (
  select  t1.Name, t1.Age, t1.Gender, t2.Illness
   , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
  from table1 t1
  INNER join table2 t2 on t1.name  = t2.name 
    AND t2.Illness ='Flu'
  left join table3 t3 on t1.name  = t3.name 
) tt1
“是”和“不使用”的数字

select Fatality_death_from_flu,  count(*) from  (
  select  t1.Name, t1.Age, t1.Gender, t2.Illness
   , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
  from table1 t1
  INNER join table2 t2 on t1.name  = t2.name 
    AND t2.Illness ='Flu'
  left join table3 t3 on t1.name  = t3.name 
) tt2 
组合查询,您可以使用

  select r1.Fatality_death_from_flu, r1.count_fatality/r22.count_tot
  from  (
      select Fatality_death_from_flu,  count(*)  count_fatality from  (
        select  t1.Name, t1.Age, t1.Gender, t2.Illness
         , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
        from table1 t1
        INNER join table2 t2 on t1.name  = t2.name 
          AND t2.Illness ='Flu'
        left join table3 t3 on t1.name  = t3.name 
      ) tt2 
  ) r1
  CROSS JOIN  (
    select count(*)  count_tot 
    from  (
        select  t1.Name, t1.Age, t1.Gender, t2.Illness
         , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
        from table1 t1
        INNER join table2 t2 on t1.name  = t2.name 
          AND t2.Illness ='Flu'
        left join table3 t3 on t1.name  = t3.name 
      ) tt1

  ) r2
但是使用一些中间查询,您可以轻松地在SQL(仅限SQL)中使用R获得所需的结果

假设您使用left(用于包含表之间不匹配的行)加入表,并在

select  t1.Name, t1.Age, t1.Gender, t2.Illness
 , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
from table1 t1
INNER join table2 t2 on t1.name  = t2.name 
  AND t2.Illness ='Flu'
left join table3 t3 on t1.name  = t3.name 
您可以使用

select count(*) from  (
  select  t1.Name, t1.Age, t1.Gender, t2.Illness
   , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
  from table1 t1
  INNER join table2 t2 on t1.name  = t2.name 
    AND t2.Illness ='Flu'
  left join table3 t3 on t1.name  = t3.name 
) tt1
“是”和“不使用”的数字

select Fatality_death_from_flu,  count(*) from  (
  select  t1.Name, t1.Age, t1.Gender, t2.Illness
   , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
  from table1 t1
  INNER join table2 t2 on t1.name  = t2.name 
    AND t2.Illness ='Flu'
  left join table3 t3 on t1.name  = t3.name 
) tt2 
组合查询,您可以使用

  select r1.Fatality_death_from_flu, r1.count_fatality/r22.count_tot
  from  (
      select Fatality_death_from_flu,  count(*)  count_fatality from  (
        select  t1.Name, t1.Age, t1.Gender, t2.Illness
         , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
        from table1 t1
        INNER join table2 t2 on t1.name  = t2.name 
          AND t2.Illness ='Flu'
        left join table3 t3 on t1.name  = t3.name 
      ) tt2 
  ) r1
  CROSS JOIN  (
    select count(*)  count_tot 
    from  (
        select  t1.Name, t1.Age, t1.Gender, t2.Illness
         , case when t3.cause_of_death ='Flu' then 'Yes' else 'Not' END Fatality_death_from_flu
        from table1 t1
        INNER join table2 t2 on t1.name  = t2.name 
          AND t2.Illness ='Flu'
        left join table3 t3 on t1.name  = t3.name 
      ) tt1

  ) r2

但是,使用一些中间查询,您可以轻松地使用R进行obatin,所需的结果

表1似乎没有必要

将其放入列中似乎比放入行中更合理:

select count(*) as num_people,
       sum(case when t3 is null then 1 else 0 end) as not_died,
       sum(case when t3 is not null then 1 else 0 end) as died,
       avg(case when t3 is null then 1.0 else 0 end) as not_died_ratio,
       avg(case when t3 is not null then 1.0 else 0 end) as died_ratio
from table2 t2 left join
     table3 t3
     on t3.name = t2.name and
        t3.illness = 'Flu'
where t2.cause_of_death = 'Flu'

表1似乎没有必要

将其放入列中似乎比放入行中更合理:

select count(*) as num_people,
       sum(case when t3 is null then 1 else 0 end) as not_died,
       sum(case when t3 is not null then 1 else 0 end) as died,
       avg(case when t3 is null then 1.0 else 0 end) as not_died_ratio,
       avg(case when t3 is not null then 1.0 else 0 end) as died_ratio
from table2 t2 left join
     table3 t3
     on t3.name = t2.name and
        t3.illness = 'Flu'
where t2.cause_of_death = 'Flu'

请注意,你可能不止一次得流感。他们是怎么处理的?请你能不止一次地感染流感。他们是如何处理的?