Merge 5 tables data into Master Table - tsql

Main Table : MT
===============
PK, C1, C2
==========
1, X, X
2, X, X
3, X, X
..........
100, X, X
Table 1 :T1
===============
PK, TC1
=======
2, D1
3, D1
Table 2: T2
===============
PK, TC2
=======
3, D2
Table 3: T3
===============
PK, TC3
=======
4, D3
Table 4: T4
===============
PK, TC4
=======
2, D4
I want output of Master Table after making some join or any how as below :
Master Table
===============
PK,C1,C2,TC1,TC2,TC3,TC4
========================
(1,X,X,null,null,null,null)
(2,X,X,D1,null,null,D4)
(3,X,X,D1,D2,null,null)
(4,X,X,null,null,D3,null)
I tried
select * from
MT inner join T1 on MT.PK=T1.PK
inner join T2 on MT.PK = T2.PK
inner join T2 on MT.PK = T3.PK
inner join T2 on MT.PK = T4.PK
But I am getting some rows duplicate. Even tried distinct still getting duplicate. I think there must be some other alternative to achieve this thing.

The problem with your existing query is you are using an INNER JOIN between all of the tables. An INNER JOIN requires that the value of the column being joined on exists in both tables.
It seems to me that you want to use a LEFT JOIN instead:
select MT.PK, MT.C1, MT.C2, T1.TC1, T2.TC2, T3.TC3, T4.TC4
from MT
left join T1 on MT.PK=T1.PK
left join T2 on MT.PK = T2.PK
left join T3 on MT.PK = T3.PK
left join T4 on MT.PK = T4.PK;
See SQL Fiddle with Demo
A LEFT JOIN will return all rows from the MT table and then the data from the other tables if it exists when the PK matches.

Related

Inner join if tables are empty with Top 1000 results

I have three table valued parameters passed into an SP. These all contain data to filter a query. I want to join them to a table as below but only if there is data in a table valued parameter
SELECT DISTINCT TOP (1000)
Person.col1,
Person.col2,
FROM dbo.Person INNER JOIN
#tbl1 t1 ON Person.col3 = t1.val INNER JOIN
#tbl2 t2 ON Person.col4 = t2.val INNER JOIN
#tbl3 t3 ON Person.col5 = t3.val
I am aware I can do a Left Outer Join but I don't want results with NULL values. The top 1000 necessary as there is a lot of data and scanning the whole table causes performance issues
Try use coalesce to cater nulls check
SELECT DISTINCT TOP (1000)
Person.col1,
Person.col2,
FROM dbo.Person INNER JOIN
#tbl1 t1 ON coalesce(Person.col3, '111') = t1.val INNER JOIN
#tbl2 t2 ON coalesce(Person.col4 , '111')= t2.val INNER JOIN
#tbl3 t3 ON coalesce(Person.col5, '111') = t3.val

Postgresql query many to many relationship with left join and use where clouse not show data

I have three table, table_1 (id), table_2(id), table_pivot(tbl1_id, tbl2_id). When I use query like this:
select *
from public.table_1 t1
left join public.table_pivot tp on tp.tbl1_id = t1.id and tp.user_id = 1
left join public.table_2 t2 on t2.id = tp.tbl2_id;
Data from table_1 is show. But when I use where condition. Like this:
select *
from public.table_1 t1
left join public.table_pivot tp on tp.tbl1_id = t1.id
left join public.table_2 t2 on t2.id = tp.tbl2_id
where tp.user_id = 1;
Data from table_1 not show.
I hope advance can help explain why?
Thanks.
Including a where clause on an outer joined table, effectively converts the join into an inner join. If there are no matching values in table_pivot, then
you will get no results at all. This is standard sql.

Left join filtering only works on where clause

Can someone explain this to me?
I have two huge tables on which I wish to LEFT JOIN, but I think it would be more efficient to filter at the 'on' rather than the 'where'.
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 1
Rather than
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id where t1.Enabled = 1
The above results are not the same. No matter what I put in as filtering, it stays unaffected:
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 1
yields exactly the same results as
Select * from Table1 t1 left join Table2 t2 on t1.Id = t2.Id and t1.Enabled = 0
Also, Table1 might only have a few records where Enabled = 1, so it would be inefficient to join millions of records first and then filter to find only 10.
I can do a sub select first, but I somehow I feel it is not the right way:
Select * from (Select * from Table1 where Enabled = 1) a left join Table2 t2 on a.Id = t2.Id
Types of Joins
Left outer join All rows from the first-named table (the "left"
table, which appears leftmost in the JOIN clause) are included.
Unmatched rows in the right table do not appear.
When you move the condition to the where then t1.Enabled = 1 is applied
Do you have actual performance problems with the first query?

SQLite SELECT with JOIN to result all relationship data

I have a db schema and I am quite not so familiar with T-SQL, I know, ORM's ruined me. Developers :)
This is the schema.
Table1 1 - 1 to three tables (Table2, Table3, Table4) and each Table(2,3,4) 1 - 1 to a Table5.
Graph (try) representation:
Table1 1<->1 Table2 1<->1 Table5
1<->1 Table3 1<->1 Table5
1<->1 Table4 1<->1 Table5
I need a query to retrieve all Table1 records and their corresponding relationship data, Table1,Table2,Table3 and their relationship data to Table5.
Any help appreciated.
Thank you.
Without seeing your full table structure and the column names you should be able to join the tables in a similar manner as this:
select *
from table1 t1
left join table2 t2
on t1.id = t2.id
left join table3 t3
on t1.id = t3.id
left join table4 t4
on t1.id = t4.id
left join table5 t5
on t2.id = t5.id
or t3.id = t5.id
or t4.id = t5.id
What I actually needed was this:
SELECT F.*, DE.Title as EnergyDrinkTitle, DE.[Picture] AS EnergyPicture, DI.Title AS InspDrinkTitle, DR.Title AS RelaxDrinkTitle
FROM
(Select M.*, E.DescriptionTitle, I.[DescriptionTitle], R.DescriptionTitle,
E.[DrinkId] as EnergyDrink, I.DrinkId as InspDrink, R.[DrinkId] as RelaxDrink
FROM [Member] M
LEFT JOIN [Energy] E
ON M.[_id] = E.[MemberId]
LEFT JOIN [Inspiration] I
ON M.[_id] = I.[MemberId]
LEFT JOIN [Relax] R
ON M.[_id] = R.[MemberId]
) AS F JOIN Drink DE ON F.EnergyDrink = DE._id
JOIN Drink DI ON F.InspDrink = DI._id
JOIN Drink DR ON F.RelaxDrink = DR._id

How to select multiple columns values same rows from single table

I have a SQL Server table. Now this table has columns like primary key Id, A, B, C, D, E, F, G
Now I want to select rows from this table like this
A=A, B=B, C=C, D=D and G > 132
So I am trying to select rows from this table which rows A,B,C,D columns has same data and G column data > 132.
So how can I do that ? Thank you.
I tried this query but returning same Id rows
SELECT TableA.Id,TableA.UserId,TableA.MaximumHp,TableA.Attack,TableA.Defense,TableA.SpAttack,TableA.SpDefense,TableA.Speed
FROM myTable as TableA
Inner Join myTable as TableB on
TableA.MaximumHp = TableB.MaximumHp
AND TableA.Attack = TableB.Attack
AND TableA.Defense = TableB.Defense
AND TableA.SpAttack = TableB.SpAttack
AND TableA.SpDefense = TableB.SpDefense
AND TableA.Speed = TableB.Speed
AND TableA.Id != TableB.Id
SQL Server 2008 R2
Sounds like you want to join the table to itself
SELECT *
FROM Table t1
Inner Join Table t2 on t1.A = t2.A
AND t1.B = t2.B
AND t1.C = t2.C
AND t1.D = t2.D
AND t1.G > 132
AND t1.ID <> t2.ID
I THINK what you mean is duplicates. Tell me if this is what you are looking for.
SELECT [Table].A, [Table].B, [Table].C, [Table].D, [Table].E, [Table].F, [Table].G
FROM [Table] LEFT JOIN (SELECT A, B, C, D FROM [Table]
GROUP BY A, B, C, D
HAVING count(*) > 1)
AS sub ON ([Table].A=sub.A) AND ([Table].B=sub.B) AND ([Table].C=sub.C) AND ([Table].D=sub.D)
WHERE G>132 and sub.A is not null;
This will give you all the rows where a,b,c, and D are equal to another row in the table...and G > 132