我創建了包含日期和位置信息的所有價格和所有競爭對手價格的大型數據庫。邏輯故障以便計算參數內的結果
我想根據地點和價格將我的數據庫縮小到只有「真正」的競爭對手,因爲我們在不同的地點收取不同的價格。例如,我只希望競爭對手的收費低於或高於我1美元。
我當前的代碼停滯不前,並且不會產生結果。我認爲這是因爲我實現了JOIN ON。
爲了調試,我把它分開,並得到了我的前兩個表的結果沒有問題。到底是爲了什麼。與第三個表「TrueComps」,沒有這樣的運氣。
由於連接了3個表格,這很複雜。我是SQL新手,因此對學習新的解決方案感興趣。我相信這是一個更好的解決辦法莫過於:
WITH
RentDotComOnly AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) 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 = '2015-07-06'
AND is_house <> 1
AND archived_apartments.high_price <> 0
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip
),
AllRJData AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) AS "all_count_clean_zip"
--, AVG((low_price+high_price)/2) AS "all_avg_price"
FROM
archived_apartments
WHERE
week = '2015-07-06'
AND is_house <> 1
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip
),
TrueComps AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) AS "true_comps"
FROM
archived_apartments, RentDotComOnly
WHERE
week = '2015-07-06'
AND is_house <> 1
AND archived_apartments.high_price <> 0
AND low_price > 10000
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip
)
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
TrueComps.true_comps AS "TrueComps"
FROM
archived_apartments, TrueComps
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip, truecomps.true_comps
ORDER BY monthlyzip
原始代碼:
AND (low_price > RentDotComOnly.rent_lower_bound and low_price < RentDotComOnly.rent_upper_bound) or (high_price < RentDotComOnly.rent_upper_bound and high_price > RentDotComOnly.rent_lower_bound)
我全碼:
WITH
RentDotComOnly AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) 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 = '2015-07-06'
AND is_house <> 1
AND archived_apartments.high_price <> 0
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip
),
AllRJData AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) 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, archived_apartments.week, archived_apartments.clean_zip
),
TrueComps AS
(
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
COUNT(distinct apt_unique_id) AS "true_comps"
FROM
archived_apartments, RentDotComOnly
WHERE
week between '2015-07-06' and '2015-10-12'
AND is_house <> 1
AND archived_apartments.high_price <> 0
AND (low_price > RentDotComOnly.rent_lower_bound and low_price < RentDotComOnly.rent_upper_bound) or (high_price < RentDotComOnly.rent_upper_bound and high_price > RentDotComOnly.rent_lower_bound)
GROUP BY monthlyzip, archived_apartments.week, archived_apartments.clean_zip
)
SELECT
distinct concat(DATE_PART(mm,archived_apartments.week),clean_zip) AS "monthlyzip",
RentDotComOnly.rent_count_clean_zip AS "RentOnly",
AllRJData.all_count_clean_zip AS "Total",
TrueComps.true_comps AS "TrueComps"
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
JOIN TrueComps
ON concat(DATE_PART(mm,archived_apartments.week),archived_apartments.clean_zip) = TrueComps.monthlyzip
GROUP BY AllRJData.monthlyzip, archived_apartments.week, archived_apartments.clean_zip, rentdotcomonly.rent_count_clean_zip, allrjdata.all_count_clean_zip, truecomps.true_comps
ORDER BY AllRJData.monthlyzip
儘管我的建議似乎沒有解決這個問題,我可以幾乎仍然保證在括號出租限制條件不正確。你是否也應該匹配'monthlyzip'上的行? – shawnt00
是的,我的努力是在月刊上匹配JOIN ON –
我認爲您還需要在'TrueComps'內部執行此操作。 – shawnt00