Use a specific column from a result set of a SELECT in another SELECT statement in same SP - sql

I have a SELECT statement which gives me a result set containing ID as a column name. I want to use the ID column in another select. Is there any possible way to do this
SELECT A.Id
,B.Product
,A.Name
,B.ProductCode
FROM Customers A
INNER JOIN Product B ON A.Id = B.Id
I want to use the resulting column A.Id in another Select
SELECT * FROM SELLER WHERE ID = 'A.ID'(How to get this A.ID??)
I need to use this logic inside a Stored Procedure where I have If else condition. So I am not able to use temporary table with same name here.

That's very simple. Just join another source table also to the procedure
SELECT A.Id
,S.*
,B.Product
,A.Name
,B.ProductCode
FROM Customers A
LEFT JOIN Product B ON A.Id = B.Id
LEFT JOIN SELLER S ON S.ID = A.ID

Related

SQL Get rows that doesn't appear in another table

I have this SQL problem: I have tables A and B. Table A has columns id and name, Table B amount and id which is a foreign key to table A.id.
I need to return all table A rows that don't have their id stored in table B. Any ideas?
So the complete opposite is:
SELECT *
FROM a
LEFT OUTER JOIN b ON a.id = b.id;
Here row what I need is left out of result
Just add a where clause:
SELECT a.*
FROM a LEFT OUTER JOIN
b
ON a.id = b.id
WHERE b.id IS NULL;
You can also use NOT EXISTS:
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
In most databases, the two methods typically have similar performance.

Separated JOIN form main INNER JOINS's

I want to INNER JOIN some tables and then insert a condition where the entries of a table are dependant on another table (that was not joined with the others)
Something like this:
SELECT * FROM TABLE_A AS a
INNER JOIN TABLE_B AS b ON b.id_b=a.id_a
INNER JOIN TABLE_C AS c ON c.id_c=b.id_b
Now I want to add a condition (possibly a "WHERE" clause) that only selects the values in a field in TABLE_C that match another condition, the existence of a value in a field in TABLE_D
Possible statement:
WHERE c.code=d.another_code AND d.reg_number LIKE 999%
How do i declare in the query the TABLE_D, since I do not want to Join it with the others?
In other words, I want to intersect 3 sets (A,B,C) and the other one (set D) is intersected only with set C
The title of the question Run-time error '13': ... doesn't seem to match the content so I'll just answer the SQL part.
Maybe this is what you want?
SELECT * FROM TABLE_A AS a
INNER JOIN TABLE_B AS b ON b.id_b=a.id_a
INNER JOIN TABLE_C AS c ON c.id_c=b.id_b
WHERE c.code = -- or possiby IN instead of =
(SELECT another_code FROM TABLE_D WHERE another_code LIKE '999%')
If the subquery can return multiple rows you need to use WHERE c.code IN instead of WHERE c.code =

Can I have record as a parameter in INSERT statement?

I'm trying to populate a new table T.
create or replace procedure extractData
is
cursor c1 is
SELECT a.id,a.data,b.id,b.data,c.id,c.data
FROM a LEFT JOIN b ON a.id=b.id
LEFT JOIN c on b.id=c.id;
begin
for currRec in c1
loop
insert into table T
values currRec,(select a_seq.nextval from dual),(select b_seq.nextval from dual),(select b_seq.nextval from dual),SYSDATE
end loop
Will this give me a table with columns from currRec then the sequences and SYSDATE? Also is there a better way to do this without making it more messy than it already is? In the real table, I have about 10 'data' columns in a,b and c each so I'm worried about something screwing up or going out of order. I'm writing a procedure for a data warehouse that takes in data from three different sources and combines them in one table.
INSERT
INTO table
SELECT a.id, a.data, b.id, b.data, c.id, c.data,
a_seq.nextval, b_seq.nextval, c_seq.nextval,
SYSDATE
FROM a
LEFT JOIN
b
ON b.id = a.id
LEFT JOIN
c
ON c.id = b.id

Can this be done with a single SQL Join?

I am not sure if this can be done with a single JOIN, but I basically have two tables with an ID column in common. To make it simple I'll say Table A just contains an ID while Table B contains an ID and Code. There is a 1:M relationship between Table A and Table B, however it's also possible an ID from Table A is not contained in Table B at all. I was hoping to have a query return every ID that exists in Table B within a particular code range, or does not exist in Table B at all.
I tried using a LEFT JOIN with something like:
SELECT A.id FROM A LEFT JOIN B ON A.id = B.id AND b.code BETWEEN '000' AND '123'
But, this still gives me the IDs that exist in Table B outside of the code range.
Use a left join, and filter the result to contain the codes in the range, and also the lines where there is no matching record in table B:
select
A.id
from
A
left join B on B.id = A.id
where
B.code between '000' and '123' or B.id is null
What about
SELECT id FROM A LEFT JOIN B ON A.id = B.id
WHERE b.code IS NULL OR b.code BETWEEN ' ' AND '123'

The Difference Inner Join Query

I'm just curious, if i have table a and table b.
I write query 1:
SELECT * FROM table a INNER JOIN table b ON table a.id = table b.id
I write query 2:
SELECT * FROM table b INNER JOIN table a ON table b.id = table a. id
What is the difference both of above query?
Thank you
When using INNER JOIN , there is no difference in resultset returned except in order of columns when SELECT * is used i.e. columns are not explicitly mentioned.
SELECT *
FROM table a
INNER JOIN table b
ON table a.id = table b.id
returns columns from tableA followed by columns from tableB
SELECT *
FROM table b
INNER JOIN table a
ON table b.id = table a. id
returns columns from tableB followed by columns from tableA
The second table matches data with the first one.
So it is better to put smaller table on the second place.