I created a large database of all my prices and all my competitor prices with date and location information.
I want to narrow my database to only the "true" competitors on a location and price basis because we charge different prices in different locations. For example, I just want the count of competitors that charge $1 below or above me.
My current code stalls and does not yield results. I think it is because of my implementation of JOIN ON.
To debug, I seperated it out and got results for my first two tables no problem. Exactly what I was going for. With the third table "TrueComps", no such luck.
It's complex as a result of joining 3 tables. I am new to SQL and am thus interested in learning new solutions. I believe there is a better solution than this:
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
Original code:
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)
My full code:
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
monthlyzipor something?TrueCompsas well.