PostgreSQL JOIN 单个最新行

问题描述 投票:0回答:2

我们正在运行此查询:

     SELECT concat_ws(' ', z.ticker,
            CASE
              WHEN a.date_approved IS NOT NULL THEN TO_CHAR(a.date_approved,'YYMMDD')
              WHEN z.date_recommended IS NULL THEN '000000'
              ELSE TO_CHAR(z.date_recommended,'YYMMDD')
            END,
             CASE
               WHEN s.ticker IS NOT NULL THEN 6
               WHEN a.approved_recommendation IS NOT NULL THEN approved_recommendation
               ELSE current_recommendation
             END,
             REPLACE(CASE
                       WHEN comments IS NULL THEN 'N/C'
                       ELSE comments
                     END,' ','_'))
     FROM recommendations z
     LEFT JOIN (SELECT ticker FROM scr_tickers) s
            ON z.ticker = s.ticker
     LEFT JOIN (SELECT ticker, date_approved, approved_recommendation
                FROM approved_recommendations) a
            ON z.ticker = a.ticker
     WHERE z.ticker NOT LIKE 'T.%'
       AND z.ticker NOT LIKE 'V.%'
     ORDER BY z.ticker;

现在,他们想在

comments
之后添加另一个数据项,即从
recommendations_history
表中检索最新的推荐值,其中推荐值
<>current_recommendation

为此,我可以执行

join
,将
ROW_NUMBER
ORDER BY
一起使用,然后选择第一行。这是在 PostgreSQL 中实现这一目标的最佳方法吗?

sql postgresql subquery left-join sql-limit
2个回答
0
投票
SELECT concat_ws(' ', z.ticker,
        COALESCE(TO_CHAR(a.date_approved,'YYMMDD'),
                 TO_CHAR(z.date_recommended,'YYMMDD'),
                 '000000'),
         CASE WHEN s.ticker IS NOT NULL THEN 6
              ELSE COALESCE(a.approved_recommendation, current_recommendation)
         END,
         REPLACE(COALESCE(comments,'N/C'),' ','_'),
         rh.recommendation_value)
 FROM recommendations z
 LEFT JOIN (SELECT ticker FROM scr_tickers) s
        USING (ticker)
 LEFT JOIN (SELECT ticker, date_approved, approved_recommendation
            FROM approved_recommendations) a
        USING (ticker)
 LEFT JOIN (SELECT rh.ticker, rh.recommendation_value
            FROM recommendations_history rh
            WHERE z.current_recommendation<>rh.recommendation_value
            LIMIT 1) rh
        USING (ticker)
 WHERE z.ticker NOT LIKE 'T.%'
   AND z.ticker NOT LIKE 'V.%'
 ORDER BY z.ticker;
  1. case when x is null then y else x end
    翻译为
    coalesce(x,y)
  2. 如果输入时获得
  3. to_char(),则
    null
     函数将返回 
    null
  4. join using
    为您保存方程式并合并同名列

-1
投票

尝试一下,我引入了

LEFT JOIN
来合并
recommendations_history
表中与建议表中当前建议不同的最新建议。这是通过加入使用
ROW_NUMBER()
窗口函数的子查询来实现的,该子查询按代码分区并按推荐日期降序排列。

SELECT concat_ws(' ', 
                 z.ticker,
                 CASE
                   WHEN a.date_approved IS NOT NULL THEN TO_CHAR(a.date_approved,'YYMMDD')
                   WHEN z.date_recommended IS NULL THEN '000000'
                   ELSE TO_CHAR(z.date_recommended,'YYMMDD')
                 END,
                 CASE
                   WHEN s.ticker IS NOT NULL THEN 6
                   WHEN a.approved_recommendation IS NOT NULL THEN a.approved_recommendation
                   ELSE z.current_recommendation
                 END,
                 REPLACE(CASE
                             WHEN z.comments IS NULL THEN 'N/C'
                             ELSE z.comments
                           END, ' ', '_'),
                 COALESCE(rh.recommendation, 'No Recent Recommendation')
               )
FROM recommendations z
LEFT JOIN (SELECT ticker FROM scr_tickers) s ON z.ticker = s.ticker
LEFT JOIN (SELECT ticker, date_approved, approved_recommendation
           FROM approved_recommendations) a ON z.ticker = a.ticker
LEFT JOIN (SELECT ticker, recommendation, ROW_NUMBER() OVER (PARTITION BY ticker ORDER BY recommendation_date DESC) AS rn
           FROM recommendations_history
           WHERE recommendation <> (SELECT current_recommendation FROM recommendations WHERE ticker = recommendations_history.ticker)
          ) rh ON z.ticker = rh.ticker AND rh.rn = 1
WHERE z.ticker NOT LIKE 'T.%'
  AND z.ticker NOT LIKE 'V.%'
ORDER BY z.ticker;
© www.soinside.com 2019 - 2024. All rights reserved.