在Postgres中加入两个子查询

问题描述

尝试在两个子查询上运行内部联接,但我收到错误消息:

org.postgresql.util.PsqlException: ERROR: Syntax error at or near "JOIN"
  Position: 550
  GROUP BY year 
JOIN temp ON temp.year = MN.ye
^
  -- INNER JOIN (

这是我的查询

   WITH temp as(
      SELECT 
        SUM(CASE WHEN rain = 'TRUE' THEN 1 END)*1.0/COUNT(date) * 100 as rain,EXTRACT(YEAR FROM date) as year 
        FROM sample
      GROUP BY year
      )
    SELECT AVG(mind) as avg_min,AVG(maxd) as avg_max,EXTRACT(YEAR FROM date) as year
      FROM sample MN
      GROUP BY year 
    JOIN temp ON temp.year = MN.year

和我的数据样本

date    prcp    maxd    mind    rain
1948-01-01 00:00:00 0.47    51  42  TRUE
1948-01-02 00:00:00 0.59    45  36  TRUE
1948-01-03 00:00:00 0.42    45  35  TRUE
1948-01-04 00:00:00 0.31    45  34  TRUE
1948-01-05 00:00:00 0.17    45  32  TRUE
1948-01-06 00:00:00 0.44    48  39  TRUE
1948-01-07 00:00:00 0.41    50  40  TRUE
1948-01-08 00:00:00 0.04    48  35  TRUE
1948-01-09 00:00:00 0.12    50  31  TRUE
1948-01-10 00:00:00 0.74    43  34  TRUE
1948-01-11 00:00:00 0.01    42  32  TRUE
1948-01-12 00:00:00 0   41  26  FALSE
1948-01-13 00:00:00 0   45  29  FALSE
1948-01-14 00:00:00 0   38  26  FALSE
1948-01-15 00:00:00 0   34  31  FALSE
1948-01-16 00:00:00 0   34  28  FALSE
1948-01-17 00:00:00 0   35  29  FALSE
1948-01-18 00:00:00 0   33  28  FALSE
1948-01-19 00:00:00 0   34  27  FALSE
1948-01-20 00:00:00 0   36  29  FALSE
1948-01-21 00:00:00 0   48  32  FALSE
1948-01-22 00:00:00 0.21    47  44  TRUE
1948-01-23 00:00:00 0   47  43  FALSE
1948-01-24 00:00:00 0.1 45  34  TRUE
1948-01-25 00:00:00 0   46  30  FALSE
1948-01-26 00:00:00 0   45  32  FALSE
1948-01-27 00:00:00 0   53  33  FALSE
1948-01-28 00:00:00 0   53  25  FALSE
1948-01-29 00:00:00 0.22    42  34  TRUE
1948-01-30 00:00:00 0.03    47  30  TRUE
1948-01-31 00:00:00 0.21    35  27  TRUE

我理想的结果将是类似于此的

avg_tmin,avg_tmax,avg_rain,year
 x          x         x       1948
 x          x         x       1949
...  

所以我的数据集中每年的平均头脑(tmin),maxd(tmax)和降雨

解决方法

我不了解您的查询尝试实现的逻辑。但是,从样本数据和预期结果来看,您似乎只想聚合:

select
    avg(mind) avg_mind,avg(maxd) avg_maxd,avg( (rain)::int ) avg_rain,extract(year from date) year
from sample
group by extract(year from date)
,

我不认为需要JOIN开头:

SELECT count(*) filter (where rain = 'TRUE') * 1.0  / count(*) as rain,AVG(mind) as avg_min,AVG(maxd) as avg_max,EXTRACT(YEAR FROM date) as year 
FROM sample
GROUP BY year

以上是完成所需工作的最有效方法。

但是,要回答您的直接问题,为什么代码不起作用:您需要在联接后移动组,并且不能在同一级别上使用列别名year({{1 }})的定义位置:

mn

请注意,这不使用CTE中的WITH temp as ( SELECT count(*) filter (where rain = 'TRUE') *1.0 / COUNT(date) * 100 as rain,EXTRACT(YEAR FROM date) as year FROM sample GROUP BY year ),SELECT AVG(mn.mind) as avg_min,AVG(mn.maxd) as avg_max,tmp.year FROM sample MN JOIN temp ON temp.year = EXTRACT(YEAR FROM mn.date) GROUP BY tmp.year 列。如果要添加,则需要通过以下方式将其添加到组中:

rain

或将其拆分为两个合并的聚合查询。

WITH temp as (
  SELECT count(*) filter (where rain = 'TRUE') *1.0 / COUNT(date) * 100 as rain,tmp.year,tmp.rain
FROM sample MN
  JOIN temp ON temp.year = EXTRACT(YEAR FROM mn.date)
GROUP BY tmp.year,tmp.rain

但再次:不需要联接,这会使整个过程效率降低。