Hi I have existing sql script need to optimise. How the script written is like this
select * from table A
Where (A.ID, A.name, A.age)
in
(
Select B.ID,B.name, B.age
from table B
where B.ID in
(
Select A.ID from table A Where age = ‘30’
)
)
When I try to run this it takes very Long time and I have no idea why the previous programmer write in this ways please help if there is better ways to rewrite this. Appreciate much thanks!
I would try something like this:
Select A.* from (select ID, NAME, age from table A WHERE age = 30) A
LEFT JOIN table B ON A.ID = B.ID AND B.NAME = B.NAME AND A.age = B.age
Related
This is a little hard to explain - hopefully it is an easy solve. I have a table that I am doing a select on.
Here is the general query:
SELECT id, sub_id, name, inception_date, pc_code FROM investments;
This will pull everything which is great - but I need to use the sub_id field data that is returned from this select statement to re-query the table and pull in the name of the sub id. Here is what I am after in psudo code:
SELECT id, name, **sub_id**,
(SELECT name FROM investments where id = (sub_id from outer select statement)),
inception_date, pc_code
FROM investments;
sub_id and sub_id names would just query the same table for id and name.
I hope this makes sense and thanks for everyone's help!
It looks like you don't need a subselect but double use of the same table with outer join, try this and be aware that the two columns from the table using sub_id can be null in case there is no match.
SELECT a.id, a.name, b.id , b.name, a.inception_date, a.pc_code
FROM investments a
LEFT OUTER JOIN investments b ON b.id = a.sub_id;
Is this work?
WITH cte as (SELECT id, name FROM investments )
SELECT a.id, a.name, a.sub_id, a.inception_date, a.pc_code , b.name
FROM investments a
INNER JOIN cte b on b.id = a.sub_id ;
Or easier
SELECT a.id, a.name, a.sub_id, a.inception_date, a.pc_code , b.name
FROM investments a
INNER JOIN investments b on b.id = a.sub_id ;
I have the following query in Hive and since Hive doesn't allow more than 1 sub select, I am not sure how to work this out. I looked at existing postings but had nothing that came close to this. I have additional subselects in this query for more joins. Any help is greatly appreciated. Thank you.
select * from table_a a inner join table_b on a.id = b.id)
where a.timestamp in (select max(c.timestamp) from table_a c
where c.id = a.id)
and b.timestamp in (select max(d.timestamp) from table_b dateofbirth
where d.id = b.id);
I have a simple question on how to read the number of occurences of some values in a table B that references to a value in a table A. It's all explained better in the following example:
Let's say I have two simple tables, A with an attribute ID and B with an attribute ID that references A.ID; I use this code to find the number of occurences of a A.ID value into B:
SELECT A.ID, COUNT(*)
FROM A JOIN B ON A.ID = B.ID
GROUP BY A.ID
Is it possible to achieve the same result using something like the following code...
SELECT ID, -- SOMETHING --
FROM A
....
... using this subquery?
SELECT COUNT(*)
FROM B WHERE B.ID = A.ID
Thank you very much
I think you might be referring to a correlated subquery:
select a.id, (select count(1) from b where id=a.id) cnt from a;
The a.id term in the subquery binds with table a in the outer query.
I am working on a project, and need to join results from 2 tables into one set.
The tables are ordered as such:
gameData: [Id,TeamID, data..........]
players: [Id (same as above), name, data.....]
I need to do something like:
SELECT * FROM gameData and SELECT data FROM players WHERE gameData.Id = players.Id
And here is what I have thusfar.
SELECT * FROM gameData AS A LEFT OUTER JOIN players AS B on A.playerID = B.Id;
And have it return all of the values from A, and only the data from B.
I know that the syntax is not correct, I have little experience working with SQL Joins, any advice would be greatly appreciated!
Edit: Trying both answers now. Thanks!
Edit2: Can I do something like: "Select a.* from tableA as a"
I love you guys, working as intended now!
Thanks!
The query I ended up using was:
Select a.*, b.height, b.weight from gameData as a LEFT OUTER JOIN players b on a.playerID = b.Id;
You could enumerate the fields that you select and alias the tables, like:
select a.Id, a.TeamId, a.data, b.data
from tableA a
join tableB b on a.Id = b.Id
Select a.Id, a.TeamID, a.data, b.data
FROM gameData as a
LEFT OUTER JOIN
players b On a.ID = b.ID
Is it possible to select multiple values from a subquery in SELECT block?
Selecting one value works fine like this:
SELECT
a.id,
(SELECT b.id FROM b WHERE b.a_id = a.id) AS b_id
FROM
a
But if i also want to get the b.name and i change the query to this:
SELECT
a.id,
(SELECT b.id, b.name FROM b WHERE b.a_id = a.id)
FROM
a
... it doesn't work anymore. One possibility would be to put the subquery to FROM block and take values from there but in my particular query that doesn't work so i would like to solve in SELECT block. Thank you!
This will help you
SELECT A.ID,
B.ID,
B.NAME
FROM A INNER JOIN B ON B.A_ID=A.ID;