多重内心关注

时间:2018-03-19 20:23:10

标签: mysql sql join inner-join teradata-sql-assistant

我试图以月度水平获取数据。

SELECT 
    c.Calendar_Month_Name, COUNT(*)
FROM
   db1 AS c
        INNER JOIN
    (SELECT DISTINCT
        a.tel_num, b.postpaid_tel_num
    FROM
        db2 AS a
    INNER JOIN db3 AS b ON a.tel_num = b.tel_num
    WHERE
        a.hs_manufacturer = 'Samsung'
            AND b.postpaid_tel_num = 1) d ON c.Dim_Calendar_Dt = d.REPORT_DT
WHERE
    c.Calendar_Year_Num = 2018
GROUP BY c.Calendar_Month_Name;

REP2_DT存在于db2中但仍然出现错误,表明REPORT_DT不存在

如果我按照以下方式更改了副作用的位置,我会收到一条错误消息,说明在' REPORT_DT'和'其中'关键词。

SELECT 
    c.Calendar_Month_Name, COUNT(*)
FROM
    (db1 AS c
    INNER JOIN (SELECT DISTINCT
        a.tel_num, b.postpaid_tel_num
    FROM
        db2 AS a
    INNER JOIN db3 AS b ON a.tel_num = b.tel_num
    WHERE
        a.hs_manufacturer = 'Samsung'
            AND b.postpaid_tel_num = 1) d ON c.Dim_Calendar_Dt = d.REPORT_DT
WHERE
    c.Calendar_Year_Num = 2018)
GROUP BY c.Calendar_Month_Name;

2 个答案:

答案 0 :(得分:1)

在第一个版本中,您似乎需要将 REPORT_DT 添加到子查询的 select 子句中 d

答案 1 :(得分:0)

FWIW,我认为格式化的查询应该如下所示:

SELECT c.Calendar_Month_Name
     , COUNT(*) 
  FROM db1 c
  JOIN 
     ( SELECT DISTINCT a.tel_num
            , b.postpaid_tel_num 
         FROM db2 a
         JOIN db3 b 
           ON a.tel_num = b.tel_num
        WHERE a.hs_manufacturer = 'Samsung'         
          AND b.postpaid_tel_num=1
     ) d
    ON c.Dim_Calendar_Dt = d.REPORT_DT
 WHERE c.Calendar_Year_Num = 2018
 GROUP 
    BY c.Calendar_Month_Name