如何将不同的查询结果组合到单个查询结果表中

时间:2014-10-28 06:46:12

标签: sql operation

这是一件事。我正在尝试查找一个可以包含所有3个结果的查询。但我只知道如何对其中一个进行查询。

问题:   对于每个至少有200名成员参与的调查,请提供以下信息:   1)调查ID和调查说明   2)开始调查的成员数量   3)已经完成的成员数量。

查询至少有200次参与的调查ID和调查说明

1)

Select survey_id, Survey_desc, count(Tbl_survey.member_id) as totalnumber
from  Tbl_survey,Tbl_member_participation
where Tbl-survey.member_id = Tbl_member_participation.member_id
group by survey_id, Survey_desc
having totalnumber >= 200

2)查询已启动但尚未完成的成员数。

select count(survey_id)
from  Tbl_survey, 
where survey_id exists ( Select survey_id, Survey_desc, count(Tbl_survey.member_id) as totalnumber
                       from  Tbl_survey,Tbl_member_participation
                       where Tbl-survey.member_id = Tbl_member_participation.member_id
                       group by survey_id, Survey_desc
                       having totalnumber >= 200) and
     finishi_date is **null**   
group by survey_id

3)查询已经完成的成员数量。

select count(survey_id)
from  Tbl_survey, 
where survey_id exists ( Select survey_id, Survey_desc, count(Tbl_survey.member_id) as totalnumber
                       from  Tbl_survey,Tbl_member_participation
                       where Tbl-survey.member_id = Tbl_member_participation.member_id
                       group by survey_id, Survey_desc
                       having totalnumber >= 200) and
     finishi_date is ***not null***   
group by survey_id

所以我只想要一种可以将这三种结果结合起来的方法       调查ID,调查设备,启动人员,完成人员。只在一个查询表中。

3 个答案:

答案 0 :(得分:1)

首先,您应该正确查询。

1)您的第一个查询没问题,但是您不应该使用过时的逗号分隔连接语法。使用显式联接(大约二十年前在SQL标准中引入的,顺便说一下)。

select s.survey_id, s.survey_desc, count(*) as totalnumber
from tbl_survey s
join tbl_member_participation mp on s.member_id = mp.member_id
group by s.survey_id, s.survey_desc
having count(*) >= 200;

2,3)你的第二个和第三个查询在语法上被破坏了。你有点混合EXISTS和IN。此外,finishi_date可能是tbl_member_participation的成员,因此您必须再次加入此表。这是更正的查询2:

select s.survey_id, count(*)
from  tbl_survey s
join tbl_member_participation mp on s.member_id = mp.member_id
where survey_id in
(
  select s.survey_id
  from tbl_survey s
  join tbl_member_participation mp on s.member_id = mp.member_id
  group by s.survey_id
  having count(*) >= 200
)
and mp.finishi_date is null
group by s.survey_id;

为了将所有三个组合在一起,您不必使用EXISTS或IN。所有需要的数据都已在查询1中提供。看,我如何修改查询1以获得更简单的查询2:

select 
  s.survey_id, 
  sum(case when mp.finishi_date is null then 1 else 0 end) as count_unfinished
from tbl_survey s
join tbl_member_participation mp on s.member_id = mp.member_id
group by s.survey_id
having count(*) >= 200;

话虽如此,您的最终查询是:

select 
  s.survey_id, 
  s.survey_desc, 
  sum(case when mp.finishi_date is null then 1 else 0 end) as count_unfinished,
  sum(case when mp.finishi_date is not null then 1 else 0 end) as count_finished,
  count(*) as totalnumber
from tbl_survey s
join tbl_member_participation mp on s.member_id = mp.member_id
group by s.survey_id, s.survey_desc
having count(*) >= 200;

答案 1 :(得分:0)

尝试类似:

select a.*, b.*, c.*
from
(select query 1...) as a,
(select query 2...) as b,
(select query 3...) as c

select ...是您的三个查询。实质上,它运行三个查询并将其结果作为中间表。然后它运行另一个查询 - 最外面的选择 - 它提供自动笛卡尔积的结果。由于您的其他2个查询只生成一行,因此您仍将获得与第一个查询中相同的行数。

您应该在查询2和3中命名输出列。与select count(survey_id) as "COUNT_1" from ...

类似

答案 2 :(得分:0)

你可以使用加入操作

    Select t1.survey_id, t1.Survey_desc,(select count(t1.survey_id)
from  Tbl_survey, 
where t1.survey_id exists (select count(survey_id)
from  Tbl_survey, 
where survey_id exists ( Select survey_id, Survey_desc, count(Tbl_survey.member_id) as totalnumber
                 from  Tbl_survey,Tbl_member_participation
                 where Tbl-survey.member_id = Tbl_member_participation.member_id
                   group by survey_id, Survey_desc
                       having totalnumber >= 200) and
     t1.finishi_date='null'  
group by survey_id),(select count(survey_id)
from  Tbl_survey, 
where survey_id exists ( Select survey_id, Survey_desc, count(Tbl_survey.member_id) as totalnumber
                       from  Tbl_survey,Tbl_member_participation
                       where Tbl-survey.member_id = Tbl_member_participation.member_id
                       group by survey_id, Survey_desc
                       having totalnumber >= 200) and
     t1.finishi_date='null'
group by t1.survey_id
from  Tbl_survey t1, 

left join Tbl_member_participation t2
on t1.member_id = t2.member_id
group by t1.survey_id, t1.Survey_desc

试试这个...如果你可以请发表你的表格然后会得到更好的结果。