Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created July 27, 2015 10:44
Show Gist options
  • Save linxlad/8bf03fd3f99f6e51a663 to your computer and use it in GitHub Desktop.
Save linxlad/8bf03fd3f99f6e51a663 to your computer and use it in GitHub Desktop.
AVG Time To Sell
SELECT
p.id AS 'PROPERTY ID',
CONCAT_WS(
', ',
CONCAT_WS(
' ',
NULLIF(TRIM(a.building_number), ''),
NULLIF(TRIM(a.building_name), ''),
a.street
),
a.town,
a.county,
a.post_code
) AS 'PROPERTY ID',
DATE_FORMAT(
MIN(status_sold.status_date),
'%d/%m/%Y'
) AS 'ACTIVE PROPERTY DATE',
DATE_FORMAT(
MIN(status_active.status_date),
'%d/%m/%Y'
) AS 'SOLD PROPERTY DATE',
DATEDIFF(
MIN(status_sold.status_date),
MIN(status_active.status_date)
) AS 'DAYS TO SELL'
FROM
properties p
JOIN property_revisions prev ON p.id = prev.property_id
AND prev.flag = 0
JOIN addresses a ON prev.address_id = a.id
JOIN property_status_change_log AS status_sold ON p.id = status_sold.property_id
AND status_sold.`status` IN ("sold_stc")
JOIN property_status_change_log AS status_active ON p.id = status_active.property_id
AND status_active.`status` = "active"
AND (
SELECT
(
DATEDIFF(
MIN(status_sold.status_date),
MIN(status_active.status_date)
)
)
) > 0
GROUP BY
p.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment