Choose rows based on two connected column values in one statement - ORACLE

Hawk

First, I'm not sure if the title represent the best of the issue. Any better suggestion is welcomed. My problem is I have the following table:

+----+----------+-------+-----------------+
| ID | SUPPLIER | BUYER | VALIDATION_CODE |
+----+----------+-------+-----------------+
|  1 | A        | Z     |       937886521 |
|  2 | A        | X     |       937886521 |
|  3 | B        | Z     |       145410916 |
|  4 | C        | V     |      775709785  |
+----+----------+-------+-----------------+

I need to show SUPPLIERS A and B which have BUYER Z, X. However, I want this condition to be one-to-one relationship rather than one-to-many. That is, for the supplier A, I want to show the column with ID: 1, 2. For the supplier B, I want to show the column 3 only. The following script will show the supplier A with all possible buyers (which I do not want):

SELECT *   
FROM   validation   
WHERE  supplier IN ( 'A', 'B' )   
       AND buyer IN ( 'X', 'Z');

This will show the following pairs: (A,Z), (A,X), (B, Z). I need to show only the following: (A,X)(B,Z) in one statement.

The desired result should be like this:

+----+----------+-------+-----------------+
| ID | SUPPLIER | BUYER | VALIDATION_CODE |
+----+----------+-------+-----------------+
|  2 | A        | X     |       937886521 |
|  3 | B        | Z     |       145410916 |
+----+----------+-------+-----------------+
tawman

You can update the WHERE clause to filter on the desired pairs:

select *
from sample
where (upper(supplier),upper(buyer))
in (('A','X'),('A','Y'),('A','Z'),('B','X'),('B','Y'),('B','Z'));

I used the UPPER function based on your mixed case examples.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

From Java

'IF' in 'SELECT' statement - choose output value based on column values

From Dev

Oracle - DELETE Rows based on Raw Column values

From Dev

Postgresql: Choose between two similar rows based on a boolean column

From Dev

Dynamically choose one of two columns based on a third column

From Dev

Filter rows in PostgreSQL based on values of consecutive rows in one column

From Dev

Transpose one column to rows based on another column values

From Dev

Oracle SQL grouping one column based on other column values

From Dev

Join up two rows to one in Oracle Sql at a particular column

From Java

Substract column values of two rows based on Dense Rank

From Dev

Removing one of duplicated rows in data frame based on character values in a column

From Dev

Count rows where one column has two values in Hive

From Dev

Combine two rows with just 2 different column values into one

From Dev

how to merge/group records based on difference in two column values in Oracle

From Dev

Increment column values based on two columns in Oracle database

From Dev

Set two values in one table based on sum of a column in another table

From Dev

copying rows based on column values

From Dev

merge rows based on values in a column

From Dev

Combine two rows into One Column

From Dev

Count Two Column Values Based on One Same Column in Two tables in mysql

From Dev

SQL select a column2 based on values in column1 in two rows

From Java

Filter rows based on one column then check if another column's values are in the specific list in Python

From Dev

Excel - calculate average of values in one column based on another grouping column. The number of rows is not constant per group

From Dev

How to keep all rows of one group if two specified values are present in one column in data.frame in R

From Dev

R: Identifying Data Frame Rows Connected By Shared Values In Two Columns

From Dev

Oracle: Is it possible to choose which table to join based on a column value?

From Dev

Combining Two Tables Based on One Column Where Duplicate Values Get Their Own Column

From Dev

Concatenate rows, based on common column Oracle

From Dev

Select a row based on two columns one with ID and another one with specific values in a column

Related Related

  1. 1

    SQL Select distinct rows with duplicate values in one column and choose one row for each duplicate based on value in primary key field

  2. 2

    'IF' in 'SELECT' statement - choose output value based on column values

  3. 3

    Oracle - DELETE Rows based on Raw Column values

  4. 4

    Postgresql: Choose between two similar rows based on a boolean column

  5. 5

    Dynamically choose one of two columns based on a third column

  6. 6

    Filter rows in PostgreSQL based on values of consecutive rows in one column

  7. 7

    Transpose one column to rows based on another column values

  8. 8

    Oracle SQL grouping one column based on other column values

  9. 9

    Join up two rows to one in Oracle Sql at a particular column

  10. 10

    Substract column values of two rows based on Dense Rank

  11. 11

    Removing one of duplicated rows in data frame based on character values in a column

  12. 12

    Count rows where one column has two values in Hive

  13. 13

    Combine two rows with just 2 different column values into one

  14. 14

    how to merge/group records based on difference in two column values in Oracle

  15. 15

    Increment column values based on two columns in Oracle database

  16. 16

    Set two values in one table based on sum of a column in another table

  17. 17

    copying rows based on column values

  18. 18

    merge rows based on values in a column

  19. 19

    Combine two rows into One Column

  20. 20

    Count Two Column Values Based on One Same Column in Two tables in mysql

  21. 21

    SQL select a column2 based on values in column1 in two rows

  22. 22

    Filter rows based on one column then check if another column's values are in the specific list in Python

  23. 23

    Excel - calculate average of values in one column based on another grouping column. The number of rows is not constant per group

  24. 24

    How to keep all rows of one group if two specified values are present in one column in data.frame in R

  25. 25

    R: Identifying Data Frame Rows Connected By Shared Values In Two Columns

  26. 26

    Oracle: Is it possible to choose which table to join based on a column value?

  27. 27

    Combining Two Tables Based on One Column Where Duplicate Values Get Their Own Column

  28. 28

    Concatenate rows, based on common column Oracle

  29. 29

    Select a row based on two columns one with ID and another one with specific values in a column

HotTag

Archive