SQL Join on 2 Tables - sql

i've been trying to build this query. hoping someone can help.
I have 2 tables.
1 table contains
Code | name | Value | Period
1 name1 1 2010
2 name2 2 2010
table 2 contains
code | name |
1 name1
2 name2
3 name3
4 name4
what i want to be displayed is
1 name1 1
2 namw2 2
3 name3 0
4 name4 0
In some instances table 1 may have a value for all name variables in table 2
but where there are only 1,2,3 names i want it to display the other one but with a value of 0 or blank.

Try this:
select
T2.*,
isnull(T1.code, 0) as code -- or value
from
table2 T2
left outer join table1 T1 on T1.name = T2.name
You can replace isnull(T1.code, 0) as code with isnull(T1.value, 0) as value. I'm not sure what you're after ...

Related

Select records from a table where two columns are not present in another table

I have Table1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
and a Table2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
I want to get the following Table,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
I.e get the rows from the first table where the ID+Program is not in second table. I am using MS SQL Server express 2012 and I don't want to add any columns to the original databases. Is it possible to do without creating temporary variables?
Several ways to do this, here's one using not exists:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
One possible variant is to use LEFT JOIN:
SELECT
Table1.*
FROM
Table1
LEFT JOIN Table2
ON Table1.ID = Table2.ID
AND Table1.Program = Table2.Program
WHERE
Table2.ID IS NULL

multiply several tables in big one

There is a task about sql.
I have tree tables
Table_1
Id Name
1 Name1
2 Name2
Table_2
Id Name
3 Name3
4 Name4
And final table
Table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
So i would like two more rows to be generated for table_3
Id Table1_Id Table2_Id Value
1 1 3 Some Text
2 1 4 Some another text
null 2 3 null
null 2 4 null
How can i do this ?
In fact, i have more then 5 tables with same signature as Table_1 and Table_2.
And each time i add one row to the one of this table, the result Table_3's values count should be multiplied.
Is Table_3 created from a JOIN of Table_1 and Table_2?
If so what JOIN are you using?
Have you tried CROSS APPLY or OUTER JOIN?

SQL query to remove duplicate column values within distinct rows

I am using MS SQL, I have tables "Table1" , "Table2" , "Table3" as mentioned below(might look like a mess, I dont know how to make a table better here).
I want the "Expected OutPut" as mentioned ant the bottom.
Want a SQL script to perform this action.
Table 1
FamilyID FamilyName
1 One
2 Two
3 Three
Table2
FamilyID UserID UserName
1 1 Name1
1 2 Name2
1 3 Name3
2 4 Name4
2 5 Name5
2 6 Name6
2 7 Name7
2 8 Name8
3 9 Name9
3 10 Name10
Table3
FamilyID RoomID RoomType
1 1 RoomType1
1 2 RoomType2
2 1 RoomType1
2 2 RoomType2
2 3 RoomType3
2 2 RoomType2
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
2 Name4 RoomType1
Name5 RoomType2
Name6 RoomType3
Name7
Name8
3 Name9 RoomType2
Name10
EDIT:
I tried how to show column value only one time if it is repeated and blank until different value comes in sql but the out put is not as i expected. It has duplications as sample shown below for FamilyID=1
Result from how to show column value only one time if it is repeated and blank until different value comes in sql
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType1
Name3 RoomType1
Name1 RoomType2
Name2 RoomType2
Name3 RoomType2
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
You can use a solution like this:
SELECT T1.FamilyID,T2.UserName,T3RoomType FROM Table1 T1
JOIN Table2 T2 ON T1.FamilyID =T2.FamilyID
LEFT JOIN Table3 T3 ON T2.UserID =T3.RoomID
GROUP BY T2.UserID

SQL JOIN WHERE IN STRING OF OTHER TABLE

I need to join two tables together where the seccond table has one of the first tables in a string.
e.g
Table1 has ID1, ID2, Lang_id, User, Text.
Table2 has ID_a, User, List_ID2.
Additionally, the List_ID2 is comma seperated, so I have to make sure I get it in all cases, so if it is the only number, or the at beginning, middle or end of the string. BUT NOT truncate the values, i.e. (10 is not 100 - '10'0)
The extra kicker is that Lang_id is the language of the Place and I also have to seperate that.
I have the language seperated fine, I can't get the ID_a into my results
i.e
TABLE 1
ID1 ID2 Lang_id User Text
1 2 1 bob Me
1 2 2 bob Mich
1 2 3 bob Mi
2 1 1 bob You
2 1 2 bob Du
2 1 3 bob usted
3 1 1 tim You
3 1 2 tim Dich
3 1 3 tim le
4 3 1 tim Hello
4 3 2 tim Hallo
4 3 3 tim ihola
TABLE 2
ID_a User List_ID2
100 bob 1, 2
200 tim 1, 3
RESULTS
ID_a ID1 ID2 English German Spanish
100 1 1 You Du usted
100 2 2 Me Mich Mi
200 3 1 You Dich le
200 4 3 Hello Hallo ihola
My statement looks a little like this:
SELECT DISTINCT main.ID1, main.ID2, ID_a
(SELECT Text
FROM table1 AS a
WHERE lang_id = 1
AND main.ID1 = a.ID1
AND main.ID2 = a.ID2) AS English,
(SELECT Text
FROM table1 AS b
WHERE lang_id = 2
AND main.ID1 = b.ID1
AND main.ID2 = b.ID2) AS German,
(SELECT Text
FROM table1 AS c
WHERE lang_id = 3
AND main.ID1 = c.ID1
AND main.ID2 = c.ID2) AS Spanish,
FROM table1 AS main
LEFT OUTER JOIN table2 ON table2.User = main.User
AND (table2.List_ID2 LIKE STR(ID2)
OR table2.List_ID2 LIKE (ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2 + ',%')
OR table2.List_ID2 LIKE ('%,' + ID2)
The first part is working fine (the language is seperated), but I can't get the ID from table2, I've tried a few differnt methods, and the above comes back without an error, but alot of NULL values.
Found the answer here:
SQL STR() function equality
I needed to specify the Length of the STR()

Select Row from Other Table if Doesn't Exist in Current Table

I have two tables:
Table1
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
Table2
id name qty
1 Tedd 2
2 Jim 2
3 Sally 2
4 Victoria 1
5 Alex 9
I need to select all the rows from Table1. However, if a row exists in Table2 that doesn't exist in Table1, I need to include that in the result set. So, in the end, my query should return this:
id name qty
1 Tedd 6
2 Jim 7
3 Sally 8
4 Victoria 1
5 Alex 9
Is there a way I can do this? Thanks.
You can use a FULL OUTER JOIN:
select
coalesce(t1.id, t2.id) id,
coalesce(t1.name, t2.name) name,
coalesce(t1.qty, t2.id) qty
from table1 t1
full outer join table2 t2
on t1.id = t2.id
See SQL Fiddle with Demo