简体   繁体   English

使用新的别名列作为键联接表

[英]Joining tables using a new alias column as the key

When I try to join 2 tables with my data, I get an error that my reference is ambiguous or I get the order that my original table does not have the alias column. 当我尝试将2个表与数据联接时,出现一个错误,即我的引用不明确,或者得到的顺序是我的原始表没有别名列。

This is among my first projects with sql and an important learning point for me so I appreciate the guidance. 这是我的第一个sql项目,对我来说是一个重要的学习点,因此我感谢指导。

Here is my code. 这是我的代码。

WITH 
RentDotComOnly AS
(
  SELECT 
    concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
    COUNT(clean_zip) AS "rent_count_clean_zip", 
    AVG((low_price+high_price)/2) AS "rent_avg_price", 0.85*min(low_price) AS "rent_lower_bound", 1.15*max(high_price) AS "rent_upper_bound"
  FROM 
    archived_apartments 
  WHERE 
    source_type in (29,36,316) 
    AND week between '2015-07-06' and '2015-10-12' 
    AND is_house <> 1  
    AND archived_apartments.high_price <> 0 
  GROUP BY monthlyzip
),
AllRJData AS
(
  SELECT
    concat(DATEPART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
    COUNT(clean_zip) AS "all_count_clean_zip", 
    AVG((low_price+high_price)/2) AS "all_avg_price"
  FROM 
    archived_apartments 
  WHERE 
    week between '2015-07-06' and '2015-10-12' 
    AND is_house <> 1  
  GROUP BY monthlyzip
)
SELECT 
  concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
  COUNT(archived_apartments.clean_zip) as filtered_count_clean_zip, 
  RentDotComOnly.rent_count_clean_zip, RentDotComOnly.rent_avg_price, RentDotComOnly.rent_lower_bound, RentDotComOnly.rent_upper_bound,
  AllRjData.all_count_clean_zip, AllRjData.all_avg_price
FROM
archived_apartments 
JOIN AllRJData 
ON concat(DATE_PART(mm,archived_apartments.week),archived_apartments.clean_zip) = AllRJData.monthlyzip
JOIN RentDotComOnly
ON concat(DATE_PART(mm,archived_apartments.week),archived_apartments.clean_zip) = RentDotComOnly.monthlyzip
WHERE 
  archived_apartments.week between '2015-07-06' and '2015-10-12'
  AND archived_apartments.is_house <> 1 
  AND archived_apartments.high_price <> 0 
  AND archived_apartments.low_price > RentDotComOnly.rent_lower_bound
  OR archived_apartments.high_price < RentDotComOnly.rent_upper_bound
  AND archived_apartments.week between '2015-07-06' and '2015-10-12'
  AND archived_apartments.is_house <> 1 
  AND archived_apartments.high_price <> 0 
GROUP BY monthlyzip
RentDotComOnly.rent_count_clean_zip, RentDotComOnly.rent_avg_price, RentDotComOnly.rent_lower_bound, RentDotComOnly.rent_upper_bound, 
AllRjData.all_count_clean_zip, AllRjData.all_avg_price
ORDER BY monthlyzip

The problem is that the monthlyzip column you are referring to in your join is ambiguous. 问题是您在monthlyzip中引用的monthlyzip列不明确。 You have three tables with that column, which table are you referring to? 您有带有该列的三个表,您要引用哪个表?

If monthly_zip existed on archived_apartments you could do: 如果archived_apartments存在archived_apartments ,则可以执行以下操作:

FROM
archived_apartments JOIN
AllRJData 
ON archived_apartments.monthlyzip = AllRJData.monthlyzip
JOIN RentDotComOnly 
ON archived_apartments.monthlyzip = RentDotComOnly.monthlyzip

But it sounds like monthly_zip doesn't exist in archived_apartments so you can't actually join on that field. 但是听起来archived_apartments中不存在monthly_zip ,因此您实际上无法加入该字段。 Instead you have to join on the formula you used to make monthlyzip : 相反,您必须加入用于制作monthlyzip的公式:

FROM
archived_apartments JOIN
AllRJData 
ON concat(DATEPART(mm,archived_apartments.week),archived_apartment.clean_zip)  = AllRJData.monthlyzip
JOIN RentDotComOnly 
ON concat(DATEPART(mm,archived_apartments.week),archived_apartment.clean_zip)  = RentDotComOnly.monthlyzip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM