在SQL中使用嵌套连接表时出错

时间:2016-04-21 07:01:54

标签: mysql

我的查询看起来像这样

SELECT level1_idnt, day_dt, avg(sls_price) as avg_rp
FROM (SELECT * FROM (SELECT DISTINCT level1_idnt,day_dt,sls_price 
                     FROM (SELECT a.level1_idnt,a.day_dt,a.sls_price,b.op
                           FROM (SELECT level1_idnt,day_dt,sls_price 
                           FROM md1.loc_sku_dy_act_pos_full_v2 
                           WHERE seasn_cd =2 and day_dt >= '2015-03-01 00:00:00' and day_dt <= '2015-09-30 00:00:00'
                                )l0 
                           a left join (SELECT level1_idnt, AVG(org_sales_price) as op
                                        FROM (SELECT DISTINCT level1_idnt, org_sales_price
                                        FROM md1.item_lv1_org_price_m
                                        WHERE fr_cntry_cde = '01' AND org_sales_price IS NOT NULL
                                        ) l
                                        GROUP BY level1_idnt)l_op
                           b 
                           ON a.level1_idnt = b.level1_idnt
                           )l1
            WHERE sls_price <= op
      )l2
      WHERE sls_price is not null
     ) l3
GROUP BY level1_idnt,day_dt;

为此我收到错误

syntax error at or near "a"

对此的任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

检查一下;)它在子查询中看起来像alias错误。

SELECT level1_idnt, day_dt, avg(sls_price) as avg_rp
FROM (SELECT * FROM (SELECT DISTINCT level1_idnt,day_dt,sls_price
                     FROM (SELECT a.level1_idnt,a.day_dt,a.sls_price,b.op
                           FROM (SELECT level1_idnt,day_dt,sls_price
                                 FROM md1.loc_sku_dy_act_pos_full_v2
                                 WHERE seasn_cd =2 and day_dt >= '2015-03-01 00:00:00' and day_dt <= '2015-09-30 00:00:00'
                                )
                          a left join (SELECT level1_idnt, AVG(org_sales_price) as op
                                       FROM (SELECT DISTINCT level1_idnt, org_sales_price
                                             FROM md1.item_lv1_org_price_m
                                             WHERE fr_cntry_cde = '01' AND org_sales_price IS NOT NULL
                                            ) l
                                       GROUP BY level1_idnt)
                    b
ON a.level1_idnt = b.level1_idnt
)l1
WHERE sls_price <= op
)l2
WHERE sls_price is not null
) l3
GROUP BY level1_idnt,day_dt;

a之前有一个字符串 l0 ,在b之前有一个字符串 l_op

答案 1 :(得分:1)

看起来你正试图给同一个子查询提供两个名字:

(SELECT level1_idnt,day_dt,sls_price 
 FROM md1.loc_sku_dy_act_pos_full_v2 
 WHERE seasn_cd =2 and day_dt >= '2015-03-01 00:00:00' 
 and day_dt <= '2015-09-30 00:00:00')l0 a 

或调用子查询&#34; l0&#34;或&#34; a&#34;

和&#34; b&#34;:

的情况相同
(SELECT level1_idnt, AVG(org_sales_price) as op
 FROM (SELECT DISTINCT level1_idnt, org_sales_price
       FROM md1.item_lv1_org_price_m
       WHERE fr_cntry_cde = '01' AND org_sales_price IS NOT NULL) l
 GROUP BY level1_idnt) l_op b 

或者您调用子查询&#34; l_op&#34;或&#34; b&#34;

从我所看到的,我认为你可以取代&#34; a&#34;用&#34; l0&#34;和&#34; b&#34;用&#34; l_op&#34;在这一行

SELECT a.level1_idnt,a.day_dt,a.sls_price,b.op

并删除名称&#34; a&#34;和&#34; b&#34;来自子查询