SQL Query using Join - sql

I have to create a table A which selects few columns from table B and B joins with another table C. But C takes few values as input from another table D. I just want to know the format of the query.
This is what I tried:
INSERT INTO A
SELECT a,b,c,d
FROM B
LEFT JOIN C
WHERE 1=1, few other conditions
I am not sure where should I put D

Related

Almost equal table with different running time

I’m using oracle. I have two table A and B
Table A has 8000 rows and 5 five columns
Table B has 5500 rows and same 5 columns
All of 5500 rows in table B are contained in Table A and they are the same
I have a query like
With t1 as (select distinct(id) from table A/B)
,T2 as (select a.id, c.value, d.value from Table A/B a
Join table c c on “conditions”
Join table d d on “conditions”
) select * from t2
So the query with Table A works excellent but with Table B it freezes for eternity.
Data types and other properties are equal in table A and table A.
Where should i look for the problem?
I tried to explain plan but differences only in row “PX PARTITION HASH JOIN-FILTER” in Table A and “PX BLOCK ITERATOR ADAPTIVE” in table B

Does wrapping my Coalesce in a subquery make my query more efficient or does it do nothing?

Lets say I have a query where one field can appear in either Table A or Table B but not both. So to retrieve it I use Coalesce.
Something like
Select
...
Coalesce(A.Number,B.Number) Number
...
From Table A
Left Join Table B on A.C= B.C
Now lets say I want to join another table to that Number field
should I just do
Join Table Z on Z.Z = Coalesce(A.Number,B.Number)
Or is it better to wrap my original table in a query and join on the definite result. So something like
Select * from (
Select
...
Coalesce(A.Number,B.Number) Number
...
From Table A
Left Join Table B on A.C= B.C
) T
left join Table Z on Z.Number= T.Number
Does this make a difference?
if i were joining another table to the result of the first query instead of a sub query i would place the first part in a CTE whenever possible, i believe the performance would be the same as a subquery but CTEs are more readable in my opinion.
with cte1 as
(
Select
...
Coalesce(A.Number,B.Number) Number
...
From Table A
Left Join Table B
on A.C= B.C
)
select *
from cte1 a
Join Table Z
on Z.Z = a.number

Oracle SQL to subtract 2 values from different table joins

I am trying to subtract sequences MN_SEQ from Table C generated based on join with other tables.
Here is the problem.
Query 1 -
Select M_Seq from Table C, Table A, Table B where C.date_sk=A.MTH_END_DT
and B.Loan_seq=A.Loan_seq
Query 2 -
Select M_Seq from Table C, Table B where C.date_sk=B.ORIG_DT
I have to get difference between 2 M_SEQ generated from the result set of query 1 and Query 2.
Below is what i tried, but I am getting error.
select mn_seq -mn_seq from
((select mn_seq from Table C, Table A, Table B where B.MTH_END_DT=C.DATE_SK and B.LOAN_SEQ=A.LOAN_SEQ)a,
(select mn_seq from Table C , Table B where B.ORIG_DT=C.DATE_SK
)b)
T
Kindly provide inputs . I am not sure if this is the right way to do it. I tried just using "-" between queries but didnt work. Thanks!
Try this..
SELECT (SELECT mn_seq
FROM TABLE c, TABLE a, TABLE b
WHERE b.mth_end_dt = c.date_sk
AND b.loan_seq = a.loan_seq) -
(SELECT mn_seq FROM TABLE c, TABLE b WHERE b.orig_dt = c.date_sk)
FROM dual
I assume both the mn_seq are NUMBER and also your WHERE clause returns only one record in each of the inner queries.

Omitting a list of observations in sas sql

I'm trying to create a table in sas that will take observations from a specific column as long as they aren't list in another column in another table.
I've used the code:
proc sql;
create table tbl as
select a.var1, a.var2, a.var3 from
tblA as a, tblB as b
where a.var1~=b.var1;
quit;
Would it be because I've assigned b as a table I'm not selecting a variable from? or is my condition just incorrect?
Your condition is incorrect, you need to tell the tables how to join (where the equality is) THEN tell them that you only want those that dont match.
A left join is used for this:
select a.var1, a.var2, a.var3
from
tblA as a
left join tblB as b on a.var1 = b.var1
where
b.var1 is null
Where a are the values you want that dont match b.
See SAS SQL join examples for more
This can also be accomplished using NOT IN

Sql Query To read from 4 tables

i have 3 tables
table A, table B and a relational table C
table A has ta_id as primary key along with other columns
table B has tb_id as primary key along with other columns
table C has ta_id, tb_id as foreign keys along with other columns.
i wanted to find all the rows of table B which have a common ta_id in table C.
my sql query for the that is.
SELECT B.ta_id,
B.type,
B.language,
B.user_id
FROM B
INNER JOIN C
ON B.tb_id=C.tb_id
where C.ta_id = 1
ORDER BY B.user_id
the above query seems to be working..
but now i have another table called table D with D.tb_id as a foreign key (which is primary key in table B ).
each row of table B has 0 or more rows associated in table D or we can say
1 or more rows in table D has exactly one corresponding row in table B.
Now i want to list my each row of table B with all the associated rows of table D.
so it should look like this.
first row of table B
first corresponding row of table D
second corresponding row of table D
...
..
second row of table B
first corresponding row of table D
second corresponding row of table D
...
..
so in a way i am mixing the contents of 2 tables in display
Please tell me how to achieve this using a sql query..?
Waiting for reply..!
Thanks
Big O
Just add another inner join like this:
SELECT B.ta_id, B.type, B.language, B.user_id
FROM B
INNER JOIN C
ON B.tb_id=C.tb_id
INNER Join D
ON B.tb_id=D.tb_id
WHERE C.ta_id = 1
ORDER BY B.user_id
I believe that you can use SQL views easily to query data with lot of tables
You cannot do this in one simpl query, you need a loop. Think about what you are trying to do...
TABLE B ROW 1
TABLE D ROW 1 (Matching Row 1 Table B)
TABLE D ROW 2 (Matching Row 1 Table B)
TABLE D ROW 3 (Matching Row 1 Table B)
TABLE B ROW 2
TABLE D ROW 1 (Matching Row 2 Table B)
TABLE D ROW 2 (Matching Row 2 Table B)
TABLE D ROW 3 (Matching Row 2 Table B)
ETC...
ETC...
The only way you can do this is inside a stored procedure using temp tables and looping.