I need to combine two tables and then joiing with 10 more tables in SQL - sql-server-2012

I have select SQL query in vb.net as below to fetch record. Except for table1 & table1history all other conditions are seems to be same. Is there any way to place the filtered date from table1 & table2 into a temp table and perfom multiple join only once against the temp ?
select
colA, colB, ...
from
table1
join
table2 on tabel1.col = table2.col
join
table3 on tabel3.col = table2.col
join
table4 on tabel4.col = table2.col
join
table5 on tabel5.col = table2.col
join
table6 on tabel6.col = table5.col
left join
table7 on tabel7.col = table5.col
Left join
table8 on tabel8.col = table4.col
Left join
table9 on tabel9.col = table8.col
Left join
table10 on tabel10.col = table5.col
union
select
colA, colB, ...
from
table1History
join
table2 on tabel1History.col = table2.col
join
table3 on tabel3.col = table2.col
join
table4 on tabel4.col = table2.col
join
table5 on tabel5.col = table2.col
join
table6 on tabel6.col = table5.col
left join
table7 on tabel7.col = table5.col
Left join
table8 on tabel8.col = table4.col
Left join
table9 on tabel9.col = table8.col
Left join
table10 on tabel10.col = table5.col;

Related

Combine column after left join

How do I combine 3 returned value columns into one column based on which column is not null?
My query is:
SELECT val1,val2,val3
FROM db.table1 t1
LEFT JOIN db.table2 t2 ON t2.pk = t1.t2_fk
LEFT JOIN db.table3 t3 ON t3.pk = t1.t3_fk
use coalesce()
SELECT coalesce(val1,val2,val3) as va
FROM db.table1 t1
left join db.table2 t2
on t2.pk = t1.t2_fk
left join db.table3 t3
on t3.pk = t1.t3_fk

Postgres UPDATE using SELECT whit INNER JOIN multiple tables

I am just starting with Postgres and I find myself with the following Query made in Postgres, which seeks to bring me data from 4 different tables.
SELECT
table1.id,
table2.icon,
table3.title,
table4.description
FROM
table1
JOIN table5 ON id_t5 = id_t3
JOIN table3 ON id_t1 = id_t3
AND id_t3 = 816
LEFT JOIN table2 ON table5.id_t2_fk = table2.id_t2
LEFT JOIN table4 ON table4.id_t3_fk = table1.id_t1;
My problem is that I have to make an UPDATEof these 4 tables after generating the Query.
I can not think of how to solve the problem, since the UPDATE syntax of Postgres is different from that of MySQL or SQLserver.
I tried to do this:
UPDATE
table1
INNER JOIN table5 ON id_t5 = id_t3
INNER JOIN table3 ON id_t1 = id_t3
LEFT JOIN table2 ON table5.id_t2_fk = table2.id_t2
LEFT JOIN table4 ON table4.id_t3_fk = table1.id_t1
SET
table2.icon = "new icon",
table3.title = "new title",
table4.description = "new description"
WHERE
table1.id_t1= 816;
Postgres allows you to do updates in CTEs. Perhaps this does what you want:
with data as (
select t1.id, t2.id_t2, t2.icon, t3.id_t3, t3.title,
t4.id_t4, t4.description
from table1 t1 join
table5 t5
on id_t5 = id_t3 join
table3
on id_t1 = id_t3 and id_t3 = 816 left join
table2 t2
on t5.id_t2_fk = t2.id_t2 left join
table4 t4
on t4.id_t3_fk = t1.id_t1
where t1.id_t1= 816
),
u2 as (
update table2
set icon = 'new icon'
where t2.id_t3 in (select data.id_t2 from data)
returning *
),
u3 as (
update table3
set title = 'new title'
where id_t3 in (select data.id_t3 from data)
returning *
)
update table4
set description = 'new description'
where id_t4 in (select data.id_t4 from data);
If not, something similar will.

How to replace an OR statement from a join in sql server

I have the following query that uses an or statement on a join, so basically if one condition on the join isn't met it must check the next condition. The problem is that with the OR statement it takes really long to run but when I remove one of the OR conditions it runs instantly. is there a better way to do this with both conditions without using the OR statement so it would speed up the query
select t5.TransactionNumber
,t4.ID
,t3.[Entry] AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
inner join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code) or (t3.TypeID = t2.TypeID) //on this join i have an or statement if one condition isnt met it must check the next condition
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'
By the rule of distributivity in logic,
P OR (Q AND R) can be written as
(P OR Q) AND (P OR R).. maybe that helps?
You could try by using left join and COALESCE function
select t5.TransactionNumber
,t4.ID
,COALESCE(t3.[Entry],t33.[Entry]) AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
left join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code)
left join Table3 t33 (t33.TypeID = t2.TypeID) //I moved it to left join
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'
You can try below query :
select * from
(
select t5.TransactionNumber
,t4.ID
,t3.[Entry] AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
inner join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code)
)A
join Table3 t3 (NOLOCK) ON (A.TypeID = t3.TypeID)
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'

How to join 3 tables in sql with or condition

I have 3 tables:
table1
inner join with table2; or:
inner join with table3
The query:
select table1.x
from table1 right outer join
table2
on table1.x = table2.x right outer join
table3
on table1.x = table3.x
but I can only see x values that are in both table2 and table3
Use left join, not right join, along with an appropriate filter condition:
select table1.x
from table1 left join
table2
on table1.x = table2.x left join
table3
on table1.x = table3.x
where table2.x is not null or table3.x is not null;
You might consider writing this using exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.x = t1.x) or
exists (select 1 from table3 t3 where t3.x = t1.x);
That seems like a more natural way to implement your logic -- and you won't get duplicates when x is duplicated in table2 or table3.

SQL Join multiple tables using where clause

I want to write a sql statement:
Select * FROM table1
inner join table2 AS t2
ON
inner join table3 AS t3
ON
inner join table4 AS t4
ON
Where FK_Client_ID = 4
All tables have the client ID in common. So Not sure what to do on the ON.
Will it be something like ON t2.FK_Client_ID = ...... not sure.
So I just want all the data from those tables that has FK_Client_ID in common.
Select *,
(Select FK_Client_ID from table2 where FK_Client_ID = t1.FK_Client_ID )As TID1,
(Select FK_Client_ID from table3 where FK_Client_ID = t1.FK_Client_ID )As TID2,
(Select FK_Client_ID from table4 where FK_Client_ID = t1.FK_Client_ID )As TID3
FROM table1 t1
Select * FROM table1 t1 inner join table2 t2
ON t1.FK_Client_ID = t2.FK_Client_ID
inner join table3 t3
ON t1.FK_Client_ID = t3.FK_Client_ID
inner join table4 t4
ON t1.FK_Client_ID = t4.FK_Client_ID
Where t1.FK_Client_ID = 4
Try this
Select * FROM table1 t1
inner join table2 AS t2
ON t2.FK_Client_ID = t1.FK_Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = t1.FK_Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = t1.FK_Client_ID
Where t1.FK_Client_ID = 4
if your foreign field name is "FK_Client_ID" and primary key in table1 is "Client_ID"
Select * FROM table1
inner join table2 AS t2
ON t2.FK_Client_ID = table1.Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = table1.Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = table1.Client_ID
Where table1.Client_ID = 4
Since you are using inner join no matter you join them with table1 or any other of them