Select data from multiple table - sql

I have the following:
Table1 IdTable1, IdTable2, IdTable3
Table2 IdTable2, Title
Table3 IdTable3, FName, LName
I Need the content of Table1 with the Title, FName, LName
I have tried
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.IdTable2 = T2.IdTable2
LEFT JOIN Table3 T3 ON T1.IdTable3 = T3.IdTable3
All what I get is the Table2 with the other columns NULL or the 2 columns of Table3 with the first column NULL.
Table 1 Contains
1 2 1
2 2 1
3 2 5
Table 2 Contains
1 Mr
2 Madame
Table3 Contains
1 A B
2 C D
3 E F
4 G H
5 I J
The Results of all my Queries are
Madame Null Null
Madame Null Null
Madame Null Null
or
NULL A B
NULL A B
NULL I J

Your SQL is fine as long as you want to return partial results.
Have you verified the actual data is correct? You are using a LEFT JOIN. This means rows in T1 will be returned even if there is no matching data in T2 or T3. If there are no matching rows in one of those tables, the columns in that table will be NULL.
If you include all of the columns in T1 in your result set, you will see that there is indeed data in T1, but there is no matching data in T2 or T3. In cases where T3 columns are NULL, that means there is data in T1 and T2 but not T3.
It looks to me like you've got a referential integrity problem. You may need to add foreign keys with drop constraints or fix some application logic problems when creating or deleting records.

I guess your Table1 is a pivot table for Table2 and Table3. I will do something like below.
SELECT T2.Title, T3.FName, T3.LName
FROM Table1 T1, Table2 T2, Table3 T3 WHERE T2.IdTable2 = T1.IdTable2 and T3.IdTable3 = T1.IdTable3

Related

If I left join table2 to table1, how I can I call on the IDs in table1 that don't appear in table2 within a CASE or IF Statement?

I am writing an IF/Case statement that requires me to identify all the Ids from and ID column in Table1 that don't appear in a 2nd table Table2 which is left joined on to Table1 on the ID Column, And based on that IF statement I would like to produce a binary column called Missing with 1s, 0s.
Table1
ID
Region
a
US
b
US
c
Mexico
d
Japan
Table2
ID
Years
a
5
d
10
After joining this is what I have:
ID
Region
Years
a
US
5
b
US
null
c
Mexico
null
d
Japan
10
The final outcome should be:
ID
Region
Years
Missing
a
US
5
0
b
US
null
1
c
Mexico
null
1
d
Japan
10
0
I don't know how to Identify those specific Ids in the IF or CASE statement but the rest of the query I can write. I tried to write
IF(Table1.ID NOT IN Table2.ID, 1, 0) As Missing
but that did not work (some sort of unnest issue)
You may try:
SELECT t1.*, t2.ID IS NULL AS Missing
FROM Table1 t1
LEFT JOIN Table2 t2
ON t2.ID = t1.ID;
Using the IF() function we can try:
SELECT t1.*, IF(t2.ID IS NULL, 1, 0) AS Missing
FROM Table1 t1
LEFT JOIN Table2 t2
ON t2.ID = t1.ID;

How to determine if there is a missing value in joined table

I am trying to find the "users" that are missing a value in a 2nd table with the value of column "A" = 16 and then column "B" = 0.
I am looking for these values because that would give me the ability to run a query adding a row for each user that is missing the row with the values of A = 16 and B = 0.
So here is the relevant structure of the tables that we would be joining on.
There are two tables, table 1 and table 2
Table 1
ID
parent id
table 2
table1_id
A
B
The problem I am running into is that table2 can have records associated with the table1_id but still needing to verify if the table2 if there is not a row with table1_id, A missing value 16 while B is missing value 0.
Here is the current idea I am working off of for the sql query
SELECT
*
FROM
table1
LEFT JOIN
table2 ON table1.id = table2.table1_id
WHERE
table1.id IS NOT NULL
AND table2.id IS NULL;
This will give me all the table1_ids that are missing records from table2 but does however would not pull the rows where there are rows for the table1_id but however does not determine if there are missing rows with the column A with value 16 or Column B = 0.
If you are able to answer that would be greatly appreciated. I just currently cannot think of a way I can logically create a query that would do this.
So, you want all rows from table 1
And you want rows from table 2 that are A=16, B=0
And you want to know where the relationship breaks down between table1 and table2:
SELECT t1.*
FROM
table1 t1
LEFT JOIN
(
SELECT * FROM table2 WHERE A=16 and B=0
) a16b0
ON
t1.id = a16b0.table1_id
WHERE
a16b0.table1_id IS NULL
There are more ways to skin this cat, but this should be fairly understandable in the sense of "join table1 to (just the a16/b0 rows from table2)"
Another form you might get on with uses EXISTS:
SELECT * FROM table1 t1
WHERE NOT EXISTS(
SELECT null FROM table2 t2
WHERE t2.table1_id = t1.id AND t2.A = 16 AND t2.B = 0
)
In english it's "Select all from table 1 where, for any particular t1 row, there does not exist a t2 row that has: a matching id in table1_id, a 16 in a, a 0 in b"
A slightly less popular form (historical performance reasons probably) would be perhaps:
SELECT * FROM table1 t1
WHERE id NOT IN (
SELECT table1_id FROM table2 WHERE A = 16 AND B = 0
)
"select everything from table1 where the row's id is not in the list of IDs that are a16/b0 from table 2" - in essence this forms a "big list of everything we dont want" and then says "get me everything that isn't in the list of don't-wants"
This is the solution.
SELECT
*
FROM
table1
LEFT JOIN
table1.id = table2.table1_id AND table2.A = 16 AND table2.B = 0
WHERE
table2.id IS NULL;
#jon Armstrong, thanks for the help.

How to in insert new rows from one table to another based on date?

I am having two tables (table1 and table2). I have columns Date, A, B, C in table1 and columns Date, D, E in table2. I need to transfer column D and E from table2 to table1 based on Date in both the tables.
I tried below code but getting 'multi-part identifier "table1.Date" could not be bound.' error
INSERT INTO table1
SELECT D,E FROM table2
WHERE table2.Date = table1.Date
Table1 :
Date A B C
1945-01-01 1 2 3
1945-02-01 1 2 4
1945-03-01 5 6 7
Table2 :
Date D E
1945-02-01 8 2
1945-03-01 5 6
Expected output:
Table1 :
Date A B C D E
1945-01-01 1 2 3 Null Null
1945-02-01 1 2 4 8 2
1945-03-01 5 6 7 5 6
First you have to add those columns to Table1. Then you need to update the existing rows. Something like this should work.
alter table Table1
add D int
alter table Table1
add E int
GO
update t
set D = t2.D
, E = t2.E
from Table2 t2
left join Table1 t on t.Date = t2.Date
You are rather asking of how to JOIN tables, because you can not add columns with insert statement.
Having said that, you are looking for LEFT JOIN (well, if table on the left side of operator is Table1), try this:
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.[Date] = T2.[Date]
If you want to have this as "table" and use it without JOINs, then I recommend you getting familiar with views: CREATE VIEW (Transact-SQL)

After Table Joining, need specific row value based on another Row with same ID

I have 2 tables as follows:
Table 1:
ID FName
1 Basics
2 Machine1
3 master
4 Machine2
15 Machine3
16 Machine16
Table 2:
ParentID Name InitialValue
1 Active 1
2 MachineName Entrylevel
2 Active 1
3 Active 1
4 MachineName Midlevellevel
4 Active 1
15 MachineName Endlevel
15 Active 1
16 MachineName Miscellenious
16 Active 0
Here, ID of Table 1 is referred as Parent ID at Table 2. I want "Initial Value" of Table 2 for MachineName Rows (of Table 2) provided "InitialValue" of Table 2 for Active Rows (of Table 2) are 1
Result should be like
ID InitialValue
2 Entrylevel
4 Midlevellevel
15 Endlevel
You could join the second table twice, once for MachineName, and once for Active:
SELECT t.ID, machine.InitialValue
FROM table1 t
INNER JOIN table2 machine
ON t.ID = machine.ParentId
AND machine.Name = 'MachineName'
INNER JOIN table2 active
ON t.ID = active.ParentId
AND active.Name = 'Active'
AND active.InitialValue = 1;
About Joins
The JOIN syntax allows you to link records to the previous table in your FROM list, most of the time via a relationship of foreign key - primary key. In a distant past, we used to do that with a WHERE condition, but that really is outdated syntax.
In the above query, that relationship of primary key - foreign key is expressed with t.ID = machine.ParentId in the first case. Note the alias that was defined for table2, so we can refer to it with machine.
Some extra condition(s) are added to the join condition, such as machine.Name = 'MachineName'. Those could just as well have been placed in a WHERE clause, but I like it this way.
Then the same table is joined again, this time with another alias. This time it filters the "Active" 1 records. Note that if the ID in table1 does not have a matching record with those conditions, that parent record will be excluded from the results.
So now we have the table1 records with a matching "MachineName" record and are sure there is an "Active" 1 record for it as well. This is what needs to be output.
Not sure if this is standard SQL but it should work using MySQL.
select T1.ID, T2.InitialValue
from Table1 T1 inner join Table2 T2 on T1.ID = T2.ParentId
where
T2.Name <> 'Active'
and exists (
select * from Table2 T3 where T3.ParentId = T1.ID and T3.Name = 'Active' and T3.InitialValue = 1
)
SELECT t1.ID, t2.InitialValue
FROM table1 t1 join table2 t2 on t1.ID=t2.ParentID
WHERE t2.name LIKE 'MachineName'AND t1.ID= ANY(SELECT t22.ParentID
FROM table2 t22
WHERE t22.InitialValue=1)
I think this should work
//slightly changed the condition in WHERE clausule (t2.parentID changed to t1.ID)

Search Row not exist in another table

Can anyone help me to write the query for below condition.
Table 1
ID Key
1 A
2 A
4 C
5 D
6 A
Table 2
ID Key
2 B
3 B
5 D
6 A
These are the two tables
I want a query in which the ID, which is not exist in Table1 corresponding to Table2, where deleted from table.
Example: ID = 1 row was completely deleted from Table1, and in which key are not match were also deleted
Example: ID = 2, exists in both tables but key are not same so the complete row also delete from Table1 not in Table2, I need a single query which is applicable to both condition
Thanks in advance
SELECT id, [Key]
FROM table1
EXCEPT
SELECT id, [Key]
FROM table2
You can try to check if exists in following:
DELETE FROM Table1
WHERE NOT EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id
)
OR EXISTS (
SELECT 1
FROM Table2 t2
WHERE Table1.Id = t2.Id AND Table1.[Key] <> t2.[Key]
)
OUTPUT
ID Key
5 D
6 A