Let's say I have a survey data schema, my fact table represents each person responding this survey, one of the dimension tables has all the answers to the survey, what's the most efficient way to get all the records from the fact table that match multiple conditionals on the answers table?
FACT TABLE (records):
| id | name | date | gender |
DIMENSION TABLE (answers):
| record_id | question_id | value |
I could do a new left join for every question, but this seems very inneficient if I want to find a record that answered multiple questions
SELECT * FROM records r
left join answers a on r.id = a.record_id
left join answers a2 on r.id = a2.record_id
where (a.question_id = 1 and a.value = 2)
and (a2.question_id = 3 and a2.value = 1);
Any alternative to this?
If I understand your question correctly, you need:
SELECT
*
FROM records r
LEFT JOIN answers a ON r.id = a.record_id
AND a.question_id = 1
AND a.value = 2
LEFT JOIN answers a2 ON r.id = a2.record_id
AND a2.question_id = 3
AND a2.value = 1;
After some more digging it looks like the most scalable and dynamic way is using EXISTS
SELECT * FROM records r WHERE
EXISTS (SELECT * FROM answers WHERE record_id = r.id AND question_id = 1 AND value = 1) AND
EXISTS (SELECT * FROM answers WHERE record_id = r.id AND question_id = 3 AND value = 1)
I would hardcode the list of questions/values you want and use join:
with
question_filter as (
select 1 as question_id, 2 as value
union select 3, 1
)
select *
from records r
join answers a
on r.id=a.record_id
join question_filter
using (question_id,value)
;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.