0

Below are the two tables I am working with, along with the columns in the tables:

**## Table 1 ##**

Grantee Column w/sample data

Grantee
----------
CLAT

CLATX1

DJSD

DJSDX3

ADKS

ADKSX5

DKHS
**## Table 2 ##**

Inact_ID Column w/sample data

Inact_ID
----------
CLAT

DJSD

ADKS

DKHS

With the following Query:

SELECT DISTINCT A.GRANTEE           
FROM TABLE1 A             
WHERE                      
EXISTS(SELECT 1                     
        FROM TABLE2 B     
        WHERE A.GRANTEE= B.INACT_ID);
These are my results:

CLAT

DJSD

ADKS

DKHS

4 Character ID's Returned - Not everything I want(Also want 6 character ID's associated with the 4 character ID's)

In addition to getting those 4 character ID's, some of those 4 character ID's also have another ID with the following naming convention - A02WXn, where X is a constant, and n is a number from 0-9, which is a 6 character ID. I want to be able to include the 6 character ID, if it exists along with the 4 character ID. The 6 character ID's only exist in Table 1.

UPDATE: I think I might be closer to a solution, hope this can clarify more what I want as results.

SELECT A.GRANTEE FROM TABLE1 A INNER JOIN (SELECT B.INACT_ID FROM INACTIVE_USERS B) ON A.GRANTEE LIKE '%' || B.INACT_ID || '%';

(Note - This was done on Postgres, and worked, but I can't get it to run in DB2)
Results from Postgres Below:(Desired Results - Both 4 character ID and 6 Character ID if exists)

GRANTEE
--------
CLAT

CLATX1

DJSD

DJSDX3

ADKS

ADKSX5

DKHS
6
  • How about ...WHERE LEFT( A.GRANTEE, 4 ) = B.INACT_ID?
    – mustaccio
    Dec 5, 2014 at 18:08
  • Artjom B. - Thanks for the edit. mustaccio - I appreciate the response! When I edit my WHERE CLAUSE to match what you have, I get incorrect results. I am getting ROWS returned that are not just 4 or 6 characters. I am wanting only the 4 character ID's and their associated 6 character ID if it exists. Thanks
    – SkiDoo1222
    Dec 5, 2014 at 18:40
  • Then you need to provide more meaningful sample data, including exceptional cases. My suggestion would work perfectly with your existing sample.
    – mustaccio
    Dec 5, 2014 at 20:06
  • Mustaccio - I updated with more information. I hope this helps.
    – SkiDoo1222
    Dec 5, 2014 at 22:32
  • I am able to get matching first 4 character positions in both tables, but I only want the result set to always include the 4 character ID and its associated 6 character ID if it exists. I want no other values returned other than 4 character(ASDF) or 6 character(ASDFXn ID's) if they exist along with it's associated 4 character ID. Right now I get values no matter how many characters they may be, as long as the first 4 characters match. I feel I am closer, but still can't figure this out. I only need ASDF and ASDFXn ID's. Nothing other than 4 or 6 characters returned. Does this help clarify?
    – SkiDoo1222
    Dec 8, 2014 at 16:08

1 Answer 1

0

Try Below SQL :

SELECT DISTINCT A.GRANTEE           
FROM TABLE1 A             
WHERE                      
EXISTS(SELECT 1                     
        FROM TABLE2 B     
        WHERE substring(A.GRANTEE,1,4)= B.INACT_ID);

I tried same thing in Sybase DB. It worked fine !

create table #temp1
(
GRANTEE varchar(125)
)
insert into #temp1 values('A02W')
insert into #temp1 values('A02WX2')
insert into #temp1 values('DBDS')
insert into #temp1 values('DBDSX4')
insert into #temp1 values('A03')
insert into #temp1 values('ASKDLAKDJD')

create table #temp2
(
INACT_ID varchar(4)
)
insert into #temp2 values('A02W')
insert into #temp2 values('AAAB')
insert into #temp2 values('AVCD')
insert into #temp2 values('SSLD')

select * from  #temp1
select * from  #temp2


SELECT DISTINCT A.GRANTEE           
FROM #temp1 A             
WHERE                      
EXISTS(SELECT 1                     
        FROM #temp2 B     
        WHERE substring(A.GRANTEE,1,4) = B.INACT_ID);

7
  • If I use the Substr(A.GRANTEE,1,4)=B.INACT_ID clause, I get rows returned that are not just 4 and 6 characters like a I am after. I rephrased and added some updates to my original post, if that will help clarify. Sorry for any confusion. I appreciate you input!
    – SkiDoo1222
    Dec 5, 2014 at 22:26
  • Thanks! If this is resolved please mark this as Answer. Dec 5, 2014 at 22:33
  • The query you provided didn't work for what I was trying to retrieve.
    – SkiDoo1222
    Dec 5, 2014 at 22:45
  • I didnt see CLATX2 in your Source Data of table1. How come it came in expected result? Could you please explain ? Becuase i am trying similar data and i am getting below result :CLAT CLATX1 DJSD DJSDX3 ADKS ADKSX5 DKHS Dec 5, 2014 at 22:52
  • Sorry for the confusion. That was a typo. It should be CLATX1. I updated that. WHERE EXISTS(SELECT 1 FROM #temp2 B WHERE substring(A.GRANTEE,1,4) = B.INACT_ID); So the substring function is still qualifying rows that contain the first four characters, as well as any other characters that may be after that? For example, RESULTS - CLAT CLATX1 CLAT4IDKSAKA CLATSHDFSDHF CLATAHAHAHAHAHAJAJAJA
    – SkiDoo1222
    Dec 5, 2014 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.