LINQ join from 3 differents tables - sql

I have 3 tables
T1: id1, col1 (id1 is primary key)
T2: id2, id1, col2 (id1 here is external key from T1)
T3: id3, id1, col3 (id external key of T1)
My question is simple: how do I get a join list of all the columns of the 3 tables using LINQ?

I figure out how to join results of several tables, this is an example, you can join as many tables as wished.
var a = (from s in _DBEntities.Services
join d in _DBEntities.Departements on s.se_id_departement equals d.id_departement
join i in _DBEntities.SsCategoriesServices on s.se_id_sscategorieService equals i.id_SsCategorieService
select new Services
{
IdService = s.id_service,
Adresse = s.adresseService,
NomSousCategory = i.libelleSsCategorieService,
NomDepartement = d.nom,
}).ToList();

Related

3 tables (1 bridge) select where table 3 does NOT have an entry in table 1

That was a hard question to phrase. Let me clarify
Table 1: ID1
Table 2: ID1, ID2
Table 3: ID2
the ID is NOT a key, and can be NULL. Each table represents a station, so in the world, they enter in an ID1 of 19 at table (station) 2, then and ID2 there as well. It is then entered in at table (station) 3.
For whatever reason they did not enter anything into table 1...maybe the line was going too fast.
Table 2 Should contain and entry to tie together each ID1 and and 2
ID1 = 19 -Table 1
ID2 = 29 -Table 3
Table 2 should then contain an entry:
ID1 19, ID2 29
However, this is not always the case. sometimes ids are 'lost' in transit, like in the above example.
I would like to find a way that I could see if I could see any entries in table 3 that do not have a corresponding entry in table 1. so the result would look like:
ID1 = NULL ID2 = 19 ID3 = 29
You can simply use a LEFT OUTER JOIN paired with a WHERE <value> IS NULL.
SELECT ID
FROM table_3 as three
INNER JOIN table_2 as two
ON two.ID2 = three.ID2
LEFT OUTER JOIN table_1 as one
ON one.ID = two.ID1
WHERE one.ID IS NULL
AND three.ID IS NOT NULL ;
Based on your comment:
I would like to find a way that I could see if I could see any entries in table 3 that do not have a corresponding entry in table 1
I feel as if you're eluding to being able to do the following:
SELECT * FROM table3 t3
LEFT JOIN table1 t1 ON t3.id=t1.id
WHERE t1.ID IS NULL
This is a pretty standard technique to find x missing from table y. Its also a common interview question.
You could also do this:
SELECT *
FROM table3 t3
WHERE NOT EXISTS
(
SELECT id
FROM table1
WHERE id = t3.id
)

How to make sql query using two tables

I have 3 tables T1,T2 and T3. Table T1 contains 2 columns (key,class/student). The column class/student contains both classes and students, for example: 'english', 'math', 'mark', 'tom'... Table T2 contains 2 columns (class,student). Each class have more than one student in it, and these 2 columns use the keys from T1. In Table T3 I want to insert a specific class with its student(s) - class(es) into column A and student(s) into column B. Knowing that these columns use the keys from T1 table I've tried this but it returns same specific class with its students multiple times:
INSERT INTO T3 (A,B)
SELECT m.class, m.student
FROM T1 b,T2 m
WHERE m.class = (SELECT key FROM T1 WHERE class/student='English')
AND b.KEY = m.student;
the result i get: 1 is id of class english ,10 is id of student mark, 11 is id of student tom
table T1:
Table T2:
Since your master table T1 has both class and student details, I would recommend joining it twice with the class_student_map table T2 to get the details:
INSERT INTO T3 (A,B)
SELECT "class_master"."key", "student_master"."key"
FROM
T2 "class_student_map"
INNER JOIN
T1 "class_master"
ON
"class_master"."key" = "class_student_map"."class"
AND
"class_master"."class/student" = 'English'
INNER JOIN
T1 "student_master"
"student_master"."key" = "class_student_map"."student";
Here you have simple code and just one join operation, which positively impacts on performance:
insert into t3
WITH spec_class AS
(select key from T1 where class_student='English')
SELECT m.class, m.student
FROM T2 m inner join spec_class sp on m.class=sp.key;

SQL to search and delete with several keycolumns

A bit bad title maybe but I try to explain what I mean.
I use SQL like this to build a list
SELECT id
FROM parcel
WHERE id IN (113715, 113824, 113855, 113954, 114010, 114116, 114272, 114329)
where ID is a column in parcel table that is quarantined to be unique, very straightforward.
But some tables use many columns to be unique.
SELECT id1, id2
FROM trip
WHERE id1, id2 IN ((113715, 113824), (113855, 113954), (114010, 114116),(114272, 114329))
The last SQL is obviously not working.
I want to select rows where
id1 = 113715 and id2=113824 or
id1 = 113855 and id2=113954 or
id1 = 114010 and id2=114116 or
id1 = 114272 and id2=114329
In reality the generated SQL can contain 500 - 1000 ids.
What SQL should I use ?
EDIT
This is a generated SQL. The ID's come from another database on another server so JOIN for example is not possible.
SELECT id1, id2 FROM trip
INTERSECT
select id1, id2 from (values (113715,113824),(113855,113954),(114010,114116),(114272,114329)) a(id1,id2)
USE INTERSECT
You could use a join with a values list:
SELECT id1, id2
FROM trip
INNER JOIN (VALUES (113715,113824),(113855,113954),(114010,114116),(114272,114329)) V(a, b)
ON id1 = a
AND id2 = b
Can you use the generated ids like this?
;WITH GenerateIds AS
(
SELECT * FROM (VALUES
-- Paste the Id list below
(113715,113824),(113855,113954),(114010,114116),(114272,114329)
) t(id1, id2)
)
-- Use inner join
SELECT id1, id2 FROM trip INNER JOIN GenerateIds g ON
trip.id1 = g.id1 AND trip.id2 = g.id2
Unfortunately SQL Server doesn't support tuples in IN like
SELECT id1, id2
FROM trip
WHERE (id1,id2) in ((113715,113824),(113855,113954),(114010,114116),(114272,114329));
which is valid in some other DBMS.
So you can either follow your own suggestion
SELECT id1, id2
FROM trip
WHERE (id1 = 113715 and id2 = 113824)
OR (id1 = 113855 and id2 = 113954)
OR (id1 = 114010 and id2 = 114116)
...;
Or use a temporary table, cte or the like. Easiest might be to join a values clause:
SELECT trip.id1, trip.id2
FROM trip
JOIN (VALUES (113715,113824),(113855,113954),(114010,114116),(114272,114329))
AS src(id1, id2) ON src.id1 = trip.id1 AND src.id2 = trip.id2;
Another Simple Query:
SELECT id1, id2
FROM trip
WHERE 1 = 1
AND ((id1 = 113715 and id2=113824)
OR (id1 = 113855 and id2=113954)
OR (id1 = 114010 and id2=114116) OR (id1 = 114272 and id2=114329))

How to select columns from two different tables, which meet conditions based on third table

this is my problem (in MS SQL SMS 2008 R2):
I have two tables:
table1 with columns id1 name1
table2 with columns id2 name2
I also have third temporary table made from imported xls with columns name1, name2
what i want is to have select that will result in something like copy of that temporary table but with id1, id2 instead of name1, name2.
Is that even possible?
you need to use left join with two tables
select id1, id2
from tempTable T
left join table1 T1
on T.name1 = T1.name1
left join table2 T2
on T.name2 = T2.name2

SQL combine two tables into one table and add a new column

I need to combine two tables into one. Ans also, add a column (assign an int value) to the new table on SQL. So that the rows from table1 and ones from table2 are assigned with different values.
Example,
table1
ID1 ID2 ID3 VALUE
table2
ID1 ID2 ID3 VALUE
table3
ID1 ID2 ID3 VALUE
i need to combine table3 and table2 into a new table and add a new column
table_new
top_id ID2 ID3 VALUE
It is Netezza SQL.
INSERT INTO new_table
SELECT *
FROM
(
SELECT * , **sum (table1.VALUE * table2.VALUE) AS new_value**
FROM table1
JOIN
table2
ON table1.id1 = table2.id1
GROUP BY table2.id2, table2.id3
) AS tt_a # here, I need to add a new column to tt, call it as top_id and also assign an int
# value to it, such as 80
UNION ALL
SELECT *
FROM
(
SELECT * , **sum (table1.VALUE * table3.VALUE) AS new_value**
FROM table1
JOIN
table3
ON table1.id1 = table3.id1
GROUP BY table3.id2, table3.id3
) AS tt_b # here, I need to add a new column to tt, call it as top_id and also assign an
# int value to it, such as 81
**ORDER BY top_id**
I got error:
I em new to SQL.
ERROR [HY000] ERROR: 0 : Functionality not implemented
Any help would be appreciated.
It's not entirely clear what you want, but I'm going to assume it's a UNION ALL example.
UNION ALL will combine the result sets of 2 or more SELECT statements. It returns all rows from each select statement (even if the row exists in more than one of the SELECT statements) as 1 result set.
example:
select '81' as top_id,id2,id3 from T1 group by id2,id3
UNION ALL
select '79' as top_id,id3,id400 from T1 group by id3, id400
UNION ALL
SELECT '80' as top_id,id2,id3
FROM table1
JOIN
table2
ON table1.id1 = table2.id1
GROUP BY table1.id2, table2.id3
;
Each select statement (this example combines 3) should be a valid query all by itself and should return the same amount of columns with the same datatypes.
reference: http://www.techonthenet.com/sql/union_all.php