Querying for multiple values in multiple columns

57 Views Asked by At

How would I search multiple (in this example, say 2) columns for an array of values?

For example, here is my table Documents :

1. DocNumber (varchar)
2. CompanyCode (varchar)
3. Data1 (varchar)
4. Data2 (varchar)
5. Data3 (varchar)

DocNumber and CompanyCode form the composite primary key of this table. Say I have a set of values which I want to search in the database such as:

DocNumber CompanyCode
1001 101
1002 102
1004 103

How would I find these unique combinations in the table with one query?

I could use in:

select *
from Documents
where DocNumber in :docNumbers and CompanyCode in :companyCodes

But that would also return records with DocNumber 1001 and CompanyCode 102 (all combinations of the 2 lists). I want to avoid that.

I am using HANA DB (through a Spring Boot application).

Sample table data:
table data

Expected response:
expected response

Response I get:
current response

1

There are 1 best solutions below

0
Mathias Kemeter On

The IN predicate also works with tuples. Please find a minimal example:

SELECT * 
FROM 
(
    SELECT 'A1' AS DOCNUMBER, 'A2' AS COMPANYCODE FROM DUMMY
)
WHERE (DOCNUMBER, COMPANYCODE) IN (('A1', 'A2'),('B1', 'B2'))

Further details and examples can be found in the documentation.