【问题标题】:Joining two subqueries in Postgres在 Postgres 中加入两个子查询
【发布时间】:2020-08-28 14:45:32
【问题描述】:

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

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)和雨

【问题讨论】:

  • GROUP BY 在 JOIN 之后。
  • 请向我们展示您想要的样本数据结果。
  • @jarlh 因为我试图在第二个子查询中找到平均值,如果我在加入后运行 groupby,那么我无法在第二个子查询中执行聚合并得到列 mn.year 的错误消息不存在
  • @GMB 我已经添加了我想要的结果
  • 我看不到任何第二个子查询

标签: sql postgresql datetime subquery average


【解决方案1】:

我不明白您的查询试图实现的逻辑。但是,从您的示例数据和预期结果来看,您似乎只需要聚合:

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)

【讨论】:

    【解决方案2】:

    我认为没有必要以 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
    

    以上是做你想做的最有效的方法。

    但是,要回答您的直接问题,为什么您的代码不起作用:您需要在加入后将组移动,并且您不能在同一级别 (mn) 上使用列别名 year,其中你定义了它:

    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 
    

    请注意,这不使用 CTE 中的 rain 列。如果您想添加它,您需要将其包含在组中:

    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, 
           tmp.rain
    FROM sample MN
      JOIN temp ON temp.year = EXTRACT(YEAR FROM mn.date)
    GROUP BY tmp.year, tmp.rain
    

    或者将其拆分为两个连接的聚合查询。

    WITH temp1 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
    ), temp2 as (
      SELECT AVG(mind) as avg_min,
             AVG(maxd) as avg_max,
             EXTRACT(YEAR FROM date) as year 
      FROM sample MN
      GROUP BY year 
    )
    select *
    from temp1
      join temp2 using (year);  
    

    但同样:不需要加入,这会降低整个事情的效率。

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      相关资源
      最近更新 更多