BrickTracker/bricktracker/sql/set/base/select.sql

53 lines
1.4 KiB
MySQL
Raw Normal View History

2025-01-17 11:03:00 +01:00
SELECT
"sets"."set_num",
"sets"."name",
"sets"."year",
"sets"."theme_id",
"sets"."num_parts",
"sets"."set_img_url",
"sets"."set_url",
"sets"."last_modified_dt",
"sets"."mini_col",
"sets"."set_check",
"sets"."set_col",
"sets"."u_id",
2025-01-17 11:03:00 +01:00
{% block number %}
CAST(SUBSTR("sets"."set_num", 1, INSTR("sets"."set_num", '-') - 1) AS INTEGER) AS "set_number",
CAST(SUBSTR("sets"."set_num", 1, INSTR("sets"."set_num", '-') + 1) AS INTEGER) AS "set_version",
2025-01-17 11:03:00 +01:00
{% endblock %}
IFNULL("missing_join"."total", 0) AS "total_missing",
IFNULL("minifigures_join"."total", 0) AS "total_minifigures"
2025-01-17 11:03:00 +01:00
FROM sets
-- LEFT JOIN + SELECT to avoid messing the total
LEFT JOIN (
SELECT
"missing"."u_id",
SUM("missing"."quantity") AS "total"
FROM "missing"
2025-01-17 11:03:00 +01:00
{% block where_missing %}{% endblock %}
GROUP BY "u_id"
) "missing_join"
ON "sets"."u_id" IS NOT DISTINCT FROM "missing_join"."u_id"
2025-01-17 11:03:00 +01:00
-- LEFT JOIN + SELECT to avoid messing the total
LEFT JOIN (
SELECT
"minifigures"."u_id",
SUM("minifigures"."quantity") AS "total"
FROM "minifigures"
2025-01-17 11:03:00 +01:00
{% block where_minifigures %}{% endblock %}
GROUP BY "u_id"
) "minifigures_join"
ON "sets"."u_id" IS NOT DISTINCT FROM "minifigures_join"."u_id"
2025-01-17 11:03:00 +01:00
{% block where %}{% endblock %}
{% if order %}
ORDER BY {{ order }}
{% endif %}
{% if limit %}
LIMIT {{ limit }}
{% endif %}