proc sql function to find mulitple LIKE matches? - sql

I'm having trouble with a LIKE function in proc sql.
PROC SQL;
CREATE TABLE NAMES_IDS AS
SELECT DISTINCT
T1.*
,T2.NAMES
,T2.NAME_ID
FROM WORK.table1 T1
LEFT JOIN data.table2 T2 ON T2.NAMES like T1.NAMES1
;QUIT;
I have several names in t2, lets say for example theres John 1, John 2, John 3, John 4, etc and in t1.Names1 there is %John%
proc sql is just pulling in the first match, John 1 and its associated ID, and applying it to all the data in T1, instead of duplicated a match for all matching names (this is what I want to achieve).
So the end table would have something like
COLUMN A COLUMN B
John John 1
John John 2
John John 3
John John 4
But instead, what I get is:
COLUMN A COLUMN B
John John 1
John John 1
John John 1
John John 1
Hopefully this makes some sort of sense...

I think I figured it out, I added TRIM to my code and I guess there may have been some erroneous spaces somewhere because that seems to fix my issue. Thanks for your responses!

Related

Postgreql. How to select unique values and count it?

I have column in postgresql
Names
Mike
Alex
Mike
Bill, Abigail
Abigail
Bill
Kurt, Adele, John
Mike
John
, is a delimiter when values two or more in the field.
How to to select it as result
Abigail 2
Adele 1
Alex 1
Bill 2
John 2
Kurt 1
Mike 3
I read about distinct and join but I can't make query.
You need to split the values and then count:
select u.name, count(*)
from t cross join lateral
unnest(string_to_array(names, ', ')) u(name)
group by u.name;
Here is a db<>fiddle.
Then you should fix your data model. Do not store multiple values in a string column. Postgres supports arrays which is one option. Another is a proper junction/association table.

SQL Join from 2 Tables with Null Values

I have 2 tables and want to pull the results back from them into one.
Now the Name field is a unique ID with multiple data attached to it, i.e. the dates and the times. I've simplified the data somewhat to post here but this is the general gist.
Table 1
Name Date
John 12th
John 13th
John 15th
John 17th
Table 2
Name Colour
John Red
John Blue
John Orange
John Green
Result Needed
Name Date Time
John 12th NULL
John 13th NULL
John 15th NULL
John 17th NULL
John NULL Red
John NULL Blue
John NULL Orange
John NULL Green
I'm currently performing a Left join to pull the data however it is posting the results next to each other like
John 12th Red
You want union all:
select name, date, null as colour
from t1
union all
select name, null, colour
from t2;
I took the liberty of naming the second column colour rather than time, simply because that makes more sense in the context of the question.

Join record between table only once in MS Access

I have tbl1 Structured like this
Name Type
====== =====
John 1
David 1
Jane 2
William 3
Alex 2
Ryan 1
And tbl2 structured like this
Index Type Job
1 1 Clean
2 1 Wash
3 2 Carry
4 2 Package
5 3 Sell
I would like to join record with matched Type, but each record in tbl1 only join once with one record in tbl2
Ie:
If
John is joined with Clean then David must be joined with Wash. Or if John is joined with Wash then David must be joined with Clean.
Doesn't matter if David is joined with Wash or Clean , I only need them to be joined with record that match the criteria and be joined ONCE.
I will make sure for each Type in 'tbl1' there will be equivalent amount of record in 'tbl2'
I mainly work on MS Access so Query on this environment would be the best~ Thank you all for reading.
Best regards
Try below query.
select name, (select TOP(1) job from tbl2 where tbl1.type = tbl2.type) from tbl1
Hope it help

Joining two tables without duplicating

I have tables like this
Table 1 :
ID NAME
001 John
Table 2 :
ID NAME FAMILY
001 John Kate
001 John Jane
Table 3 :
ID NAME TRAINING
001 John ERP
001 John CCNA
001 John Java
I want to join these tables and show data like this :
Join Table :
ID NAME FAMILY TRAINING
001 John Kate ERP
001 John Jane CCNA
001 John Java
Can someone help me find a SQL statement so that I can get that result?
I Try using UNION like this
SELECT table1.ID, table1.name, table2.family, null as training
FROM table1 INNER JOIN table2 ON table1.ID = table2.ID
UNION
SELECT table1.ID, table1.name, null as family, table3.training
FROM table1 INNER JOIN table2 ON table1.ID = table3.ID
i got result like this :
ID NAME FAMILY TRAINING
001 John Kate NULL
001 John Jane NULL
001 John NULL Java
001 John NULL CCNA
001 John NULL ERP
but, i want to got result like this
ID NAME FAMILY TRAINING
001 John Kate ERP
001 John Jane CCNA
001 John NULL Java
so, anyone here can help me to solve this problem?
Don't think this can be done in a generic way. There is no way to connect Training to Family, there is no connection between the 2. Kate has nothing to do with ERP, could just as easily be CCNA or Java. Therefor, you will always get 6 results: John-Kate-3 Trainings, John-Jane-3 Trainings. (which is the correct result, logically speaking)
If you really want the result you mentioned, and I can't think of a reason why you would, you'd have to write a non-generic query. This would probably be along the lines of:
First select John, Kate and a (random?) training (top 1, or something, I'm not familiar with firebird)
Then select the same for Jane
Finally select John, NULL and the left-over training.
Like I said, this would be a non-generic query where you use the values "Kate" and "Jane" in the where clause(s).
If I where you, I'd have some deep thoughts about the whole set-up, and what you're trying to accomplish. You are trying to use SQL to do something it was never meant to do, in fact, it's the opposite of what SQL was meant to do.

SQL Query that leaves duplicate fields blank?

What I would like is if my data is this:
ID Name
0 Jim
1 Dave
1 Bob
1 John
2 Ann
My query returns this:
ID Name
0 Jim
1 Dave
Bob
John
2 Ann
Is there a simple way to do this?
Edit:
The query which returns the original 2 columns of data would be something like this:
SELECT ID, NAME
FROM TestTable
ORDER BY ID