After many years lurking stackoverflow, I finally have the need to come to you dev gurus in search for a little bit of help.
Context:
I'm a developer that is playing with information that I extract from SAP. I'm extracting the info and I'm creating two tables in memory using H2 database.
My tables look like this:
CREATE TABLE USERS
(
SID VARCHAR(255),
SYSID VARCHAR(5),
MANDT VARCHAR(3),
BNAME VARCHAR(255),
GLTGV DATE,
GLTGB DATE,
USTYP VARCHAR(2),
LOCNT VARCHAR(3),
UFLAG VARCHAR(3),
TRDAT DATE,
LTIME VARCHAR(255),
CLASS VARCHAR(255),
PWDCHGDATE DATE,
PROFILE VARCHAR(255)
)
CREATE TABLE ROLES
(
SID VARCHAR(255),
SYSID VARCHAR(5),
MANDT VARCHAR(3),
UNAME VARCHAR(255),
AGR_NAME VARCHAR(255)
)
Do not worry about the definitions, I'm just "dummyfing" them for the sake of simplicity.
One user can have none or many roles. How do you tie them together?
Well SYSID
, MANDT
and BNAME
from table USERS
have to match the content of the ROLES
table, columns SYSID
, MANDT
and UNAME
(all of the same type)
So I have created this query
SELECT DISTINCT
t1.SID,
t1.SYSID,
t1.MANDT,
t1.BNAME,
t1.GLTGV,
t1.GLTGB,
t1.USTYP,
t1.LOCNT,
t1.UFLAG,
t1.TRDAT,
t1.LTIME,
t1.CLASS,
t1.PWDCHGDATE,
t1.PROFILE
FROM
USERS AS t1,
ROLES AS t2
WHERE
t1.SYSID = t2.SYSID AND
t1.MANDT = t2.MANDT AND
t1.BNAME = t2.UNAME AND
(t2.AGR_NAME = "ZTEST_ROLE")
which works well as long as I only query by one ROLE.
To query for more than one role, I do
SELECT DISTINCT
t1.SID,
t1.SYSID,
t1.MANDT,
t1.BNAME,
t1.GLTGV,
t1.GLTGB,
t1.USTYP,
t1.LOCNT,
t1.UFLAG,
t1.TRDAT,
t1.LTIME,
t1.CLASS,
t1.PWDCHGDATE,
t1.PROFILE
FROM
USERS AS t1,
ROLES AS t2
WHERE
t1.SYSID = t2.SYSID AND
t1.MANDT = t2.MANDT AND
t1.BNAME = t2.UNAME AND
(t2.AGR_NAME = "ZTEST_ROLE" OR t2.AGR_NAME = "ZTEST_ROLE2")
It also works. You would use this query if you want to get info from all the users that either have ZTEST_ROLE or ZTEST_ROLE2 as roles.
Now, here is my problem, I cannot make it to work if I want to select all the users that have both roles ZTEST_ROLE and ZTEST_ROLE2. my query always return 0 rows
Here is the query that I'm using
SELECT DISTINCT
t1.SID,
t1.SYSID,
t1.MANDT,
t1.BNAME,
t1.GLTGV,
t1.GLTGB,
t1.USTYP,
t1.LOCNT,
t1.UFLAG,
t1.TRDAT,
t1.LTIME,
t1.CLASS,
t1.PWDCHGDATE,
t1.PROFILE
FROM
USERS AS t1,
ROLES AS t2
WHERE
t1.SYSID = t2.SYSID AND
t1.MANDT = t2.MANDT AND
t1.BNAME = t2.UNAME AND
(t2.AGR_NAME = "ZTEST_ROLE" AND t2.AGR_NAME = "ZTEST_ROLE")
I have been playing with this for a while and I do not see where my error is
For convenience and in case you want to try this online, I have been using https://sqliteonline.com/
I appreciate your help and my apologies for the long post