A
A
Alexey Nikolaev2018-08-05 21:59:03
SQL
Alexey Nikolaev, 2018-08-05 21:59:03

How to select a value based on data from multiple rows?

Good evening.
I have a table with the following structure:

ID  NAME    VALUE
1    foo      1      +
1    bar      2      +
2    foo      3
2    bar      1

I need to select ID value by VALUE column. In this case, you need to take into account VALUE for rows with the value of the NAME column, both foo and bar (marked with plus signs above). At the same time, if I write something like
WHERE VALUE = 1 AND VALUE = 2
nothing, of course, it won’t work, because. verification occurs within a single line. If I replace AND with OR, then in the resulting set I will also get ID 2, because the VALUE column contains non-unique values. I tried a lot of things besides this, but none of the options with subqueries and joins worked - I just can’t figure out how to take into account the previous ones with each subsequent check.
How to be in this situation?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question