2015-10-29 51 views
0

我創建了包含日期和位置信息的所有價格和所有競爭對手價格的大型數據庫。邏輯故障以便計算參數內的結果

我想根據地點和價格將我的數據庫縮小到只有「真正」的競爭對手,因爲我們在不同的地點收取不同的價格。例如,我只希望競爭對手的收費低於或高於我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 
+0

儘管我的建議似乎沒有解決這個問題,我可以幾乎仍然保證在括號出租限制條件不正確。你是否也應該匹配'monthlyzip'上的行? – shawnt00

+0

是的,我的努力是在月刊上匹配JOIN ON –

+0

我認爲您還需要在'TrueComps'內部執行此操作。 – shawnt00

回答

0

我想你可能有括號錯誤的最後一部分的WHERE條款。我不知道你想實現的東西邏輯,但我的猜測來解決它:

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 
) 

正如你已經得到它寫的or「d條件不與他人勾結,並隨時單獨哪些也可能會導致你看到的放緩。

另一種猜測是,您正在尋找價格範圍的重疊。難道你真的想要這個?:

AND (
      low_price <= RentDotComOnly.rent_upper_bound 
     and high_price >= RentDotComOnly.rent_lower_bound 
) 
+0

它仍然超時對我:「[亞馬遜](500310)操作無效:磁盤已滿」 –

+0

我用'AND(low_price> 3000和low_price <4000或high_price <4000和high_price> 3000'替換了您的線路,發現也許我的JOIN邏輯關閉了,因爲它仍然停滯不前。 –

+0

然後,它可能與'archived_apartments'和'RentDotComOnly'的叉積有關。我真的不理解這兩個表/表達式之間的關係或者 – shawnt00

1

嘗試增加在TrueComps連接條件:

FROM 
    archived_apartments INNER JOIN RentDotComOnly 
     ON concat(DATE_PART(mm,archived_apartments.week),archived_apartments.clean_zip) = 
      RentDotComOnly.monthlyzip 
+0

仍然沒有運氣sql很難 –

+0

好吧所以我現在得到了結果,但有些事情出錯了,因爲我每月每月的記錄數超過5行,而不是僅僅爲每月的一個月刊提供一次truecomps。有什麼想法嗎? –

+0

那麼你的分組是在三列,我幾乎寫道,加入所有三個(每月,每週,clean_zip)。我不知道這些是什麼意思,所以我仍然只是猜測。 – shawnt00