如何在同一结果表中包含来自两个查询的信息?

时间:2014-12-01 20:21:13

标签: mysql sql subquery

我有以下疑问:

select year, avg(value) as winter_avg_Miami from AP_DATA where substring(series_id, 8) ='killowatts' and  substring(series_id, 3,5) = 'Miami' and period IN ('M10','M11','M12') group by year;
select year, avg(value) as winter_avg_notMiami from AP_DATA where substring(series_id, 8) = 'killowatts' and  substring(series_id, 3,5) != 'Miami' and period IN ('M10','M11','M12') group by year;

我想做的不是获得两个不同的表,一个像:

year       winter_avg_Miami
2000             28.1
2001             30.2

和另一个表格如下:

year        winter_avg_notMiami
2000             40.1
2001             50.2

我想在一个表中包含所有信息,例如:

year       winter_avg_Miami  winter_avg_notMiami
2000             28.1              40.1 
2001             30.2              50.2

如何编写查询以完成此操作?

5 个答案:

答案 0 :(得分:1)

您可以在case汇总中使用avg语句:

select year, 
   avg(case when substring(series_id, 3,5) = 'Miami' then value end) as winter_avg_Miami,
   avg(case when substring(series_id, 3,5) != 'Miami' then value end) as winter_avg_notMiami 
from AP_DATA 
where substring(series_id, 8) ='killowatts'
    and period IN ('M10','M11','M12') 
group by year

答案 1 :(得分:0)

select
      year,
      case when CntMiami = 0 then 0 else TotMiami / CntMiami end ) as AvgMiami,
      case when CntNotMiami = 0 then 0 else TotNotMiami / CntNotMiamt end ) as AvgNotMiami
from
   (
select 
      year, 
      sum( case when substring(series_id, 3,5) = 'Miami' then value else 0 end ) as TotMiami,
      sum( case when substring(series_id, 3,5) = 'Miami' then 1 else 0 end ) as CntMiami,
      sum( case when substring(series_id, 3,5) != 'Miami' then value else 0 end ) as TotNotMiami,
      sum( case when substring(series_id, 3,5) != 'Miami' then 1 else 0 end ) as CntNotMiami
   from 
      AP_DATA 
   where 
          substring(series_id, 8) ='killowatts' 
      and period IN ('M10','M11','M12') 
   group by 
      year )

答案 2 :(得分:0)

如果您的查询是正确的,这应该有效:

SELECT a.year, a.winter_avg_Miami, b.winter_avg_notMiami
From (select year, avg(value) as winter_avg_Miami 
     from AP_DATA where substring(series_id, 8) ='killowatts' and  
         substring(series_id, 3,5) = 'Miami' and period IN ('M10','M11','M12') 
     group by year)a, 
    (select year, avg(value) as winter_avg_notMiami 
    from AP_DATA where substring(series_id, 8) = 'killowatts' and  
    substring(series_id, 3,5) != 'Miami' and period IN ('M10','M11','M12') 
    group by year) b
WHERE a.year = b.year;

答案 3 :(得分:0)

UNIONS是一个简单的替代选择,如果有某种原因你可以采用像sgeddes建议的更传统的方式之一。

答案 4 :(得分:0)

请尝试以下解决方案:

解决方案:1

select ap1.year, avg(ap1.value) as winter_avg_Miami,avg(ap2.value) as winter_avg_notMiami
from AP_DATA ap1 left join ap2.AP_DATA on ap1.year=ap2.year
where substring(ap1.series_id, 8) ='killowatts' and  substring(ap1.series_id, 3,5) = 'Miami' and ap1.period IN ('M10','M11','M12') 
substring(ap2.series_id, 8) = 'killowatts' and  substring(ap2.series_id, 3,5) != 'Miami'and ap2.period IN ('M10','M11','M12')
group by ap1.year;

解决方案:2

--create temp table
create table temp1
(year int, winter_avg_Miami int null,winter_avg_notMiami int null)

--insert into temp table
insert into temp1
(year int, winter_avg_Miami ,winter_avg_notMiami)
(
select year, avg(value) as winter_avg_Miami,null as winter_avg_notMiami from AP_DATA where substring(series_id, 8) ='killowatts' and  substring(series_id, 3,5) = 'Miami' and period IN ('M10','M11','M12') group by year
union all
select year, null as winter_avg_Miami, avg(value) as winter_avg_notMiami from AP_DATA where substring(series_id, 8) = 'killowatts' and  substring(series_id, 3,5) != 'Miami' and period IN ('M10','M11','M12') group by year
)

--result in single table
select year,avg(winter_avg_Miami) as winter_avg_Miami,avg(winter_avg_notMiami) as winter_avg_notMiami
from temp1
group by year

我在sybase中尝试了上述解决方案。它工作得很好。

希望这会有所帮助:)