Skip to content

Instantly share code, notes, and snippets.

@grahamu
Created December 20, 2018 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamu/6a80154246dedc03b6b0e084d372991e to your computer and use it in GitHub Desktop.
Save grahamu/6a80154246dedc03b6b0e084d372991e to your computer and use it in GitHub Desktop.
SELECT DISTINCT "realestate_db_building"."id",
"realestate_db_building"."public_notes",
"realestate_db_building"."public_notes_changed",
"realestate_db_building"."private_notes",
"realestate_db_building"."private_notes_changed",
"realestate_db_building"."name",
"realestate_db_building"."displayed",
"realestate_db_building"."website",
"realestate_db_building"."kind",
"realestate_db_building"."access",
"realestate_db_building"."access_details",
"realestate_db_building"."street",
"realestate_db_building"."city",
"realestate_db_building"."state",
"realestate_db_building"."lat",
"realestate_db_building"."lon",
"realestate_db_building"."contact_id",
"realestate_db_building"."boosted",
"realestate_db_building"."address_id",
SUM(CASE
WHEN "realestate_db_suite"."available_type" = %s THEN %s
ELSE %s
END) AS "displayed_suite_count",
(ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) AS "is_in_geojson_bounds",
COUNT("realestate_db_buildingphoto"."id") AS "photo_count",
(SELECT V0."available_changed"
FROM "realestate_db_suite" V0
LEFT OUTER JOIN "taggit_taggeditem" V2 ON (V0."id" = V2."object_id"
AND (V2."content_type_id" = %s))
WHERE (V0."building_id" IN
(SELECT U0."id" AS Col1
FROM "realestate_db_building" U0
LEFT OUTER JOIN "realestate_db_suite" U1 ON (U0."id" = U1."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" U2 ON (U0."id" = U2."building_id")
WHERE (U0."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s)
GROUP BY U0."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326)))
HAVING (SUM(CASE
WHEN U1."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT(U2."id") >= %s))
AND NOT (V0."available_type" = %s)
AND V0."rsf" BETWEEN %s AND %s
AND V2."tag_id" IN
(SELECT U0."id" AS Col1
FROM "taggit_tag" U0
WHERE U0."slug" IN (%s))
AND V0."building_id" = ("realestate_db_building"."id"))
ORDER BY V0."available_changed" DESC
LIMIT 1) AS "latest_suite_availability"
FROM "realestate_db_building"
LEFT OUTER JOIN "realestate_db_suite" ON ("realestate_db_building"."id" = "realestate_db_suite"."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" ON ("realestate_db_building"."id" = "realestate_db_buildingphoto"."building_id")
WHERE ("realestate_db_building"."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s
AND "realestate_db_building"."id" IN
(SELECT DISTINCT V0."building_id" AS Col1
FROM "realestate_db_suite" V0
LEFT OUTER JOIN "taggit_taggeditem" V2 ON (V0."id" = V2."object_id"
AND (V2."content_type_id" = %s))
WHERE (V0."building_id" IN
(SELECT U0."id" AS Col1
FROM "realestate_db_building" U0
LEFT OUTER JOIN "realestate_db_suite" U1 ON (U0."id" = U1."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" U2 ON (U0."id" = U2."building_id")
WHERE (U0."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s)
GROUP BY U0."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326)))
HAVING (SUM(CASE
WHEN U1."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT(U2."id") >= %s))
AND NOT (V0."available_type" = %s)
AND V0."rsf" BETWEEN %s AND %s
AND V2."tag_id" IN
(SELECT U0."id" AS Col1
FROM "taggit_tag" U0
WHERE U0."slug" IN (%s))))
AND
(SELECT V0."available_changed"
FROM "realestate_db_suite" V0
LEFT OUTER JOIN "taggit_taggeditem" V2 ON (V0."id" = V2."object_id"
AND (V2."content_type_id" = %s))
WHERE (V0."building_id" IN
(SELECT U0."id" AS Col1
FROM "realestate_db_building" U0
LEFT OUTER JOIN "realestate_db_suite" U1 ON (U0."id" = U1."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" U2 ON (U0."id" = U2."building_id")
WHERE (U0."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s)
GROUP BY U0."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326)))
HAVING (SUM(CASE
WHEN U1."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT(U2."id") >= %s))
AND NOT (V0."available_type" = %s)
AND V0."rsf" BETWEEN %s AND %s
AND V2."tag_id" IN
(SELECT U0."id" AS Col1
FROM "taggit_tag" U0
WHERE U0."slug" IN (%s))
AND V0."building_id" = ("realestate_db_building"."id"))
ORDER BY V0."available_changed" DESC
LIMIT 1) >= %s)
GROUP BY "realestate_db_building"."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))),
(SELECT V0."available_changed"
FROM "realestate_db_suite" V0
LEFT OUTER JOIN "taggit_taggeditem" V2 ON (V0."id" = V2."object_id"
AND (V2."content_type_id" = %s))
WHERE (V0."building_id" IN
(SELECT U0."id" AS Col1
FROM "realestate_db_building" U0
LEFT OUTER JOIN "realestate_db_suite" U1 ON (U0."id" = U1."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" U2 ON (U0."id" = U2."building_id")
WHERE (U0."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s)
GROUP BY U0."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326)))
HAVING (SUM(CASE
WHEN U1."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT(U2."id") >= %s))
AND NOT (V0."available_type" = %s)
AND V0."rsf" BETWEEN %s AND %s
AND V2."tag_id" IN
(SELECT U0."id" AS Col1
FROM "taggit_tag" U0
WHERE U0."slug" IN (%s))
AND V0."building_id" = ("realestate_db_building"."id"))
ORDER BY V0."available_changed" DESC
LIMIT 1)
HAVING (SUM(CASE
WHEN "realestate_db_suite"."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT("realestate_db_buildingphoto"."id") >= %s)
ORDER BY
(SELECT V0."available_changed"
FROM "realestate_db_suite" V0
LEFT OUTER JOIN "taggit_taggeditem" V2 ON (V0."id" = V2."object_id"
AND (V2."content_type_id" = %s))
WHERE (V0."building_id" IN
(SELECT U0."id" AS Col1
FROM "realestate_db_building" U0
LEFT OUTER JOIN "realestate_db_suite" U1 ON (U0."id" = U1."building_id")
LEFT OUTER JOIN "realestate_db_buildingphoto" U2 ON (U0."id" = U2."building_id")
WHERE (U0."displayed" = %s
AND (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326))) = %s)
GROUP BY U0."id", (ST_Contains(ST_GeomFromEWKT(%s), ST_SetSRID(ST_Point("lon", "lat"), 4326)))
HAVING (SUM(CASE
WHEN U1."available_type" = %s THEN %s
ELSE %s
END) >= %s
AND COUNT(U2."id") >= %s))
AND NOT (V0."available_type" = %s)
AND V0."rsf" BETWEEN %s AND %s
AND V2."tag_id" IN
(SELECT U0."id" AS Col1
FROM "taggit_tag" U0
WHERE U0."slug" IN (%s))
AND V0."building_id" = ("realestate_db_building"."id"))
ORDER BY V0."available_changed" DESC
LIMIT 1) DESC NULLS LAST
@grahamu
Copy link
Author

grahamu commented Dec 20, 2018

Parameters?

(u'no', 0, 1, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', 51, True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', u'no', 0, 1, 1, 1, u'no', 2500, 3500, u'traditional', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, 51, True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', u'no', 0, 1, 1, 1, u'no', 2500, 3500, u'traditional', 51, True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', u'no', 0, 1, 1, 1, u'no', 2500, 3500, u'traditional', datetime.datetime(2018, 9, 20, 19, 41, 39, 613576, tzinfo=<UTC>), u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', 51, True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', u'no', 0, 1, 1, 1, u'no', 2500, 3500, u'traditional', u'no', 0, 1, 1, 1, 51, True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', True, u'SRID=4326;MULTIPOLYGON (((-96.81702464818954 32.8163179425074, -96.81082874536514 32.77174143379595, -96.7876772582531 32.78116354354958, -96.7920345067978 32.7859824610326, -96.79210759699345 32.81096302069943, -96.78060829639433 32.83251712351861, -96.80696703493595 32.82450355259063, -96.81702464818954 32.8163179425074)))', u'no', 0, 1, 1, 1, u'no', 2500, 3500, u'traditional')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment