0

当前的 SELECT 查询

SELECT a.*, ROUND(((a.avg-b.avg)/b.avg)*100,2) pct_change
FROM
  ( 
     SELECT x.*, COUNT(*) rank
     FROM blavg x 
         JOIN blavg y 
             ON y.bline_id = x.bline_id 
            AND y.id >= x.id 
     GROUP BY x.id
) a
JOIN
( 
     SELECT x.*, COUNT(*) rank
     FROM blavg x 
         JOIN blavg y 
             ON y.bline_id = x.bline_id 
            AND y.id >= x.id 
     GROUP BY x.id
 ) b
     ON b.bline_id = a.bline_id
     AND b.rank = a.rank+1
WHERE a.rank = 1;

我也想

SELECT bl_id, bline FROM bl
bl.bl_id = blavg.bline_id

我试过了

SELECT bl_id, bline 
FROM bl bl 
    LEFT JOIN ON bl.bl_id = blavg.bline_id

但不确定我是否有正确的语法,我不确定在哪里添加ON以及括号 () 去了哪里。

任何帮助将不胜感激。

4

1 回答 1

0

因此,您正在执行某种查询以两次两次检索行并进行一些数学运算。
幸运的是,您的查询已经返回了bline_id您需要链接到您的bl表的信息。

因此,您只需要在结果上再增加一个关节即可检索bl.bline您需要的字段:

SELECT bl.bline, a.*, ROUND(((a.avg-b.avg)/b.avg)*100,2) pct_change
FROM
  ( 
     SELECT x.*, COUNT(*) rank
     FROM blavg x 
         JOIN blavg y 
             ON y.bline_id = x.bline_id 
            AND y.id >= x.id 
     GROUP BY x.id
) a
JOIN
( 
     SELECT x.*, COUNT(*) rank
     FROM blavg x 
         JOIN blavg y 
             ON y.bline_id = x.bline_id 
            AND y.id >= x.id 
     GROUP BY x.id
 ) b
     ON b.bline_id = a.bline_id
     AND b.rank = a.rank+1
     LEFT OUTER JOIN bl
         ON bl.bl_id = a.bline_id
WHERE a.rank = 1;
于 2013-04-03T12:38:27.007 回答