Table: FirstNames
NAME
Tom
Joe
Peter
Table: FullNames
FULL_NAME:
Tom Petty.
Joe Satriani.
Peter Griffin.
Sarah Connor.
I would like to run a query:
select *
from FullNames where FULL_NAME like '%' || (select NAME from FirstNames) || '%'
It yields:
ORA-01427: single-row subquery returns more than one row
which seems correct. Is there a way to do that in Oracle?
You could use JOIN:
SELECT *
FROM FullNames f
JOIN FirstNames g
ON f.FULL_NAME LIKE '%' || g.NAME || '%';
You can use exists:
select f.*
from FullNames f
where exists (select 1
from firstnames fn
where f.FULL_NAME like '%' || fn.NAME || '%'
);
Related
Let say I have student Table like below in BigQuery:
Mark Table looks like below in Big Query:
I would like to join this two tables and create a new table like below :
Consider below query,
SELECT s.*, m.Mark_Details
FROM student_table s
JOIN (
SELECT student_ID,
STRING_AGG('' || Subject_ID || ':' || Subject_Name || ':' || Mark, ' | ') AS Mark_Details
FROM mark_table
GROUP BY 1
) m USING (student_ID)
I have a T-SQL query that has a subquery listing 2 names:
Coop
Bro
Code:
Select LastName
From Managers
Where Type = prefix;
I need to have the outer query use this above sub-query in something like an 'IN' statement, but it's not an exact match, but rather a BEGINS WITH. Something like the following with + '%':
Select *
From Employees
Where LastName In (Select LastName + '%'
From Managers
Where Type = prefix)
Desired query would return back outer query results such as:
Cooper
Coopersmith
Coopmoth
Brown
Brownly
Bronnan
...but, this is not working. I get 0 results.
You can use exists. Presumably, you intend something like this:
Select e.*
from Employees e
where exists (select 1
from managers m
where type = prefix and
e.LastName like m.LastName + '%'
)
Got it with similar to:
select * from employees e
inner join Managers m on e.lastname like m.lastname + '%' and m.type = prefix
Get all Users from first table that have any word matching in another table;
I have Users table that contain, among others, column with FullName, that column is Full Text Search Indexed. Sometimes first "word" is name, sometimes first "word" is surname from the FullName column;
Like John Smith or Smith John.
And another table that have just local firstname.
I want to get all Users that have matching local name.
Users Table:
John Smith
Rebecca Mark
Maria Anna
Lance Maria
Emilia Clark
Snow John
Natalie Butler
Name Table:
Maria
Smith
Result of Query:
John Smith
Maria Anna
Lance Maria
Snow John
I can do only single Name with Contains function.
SELECT * FROM Users WHERE CONTAINS(FullName, 'John');
But I need it for each row in Name Table.
Every row from FullName contains any of Name Table... But in SQL Query.
To avoid a case where you are search for 'Maria' and the matching name is 'Marianne', check for 2 conditions: (1) the name is at the start or (2) at the end of FullName:
(1):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
u.FullName LIKE concat(n.name, ' %')
OR
u.FullName LIKE concat('% ', n.name)
or (2):
SELECT u.*
FROM Users u INNER JOIN Name n
ON
concat(' ', u.FullName, ' ') LIKE concat('% ', n.name, ' %')
use join and like for matching
select u.* from table_users u join table_name b on
u.users like concat('%',b.name,'%')
You can use exists for this:
select u.*
from users u
where exists (select 1
from nametable n
where u.fullname like '%' + n.name + '%'
);
If you want to avoid partial matches on names, then take the delimiters into account:
where exists (select 1
from nametable n
where ' ' + u.fullname + ' ' like '% ' + n.name + ' %'
);
I'm creating a sort of geocache team building system and I'm having trouble with my query.
To collate and display the individuals against the teams I'm using GROUP_CONCAT, however when I try to include the locations (listed as markers) the individuals names are being duplicated based on the number of markers.
I have put together a SQL Fiddle to explain my workings.
Here is my query:
SELECT SQL_CALC_FOUND_ROWS
map_team.id AS team_id,
map_team.name,
GROUP_CONCAT(firstname, ' ', surname SEPARATOR ', ') AS full_name
FROM map_team
LEFT JOIN members
ON members.map_team=map_team.id
WHERE members.map_team IS NOT NULL
GROUP BY map_team.name
ORDER BY map_team.name ASC
Any advice would be appreciated.
GROUP_CONCAT( distinct ... ) will not give correct answers as the team member names in a team can be same. If you want to get it in a single query you can use:
SELECT SQL_CALC_FOUND_ROWS
map_team.id AS team_id,
map_team.name,
GROUP_CONCAT(firstname, ' ', surname SEPARATOR ', ') AS full_name,
(select count(*) from map_markers where team_id = map_team.id) AS total_markers,
(select count(*) from map_markers where team_id = map_team.id and status = 'completed') AS total_completed
FROM map_team
LEFT JOIN members
ON members.map_team=map_team.id
WHERE members.map_team IS NOT NULL
GROUP BY map_team.name
ORDER BY map_team.name ASC
If you don't like the idea of a subquery in select. You need to do it separately.
Use
GROUP_CONCAT(distinct firstname ...
use following query:
SELECT SQL_CALC_FOUND_ROWS
map_team.id AS team_id,
map_team.name,
GROUP_CONCAT(distinct firstname, ' ', surname SEPARATOR ', ') AS full_name,
count(*) AS total_markers,
SUM(IF(status = 'completed', 1, 0)) AS total_completed
FROM map_team
LEFT JOIN members
ON members.map_team=map_team.id
LEFT JOIN map_markers
ON map_markers.team_id=map_team.id
WHERE members.map_team IS NOT NULL
GROUP BY map_team.name
ORDER BY map_team.name ASC
I need to fetch the row value available in the same table using join query .
example :
Id (Primary Key) Name Address Parent Organization ID (Foreign Key) reference (Id)
1 Sup Org1 Address1 null
2 Sup Org2 Address2 null
3 Sup Org3 Address3 null
4 Sub Org5 Address4 1
Output:
Name Address
Sup Org1 Address1
Sup Org2 Address2
Sup Org3 Address3
Sub Org5(Sup Org1) Address4
Except the Super organaization value all the sub organization should be appended with
the mapped Super Organziation (ex: Sub Org5(Org1)).
Could any one help me out on this to write query to fetch like above output format
I didn't test but this should cover
Select
t1.Name || Decode(NVL(t2.Name , ''), '', '', '(' || t2.Name || ')'), -- here we convert null to empty string and don't wrap empty string into parenthesis
Address
From Mytable t1 left join
Mytable t2 on t1.id = t2.Parent
Here's the SQL Fiddle (which you should supply next time you have a question like this) and here's the SQL:
SELECT o2.Name, case when o1.Name is not null then '(' || o1.Name || ')' END, o2.Parent_Organization_ID, 01.Address
FROM Organization o1
RIGHT JOIN Organization o2 on o1.id = o2.Parent_Organization_ID
ORDER BY o2.NAME
the procedure you're looking for is called a "self join". sql zoo has some lessons that might help sharpen your skills.
no fiddle provided, but this looks about right to me:
select sub.name||coalesce('('||sup.name||')',''),address
from table_name as sub
left join table_name as sup on sub.parent=sup.id