I'm guessing my question was asked before, but after hours of scouring, either I'm not able to figure out how the answers match my dilemma or it truly hasn't been asked (I'm guessing the former). So, I'm going to ask again, in my own way, so that I may better understand the answer.
The goal is to grab data for a project based on criteria that may be provided to me. There are three different types of criteria: C, M, and P. I usually receive two of the three different types of criteria for a project, and it's not always the same two. I need to be able to restrict a particular type of criteria only if they provide to me any of that type of criteria.
Here's the general layout of my query that I'm starting with (assuming project_id = 1 for this example):
WITH
cte_c AS (SELECT c FROM prj_rqst_c_criteria WHERE project_id = 1)
, cte_m AS (SELECT m FROM prj_rqst_m_criteria WHERE project_id = 1)
, cte_p AS (SELECT p FROM prj_rqst_p_criteria WHERE project_id = 1)
SELECT * FROM data_table
INNER JOIN cte_c ON data_table.c = cte_c.c
INNER JOIN cte_m ON data_table.m = cte_m.m
INNER JOIN cte_p ON data_table.p = cte_p.p
;
Assuming I have rows returned in cte_c and cte_p but no rows returned in cte_m, this would obviously yield 0 results for the entire query. What I want in that scenario is for cte_m to effectively be ignored while applying the JOINs for cte_c and cte_p. How do I modify the JOINs so that if any cte returns no rows, that particular cte will be ignored?
Thanks!
Edit: Some additional items: data_table has well over 1 million rows, so the goal is to only return rows that match the criteria that was provided for the project.
Some example data:
data_table
id | c | m | p
------------------
1 | A | 101 | 999
2 | B | 102 | 998
3 | A | 103 | 998
4 | A | 102 | 999
5 | B | 101 | 998
If I'm asked to grab a project where c = 'A' and p = '999' but I'm not given any criteria for m, then I'd want the following rwos returned:
id | c | m | p
------------------
1 | A | 101 | 999
4 | A | 102 | 999
If I'm asked to pull a project where m = 102 and c = 'A', then I just want the following returned:
id | c | m | p
------------------
4 | A | 102 | 999
I hope this helps to visualize. Thanks again!
Left jointhat way if there is no data in M, you'll still get rows from C & P? Also your join's need to be on cte_c, cte_m, not c,m,p. Sample data and expected results would be useful to ensure we fully understand the question.OR (SELECT COUNT(*) FROM CTE_x) = 0to each of the inner join ON conditions.