来自sql query

时间:2016-05-18 10:16:24

标签: sql teradata

我有这个SQL查询:

Select top 10
sub_id1

FROM
(Select top 10
offer_loj_OD.subs_id as sub_id1,
offer_loj_OD.offer_id,
offer_loj_OD.subs_inst_offer_id,
offer_loj_OD.loj_od, offer_loj_OD.loj_do
FROM
(SELECT subs_id, offer_id,  subs_inst_offer_id, CAST(data_od AS DATE) AS loj_od
, CAST(data_do AS DATE) AS loj_do   , src_system_cd 
FROM
(SELECT subs_id,subs_inst_offer_id, ST_DT, end_dt, early_term_dt, offer_id, src_system_cd
,COALESCE(MIN(ST_DT) OVER (PARTITION BY subs_id
, subs_inst_offer_id ORDER BY ST_DT ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)    ,  ST_DT )  AS data_od
, CASE WHEN early_term_dt <=end_dt AND early_term_dt IS NOT NULL THEN early_term_dt ELSE end_dt END AS data_do
FROM vd08_offer.T08010_SUBS_INST_OFFER
) AS umowy
QUALIFY ROW_NUMBER() OVER (PARTITION BY subs_id, subs_inst_offer_id ORDER BY ST_DT DESC) = 1
) AS offer_loj_OD

WHERE 
loj_od between '2015-01-01' and '2015-12-31' 
AND offer_loj_OD.src_system_cd =1 

) as offer_loj_OD_FINAL

left join 


(SELECT top 10
offer_loj_do.subs_id  
, offer_loj_do.offer_id 
, offer_loj_do.subs_inst_offer_id
, offer_loj_do.loj_od, offer_loj_do.loj_do -- datay 

FROM
(SELECT subs_id, offer_id,  subs_inst_offer_id, CAST(data_od AS DATE) AS loj_od
, CAST(data_do AS DATE) AS loj_do   , src_system_cd 
FROM
(SELECT subs_id,subs_inst_offer_id, ST_DT, end_dt, early_term_dt, offer_id, src_system_cd
,COALESCE(MIN(ST_DT) OVER (PARTITION BY subs_id
, subs_inst_offer_id ORDER BY ST_DT ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING)    ,  ST_DT )  AS data_od
, CASE WHEN early_term_dt <=end_dt AND early_term_dt IS NOT NULL THEN early_term_dt ELSE end_dt END AS data_do
FROM vd08_offer.T08010_SUBS_INST_OFFER
) AS umowy
QUALIFY ROW_NUMBER() OVER (PARTITION BY subs_id, subs_inst_offer_id ORDER BY ST_DT DESC) = 1
) AS offer_loj_do

WHERE 
loj_do between '2015-01-01' and '2015-12-31'
AND offer_loj_do.src_system_cd =1 
) as offer_loj_do_final on offer_loj_do.subs_id = offer_loj_OD.subs_id

当我试图运行它时,我收到错误消息:SELECT Failed。 [3807]对象&quot; offer_loj_do&#39;不存在。你能告诉我我在这里做错了什么吗?

也许关于查询的话很少。我需要在2015年选择与客户达成协议的数据(查询的第2部分),但该客户决定签署新协议(查询的第一部分)。

我希望你能帮助我。

1 个答案:

答案 0 :(得分:1)

解析器会抱怨您尝试加入ONoffer_loj_do的最终offer_loj_od,但只有offer_loj_do_finaloffer_loj_od_final属于当前范围。有效的连接条件是

 ON offer_loj_do_final.sub_id1 = offer_loj_od_final.subs_id

但这没有任何意义,你加入两个具有完全相同Select的Derived Tables。

你的格式很糟糕,这就是它通过格式化工具后的样子: - )

SELECT
   TOP 10 sub_id1
FROM
   ( 
      SELECT
         TOP 10 offer_loj_od.subs_id AS sub_id1 
       , offer_loj_od.offer_id 
       , offer_loj_od.subs_inst_offer_id 
       , offer_loj_od.loj_od 
       , offer_loj_od.loj_do
      FROM
         ( 
            SELECT
               subs_id 
             , offer_id 
             , subs_inst_offer_id 
             , CAST(data_od AS DATE) AS loj_od 
             , CAST(data_do AS DATE) AS loj_do 
             , src_system_cd
            FROM
               ( 
                  SELECT
                     subs_id 
                   , subs_inst_offer_id 
                   , st_dt 
                   , end_dt 
                   , early_term_dt 
                   , offer_id 
                   , src_system_cd 
                   , COALESCE(MIN(st_dt) OVER (partition BY subs_id , subs_inst_offer_id order by st_dt ROWS BETWEEN 1 preceding AND 1 preceding) , st_dt ) AS data_od 
                   , CASE
                        WHEN early_term_dt            <=end_dt
                           AND early_term_dt is not null
                        THEN early_term_dt
                        ELSE end_dt
                     END AS data_do
                  FROM
                     vd08_offer.t08010_subs_inst_offer
               ) AS umowy 
            QUALIFY ROW_NUMBER() OVER (partition BY subs_id, subs_inst_offer_id order by st_dt DESC) = 1
         ) AS offer_loj_od
      WHERE loj_od BETWEEN '2015-01-01' AND '2015-12-31'
         AND offer_loj_od.src_system_cd =1
   ) AS offer_loj_od_final
LEFT JOIN
   ( 
      SELECT
         TOP 10 offer_loj_do.subs_id 
       , offer_loj_do.offer_id 
       , offer_loj_do.subs_inst_offer_id 
       , offer_loj_do.loj_od 
       , offer_loj_do.loj_do -- datay
      FROM
         ( 
            SELECT
               subs_id 
             , offer_id 
             , subs_inst_offer_id 
             , CAST(data_od AS DATE) AS loj_od 
             , CAST(data_do AS DATE) AS loj_do 
             , src_system_cd
            FROM
               ( 
                  SELECT
                     subs_id 
                   , subs_inst_offer_id 
                   , st_dt 
                   , end_dt 
                   , early_term_dt 
                   , offer_id 
                   , src_system_cd 
                   , COALESCE(MIN(st_dt) OVER (partition BY subs_id , subs_inst_offer_id order by st_dt ROWS BETWEEN 1 preceding AND 1 preceding) , st_dt ) AS data_od 
                   , CASE
                        WHEN early_term_dt            <=end_dt
                           AND early_term_dt is not null
                        THEN early_term_dt
                        ELSE end_dt
                     END AS data_do
                  FROM
                     vd08_offer.t08010_subs_inst_offer
               ) AS umowy 
            QUALIFY ROW_NUMBER() OVER (partition BY subs_id, subs_inst_offer_id order by st_dt DESC) = 1
         ) AS offer_loj_do
      WHERE loj_do BETWEEN '2015-01-01' AND '2015-12-31'
         AND offer_loj_do.src_system_cd =1
   ) AS offer_loj_do_final 
ON offer_loj_do.subs_id = offer_loj_od.subs_id