I am trying to use a concat result in one of my queries in oracle:
Select *
from T1
join T2
on T1.f1 like CONCAT(T2.f2,'%')
This is not producing results for T2.f2 rows with NULL value. I assumed that would have resulted in something like T1.f1 like '%'.
Any ideas?
I am not completely sure of what you're attempting;
however, to get around the specific issue you have, try COALESCE:
SELECT *
FROM T1
JOIN T2
ON T1.f1 LIKE CONCAT( COALESCE( T2.f2, '' ), '%' );
Below query may help you
SELECT *
FROM PINKY1
INNER JOIN PINKY2 ON NOTES LIKE VAL1
WHERE NOTES LIKE VAL2
AND NOTES LIKE VAL3
AND NOTES LIKE VAL4
AND NOTES LIKE VAL5
Related
Let's say I have 2 tables: t1 and t2, the data looks like below:
Both colA and colB are stored as string.As you can see, the data in colB is splited by comma.
Now I want to make a join between 2 tables and the condition is that if colA's value can match colB's value after splited by comma.
I imagine the code may looks like:
select * from t1 join t2 on t1.colA in split(t2.colB,',')
and the result should look like:
I wonder how can I achieve the same effect in Hive sql. I hope my statement is clear. Please help if you have any idea, thanks!
You should fix the data model. Storing multiple values in a string is just not the SQL way to store data. The values should be in separate rows.
If you are stuck with someone else's really, really bad data model, you can use it, but the query is much less efficient than it otherwise would be:
select *
from t1 join
t2
on ',' || t2.colB || ',' like '%,' || t1.colA || ',%';
You can try array_contains:
select *
from t1
join t2
on array_contains(split(t2.colB,','), t1.colA)
select *
from t1
join (select t2.*
from t2 lateral view outer explode(split(t2.colB,',')) e as colB_exploded
) t2
on t2.colB_exploded=t1.colA
I am trying to join two tables in big query,
Table1 contains an ID column, and Table2 contains a column which has the same ID or multiple ID's in the form of a long string separated by commas, like "id123,id456,id678"
I can join the tables together if Table1.ID = Table2.ID but this ignores all the rows where Table1.ID is one of the multiple IDs in Table2.ID.
I have looked at similar post that tell me to use wildcards like
on concat('%',Table1.ID,'%') = Table2.ID
but this does not work, because it seems to create a string that contains the '%' character and doesn't actually use it as a wildcard.
I'm using standard sql in BigQuery, any help would be appreciated
Below example is for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table1` AS (
SELECT 123 id, 'a' test UNION ALL
SELECT 456, 'b' UNION ALL
SELECT 678, 'c'
), `project.dataset.table2` AS (
SELECT 'id123,id456' id UNION ALL
SELECT 'id678'
)
SELECT t2.id, test
FROM `project.dataset.table2` t2, UNNEST(SPLIT(id)) id2
JOIN `project.dataset.table1` t1
ON CONCAT('id', CAST(t1.id AS STRING)) = id2
result is as below
Row id test
1 id123,id456 a
2 id123,id456 b
3 id678 c
It is doubtful that you have values in the table that start and end with percentage signs. = does not recognize wildcards; like does:
on Table2.ID like concat('%', Table1.ID, '%')
As a warning. Such a construct is usually a performance killer. You would be better off trying to have columns in Table1 and Table2 that match exactly.
I am writing a DB2 Stored procedure where I need to have a condition that select all values from table 1 and table 2 where table1.column_a starts with table2.column_b.
select * from table1 T1, table2 T2 where T1.column_a like T2.column_b + '%'
I tried playing around the above SQL but it seems like an invalid SQL. Any suggestions?
You can use the concat() function or || concatenation operator. + for string concatenation is used by SQL Server and similar databases.
I would phrase this as a join:
select *
from table1 T1 join
table2 T2
on T1.column_a like concat(T2.column_b, '%');
Consider that column_a and column_b has trailing blanks and that there is an issue with like operand. You could always use left() and combine it with RTRIM(). I've have very limited experience with DB2 but this is my hunch.
select *
from table1 T1
INNER JOIN table2 T2
on LEFT(RTRIM(T1.column_a), LENGTH(RTRIM(T2.column_b))) = T2.column_b
How to make query like this:
SELECT column1 FROM table1 WHERE ppp=$parameter
AND (SELECT * FROM table2 WHERE parameter=$parameter AND qweqwe=$parameter2)
PS: '$' not from PHP, I just show that this values are variable.
Actually, I need AND qweqwe<650 in the second query
Maybe something like this:
SELECT
column1
FROM
table1
WHERE
ppp=$parameter
AND EXISTS
(
SELECT
NULL
FROM
table2
WHERE
table1.ppp=table2.parameter
AND qweqwe<$parameter2
)
SELECT column1 FROM table1 WHERE parameter=$parameter
AND exists
(SELECT 1 FROM table2 WHERE table2.parameter=table1.parameter AND table2.qweqwe=$parameter2)
It depends on you data.
You can either use exists as shown in previous answers or use join.
SELECT column1 FROM table1
join table2 on table1.ppp=table2.parameter and table2.qweqwe=$parameter2
where table1.ppp=$parameter
In most cases join will be much faster then exists. But it can turn out that you have to use distinct if there's more then one recotd in table2 for each record in table1
what exactly are you trying to do?
the thing after first AND should be logical, for example
... AND 650<(SELECT qweqwe FROM table2 WHERE parameter=$parameter2)
Normally LIKE statement is used to check the pattern like data.
example:
select * from table1 where name like 'ar%'
My problem is to use one column of table with LIKE statement.
example:
select * from table1, table2 where table1.x is like table2.y%
Query above results error . how to use one column data in like query?
You're close.
The LIKE operator works with strings (CHAR, NVARCHAR, etc). so you need to concattenate the '%' symbol to the string...
MS SQL Server:
SELECT * FROM table1,table2 WHERE table1.x LIKE table2.y + '%'
Use of LIKE, however, is often slower than other operations. It's useful, powerful, flexible, but has performance considerations. I'll leave those for another topic though :)
EDIT:
I don't use MySQL, but this may work...
SELECT * FROM table1,table2 WHERE table1.x LIKE CONCAT(table2.y, '%')
SQL SERVER
WHERE ColumnName LIKE '%'+ColumnName+'%'
ORACLE DATABASE example:
select *
from table1 t1, table2 t2
WHERE t1.a like ('%' || t2.b || '%')
...
WHERE table1.x LIKE table2.y + '%'
declare #LkeVal as Varchar(100)
declare #LkeSelect Varchar(100)
Set #LkeSelect = (select top 1 <column> from <table> where <column> = 'value')
Set #LkeVal = '%' + #LkeSelect
select * from <table2> where <column2> like(''+#LkeVal+'');
For SQLLite you will need to concat the strings
select * from list1 l, list2 ll
WHERE l.name like "%"||ll.alias||"%";
for MySql you use like below,which is worked for me
SELECT * FROM table1, table2 WHERE table1.x LIKE (table2.y);
It takes table2's column values of y.
This works for me:
SELECT * FROM `mdl_user` WHERE `firstname` LIKE concat(lastname,'%') AND lastname != ''