1

I am trying to run the below query. for a reason i need to join different tables which includes the results from all tables.

Let's say the query is :

Select a.value1, b.value2, c.value3 
from a 
join b on a.some_value = b.some_value1 
join c on c.Some_other_value = b.some_diff_value 
where (my conditions)...

Let's say the result of the above query is :

value1 | value2 | value3  
-------+--------+--------
1      | val1   | val2
1      | some1  | some2
2      | othr1  | othr2
3      | diff2  | diff3

I want to remove the values from the above result set which value1 is not 1.

Expected output:

value1 | value2 | value3 
-------+--------+--------
1      | val1   | val2
1      | some1  | some2

Is there any way in postgresql that I can achieve this? Searched for different threads but no clue in any other posts. Any suggestions are appreciated.

Note: I cannot use where a.value1 = 1 as the logic in the SQL query will result in losing some records for doing math. So the whole idea is to run the SQL query as is but to remove the records after the Select operation is done and just have the result with value1 = 1.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ging
  • 117
  • 8
  • 1
    Read re subqueries. PS This is obviously a duplicate, but I didn't easily find one. Before considering posting please always google any error message and/or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. [ask] That usually involves many synonyms for overloaded words, like remove or filter instead of delete, or query result instead of resultset. PS Please put stuff like what follows "So the whole idea is ..." at the beginning. It could be the whole post. – philipxy Mar 28 '19 at 03:37
  • Possible duplicate of [Performing a query on a result from another query?](https://stackoverflow.com/questions/949465/performing-a-query-on-a-result-from-another-query) – philipxy Mar 28 '19 at 03:55

1 Answers1

0

use subquery and then filter out a.value1=1 in where clause

select * from
( 
Select a.value1,b.value2,c.value3 
        from a join b on a.some_value=b.some_value1 
        join c on c.Some_othr_value=b.some_diff_value 
    where your conditions
)AA where a.value1=1
Fahmi
  • 37,315
  • 5
  • 22
  • 31
  • Nope. Sorry if i am not clear in the question. I cannot use a.value1=1 here as it will eliminate some logic in the joins. @fa06 – ging Mar 28 '19 at 01:56
  • 1
    @Perfect. Will accept the answer as soon as i can. Can accept this after 6min. Thanks for the quick response. – ging Mar 28 '19 at 02:00