Hive SQL: How to join number to a string of delimited numbers - sql

I need to join two tables by an ID where one ID is stored as a number (i.e. 12345) the other ID is stored as a pipe delimited string (i.e 12345|12346|12347). Is there a quick way to join on the two? Thanks!
** I guess I should say join if the number ID (12345) is in the string of numbers (12345|12346|12347). In theory this example would join as 12345 is in the pipe delimited string.

This will work in Hive
select obj1.*,obj2.some_fields from table1 obj1
JOIN table2 obj2
on (obj1.id=split(obj2.id,'|')[0])

It's not clear to me if you mean SQL or HiveQL.
Is there a quick way to join on the two?
No, not really.
Your DB schema violates First Normal Form. Joining these tables will be slow and error prone.
For DB-agnostic try:
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t2.id LIKE ('%' + CAST(t1.id as varchar) + '%')

Related

Postgres Query based on Delimited String

we have a column in one of our tables that consist of a delimited string of ID's, like this: 72,73,313,502.
What I need to do in the query, is to parse that string, and for each ID, join it to another table. So in the case of this string, I'd get 4 rows.......like the ID and the name from the other table.
Can this be done in a query?
One option is regexp_split_to_table() and a lateral join. Assuming that the CSV string is stored in table1(col) and that you want to join each element against table2(id), you would do:
select ... -- whatever columns you want
from table1 t1
cross join lateral regexp_split_to_table(t1.col, ',') x(id)
inner join table2 t2 on t2.id = x.id::int
It should be noted that storing CSV strings in a relational database is bad design, and should almost always be avoided. Each value in the list should be stored as a separate row, using the proper datatype. Recommended reading: Is storing a delimited list in a database column really that bad?

Sql server query with CTE containing CLR returns empty string after joining to more than 5 tables

Kind of a weird one.
I have a CTE, it selects 3 columns. and id, then two varchar fields. The varchar fields use a CLR function to concatenate names with a '/' dividing them.
I run this query on 5 environments and on 4 of them it returns the names with the '/'.
On the other environment, it's empty string (all enviros/servers have same copy of db).
Running the CTE body returns data fine on all environments.
In commenting out sections of the sql, data is returned fine until I join to more than 5 tables (even if the table has nothing to do with the query). Just seems that no matter what table I join to, after 5 joins my varchar data becomes empty string. If I filter out for a specific id, data is returned fine.
Im guessing there's some sort of environment config that's different on the one environment compared to the others. Anyone have any idea?
with my_cte as
(
select my_id,
dbo.List(names1, '/') names1,
dbo.List(names2, '/') names2
from table
where blah= blah
)
select A.some_field,
OU.names1,
OU.names2
from myTable A
inner join my_cte OU on (OU.this_id = A.this_id)
inner join table3
inner join table4
inner join table5
inner join table6 --once i get here, no matter what table6 is, the names1, names2 become empty string
where A.blah = xxxx;
Quick fix was to rewrite the CTE into a temp table until dba's check the config diffs between the boxes. Thanks for the comments.

Using LIKE in a JOIN query

I have two separate data tables.
This is Table1:
Customer Name Address 1 City State Zip
ACME COMPANY 1 Street Road Maspeth NY 11777
This is Table2:
Customer Active Account New Contact
ACME Y John Smith
I am running a query using the JOIN where only include rows where the joined fields from both tables are equal.
I am joining Customer Name from Table1 and Customer from Table2. Obviously no match. What I am trying to do is show results where the first 4 characters match in each table so I get a result or match. Is this possible using LIKE or LEFT?
Yes, that's possible.
But I doubt, that every name in table 2 only has 4 letters, so here's a solution where the name in table2 is the beginning of the name in table1.
Concat the string with a %. It's a placeholder/wildcard for "anything or nothing".
SELECT
*
FROM
Table1
INNER JOIN Table2 ON Table1.CustomerName LIKE CONCAT(Table2.Customer, '%');
Concatenating of strings may work differently between DBMS.
It probably is, though this might depend on the Database you are using. For example, in Microsoft SQL, it would work to use somthing like this:
SELECT *
FROM [Table1] INNER JOIN [Table2]
ON LEFT([Table1].[Customer Name],4) = LEFT([Table2].[Customer],4)
Syntax may be different if using other RDBMS. What are you trying this on?
Seems like this should work:
Select *
From Table1, Table2
Where Table1.CustomerName Like Cat('%',Trim(Table2.CustomerName),'%')
If you are only trying to match first four Characters you can use following :
SELECT --your columns
FROM Table1 T1
JOIN Table T2
ON
SUBSTRING ( T1.CustomerName ,1, 4) = SUBSTRING ( T2.Customer ,1, 4)

Join over substring across tables in MSSQL

I have two tables with one table with one column having a URL and another table having a substring from that URL
Table 1
Id | URL
----------
1 ...\aaa_common\
2 ...\aaa_qa..
3 ...\aaa_test\Analytics
Table 2
SomeId | compname
-----------------
1 aaa_common
2 aaa_qa
3 aaa_test
It is possible to join using string functions (charindex and substring) . But is there an easier alternative?
Yes you can use join, but not sure that this is best method, cause join on string is not good idea, also I am not sure about repetitive values in your table. Still if require to do so, I will suggest you to have one more column in your Table1, in which you can update only compname from same table using sub-string & then join both tables including new column from Table1 & compname from Table2.
Also for using sub-string you should be 100% sure with index/pattern of your compname in string of Table1.
Please look into this DEMO
Just has example of join on string using sub-string & charindex
You can join using like, but it will be a bit of a performance hit.
select
*
from
table_1 t1
inner join table_2 t2 on
t1.url like concat('%',t2.compname, '%')

Oracle Joins with multiple values to one value

I am trying to join between the two tables On one column with one value and other column with multiple values.
For ex. table1 column varieties table2 Items
Fruits vegetables,spinach,fruits
Vegetable
Spinach
Is there any way I can join like this:
select * from table1 t1 left join table2 t2 on t1.varieties in t2.items
Thanks in Advance
It sounds like you have a comma delimited list in the column t1.items. This is bad database design. The right design is to have a separate association table.
However, you can do the join using like:
select *
from table1 t1 left join
table2 t2
on ','||t1.items||',' like '%,'||t2.varieties||',%'
Note that this puts a delimited at the beginning and end of the list and then searches for words surrounded by the delimited. This prevents "apple" from matching "pineapple".