Joining 3 SQL tables without foreign keys relationships - sql

I want to join 3 SQL tables into one. 2 of them are linked by a foreign key but the 3rd isn't.
The 3 tables schemes are:
PaysSociete
Employe
Ligne
I want to join the 3 of them into a unique table called User with the following columns:
Pays: PaysSociete.LibellePaysSociete
Societe: Employe.SOCIETE
Utilisateur: Ligne.UtilisateurLigne
Matricule: Employe.Matricule
NumLigne: Ligne.Numero
EmpMetier: Employe.SectionIris
In the table Ligne, the column CodeSociete corresponds to the idcolumn in the table PaysSociete (as a foreign key)
The column Societe of the table Employe corresponds to the CodePaysSociete column in PaysSociete (but it's not a foreign key because it isn't unique)
Here is my request:
select
case
when p.PaysSocieteId=1 then 'CountryCode1'
when p.PaysSocieteId=2 then 'CountryCode2'
when p.PaysSocieteId=3 then 'CountryCode3'
end Pays,
p.CodePaysSociete Societe,
l.UtilisateurLigne Utilisateur,
e.Matricule Matricule,
l.Numero NumLigne,
e.SectionIris EmpMetier
from Ligne as l
join PaysSociete as p on l.CodeSociete=CONVERT(varchar(10), p.PaysSocieteId)
join Employe as e on e.Societe = p.CodePaysSociete
But i'm getting a bad result with duplicated User.Matricule and User.EmpMetier columns.
How can I fix it?
Please help!!!

You can join tables on any column that corresponds to an other column. A foreign key is not necessary for a join.
SELECT *
FROM Ligne
JOIN PaysSociete ON Ligne.CodeSociete = CONVERT(varchar(10), PaysSociete.id)
JOIN Employe ON Employe.Societe = PaysSociete.CodePaysSociete

Problem solved!
The column ReferentielId of Ligne handled datas from column EmployeId of Employe.
A simple junction works.
Sorry for the trouble.

Related

How to select query in oracle from 2 different tables?

I have created 2 tables as follows :
CREATE TABLE emp_bio(name VARCHAR2(15) NOT NULL ,id NUMBER, DOB DATE,PRIMARY KEY(id));
CREATE TABLE emp_sal(id NUMBER REFERENCES emp_bio(id), salary NUMBER,PRIMARY KEY(id));
Now, when I query as,
SELECT emp_sal.salary,emp_bio.name
FROM emp_sal right join emp_bio on emp_sal.id=emp_sal;
I get duplicate values in the output. Can anyone help me to remove that duplicate values !? Thank you.
You want to do a JOIN on both tables using both tables' id columns, since they're the ones relating both tables:
SELECT
eb.name,
es.salary
FROM emp_bio AS eb
INNER JOIN emp_sal AS es
ON es.id = eb.id

SQL to get data from 3 tables

I need to write an sql query to show the wing_name, sister_name and nurse_name by joining tables.
SELECT WING.WING_NAME, SISTER.SISTER_NAME, NURSE.NURSE_NAME
FROM NURSE
JOIN SISTERNURSE ON NURSE.NURSE_ID = SISTERNURSE.NURSE_ID
JOIN SISTER ON SISTER.SISTER_ID = SISTERNURSE.SISTER_ID
JOIN WING ON WING.SISTER_ID = SISTER.SISTER_ID
WHERE WING.WING_NAME = '*';
Can anyone see what's wrong with this code?
I need to pull Nurse_name from the Nurse table, which is linked to another table called Sister by the foreign key Sister_ID, this table is then linked to another table called Wing which has the foreign key Sister_ID. SisterNurse is just a bridge table with the foreign keys Nurse_id and Sister_id.
I have four values in wing_name. Would I just put WING.WING_NAME= 'SPARROW', 'LORIKEET', 'MACAW', 'KINGFISHER'; ?
Table structure:
Sister table - Sister_id (primary key), sister_name, sister_surname, sister_contactnumber, sister_salary
Wing Table - wing_id(primary key), wing_name, number_of_rooms, sister_id(foreign key)
Nurse table - Nurse_id(primary key), nurse_name, nurse_surname, nurse_contactnumber, nurse_salary
SisterNurse table - SisterNurse_ID(primary key), Sister_id(foreign key), Nurse_id(foreign key)
As you have commented on another answer below; I am adding those names in In Operator.
Please check the output:
SELECT WING.WING_NAME, SISTER.SISTER_NAME, NURSE.NURSE_NAME
FROM NURSE
JOIN SISTERNURSE ON NURSE.NURSE_ID = SISTERNURSE.NURSE_ID
JOIN SISTER ON SISTER.SISTER_ID = SISTERNURSE.SISTER_ID
JOIN WING ON WING.SISTER_ID = SISTER.SISTER_ID
WHERE WING.WING_NAME in ('SPARROW', 'LORIKEET', 'MACAW', 'KINGFISHER');
use a proper value to wing_name and all other sql queries are seems to be ok. You enter a proper value for wing_name and execute the query. Check if it is still giving the same error or not.

SQL: List all entries for a foreign key, acquired in same query

I need to make a really simple DB in an android application (I'm using SQLite) and have no idea how to make this query.
Let's say I have these two tables, linked together with the locationid from the first:
CREATE TABLE locations
(
locationid INTEGER PRIMARY KEY AUTO_INCREMENT,
floor INTEGER,
room INTEGER
);
CREATE TABLE entries
(
entryid INTEGER PRIMARY KEY AUTO_INCREMENT,
title TEXT,
summary TEXT,
date DATE,
FOREIGN KEY(location) REFERENCES locations(locationid)
);
How do I construct the following query: get all rows from table "entries" for room 308, floor 3?
SELECT *
FROM entries
WHERE locations = (SELECT locationid
FROM locations
WHERE room=308
AND floor=3)
Alternatively:
SELECT e.*
FROM entries e
JOIN locations l ON e.locations = l.locationid
WHERE l.room = 308
AND l.floor = 3
SELECT e.*
FROM locations l INNER JOIN entries e
ON l.locationid = e.location
WHERE l.floor = 3
AND l.room = 308
It's a simple JOIN statement :
select
locations.*, -- suppress this if only entries table are needed
entries.* -- you can select only date and title instead of everything
from locations
join entries
on entries.location=locations.locationid -- using your foreign key
where
locations.room=308 -- first condition
and locations.floor=3 -- second condition
;
You could use LEFT JOIN instead of JOIN if you want to retrieve informations on the room (for example) even if no data are linked within entries table.
Final word : I'm pretty sure the answer was already here on stackoverflow and on the web too.

retriveing data using join in an sql query and reprenting it in jdbc template

I have 3 tables:
REPOTRANSSMISSION TABLE column are
REPO_TRANSMISSION_ID,
G3_SESSION_ID,
CLIENT_NM,
ASSESSMENT_SESSION_ID,
PACKAGE_SESSION_ID,
TEST_SESSION_ID,
SCORE_SESSION_ID,
REPO_TRANSMISSION_STATE_CD,
REPO_TRANSMISSION_DATA_TX,
REPO_TRANSMISSION_LEVEL_CD,
CREATE_DT,
LAST_MODIFIED_DT.
here REPO_TRANSMISSION_ID is the primary key and REPO_TRANSMISSION_STATE_CD is the foregin key
2nd table REPO_ TRANSSMISSION_REQ_LOG column are
REPO_TRANSMISSION_REQ_LOG_ID
REPO_TRANSMISSION_ID
REQUEST_TX
RESPONSE_TX
ERROR_TX
CREATE_DT
LAST_MODIFIED_DT
here PK_REPO_TRANSMISSION_REQ_LOG is the primary key, REPO_TRANSMISSION_ID is foregin key
3rd tables REPO TRANSSMISSION STATE column are
REPO_TRANSMISSION_STATE_CD
REPO_TRANSMISSION_STATE_DS
CREATE_DT
LAST_MODIFIED_DT
and
REPO_TRANSSMISSION_STATE_CD values are TRANS_RESP,
RECON_REQ,
RECON_ERR,
RECON_RETRY,
RECON_RESP
here PK_REPO_TRANSMISSION_STATE_cd is the primary key
I have to retrieve the repo_transsmission_Id when the repotransmission_state_cd value is above 4 and I have to join the 1 st and 2nd table.
How I will write the sql query?
Do you just want to see how a query would look like to give you the results you need?
It would be something like this:
SELECT tr.repo_transmission_id
FROM REPOTRANSSMISSION tr
JOIN REPO_TRANSSMISSION_REQ_LOG lg ON (tr.REPO_TRANSMISSION_ID = lg.REPO_TRANSMISSION_ID)
WHERE tr.REPO_TRANSMISSION_STATE_CD > 4;

SQL Server : show foreign key constraints tied to a single record

This probably is a bit complicated but is there anyway or already a script out there that could show you all foreign key constraints tied to a single table row.
What I mean by this is say you have the following DB structure:
TABLE 1
column a
column b
TABLE 2
column c
column d (foreign key constraint to 1.a)
TABLE 3
column e
column f (foreign key constraint to 2.c)
TABLE 4
column g (foreign key constraint to 3.e)
column h
Then, you have 2 rows in Table 1. One of the rows is constrained through table 2, then further to table 3, BUT not further to table 4 (IDs tied throughout tables 1-3).
I would like to simply query one of the rows in Table 1 and have it tell me that for that row there are ties that go to Table 2, and then those rows have ties to Table 3. Using this 'query' on the second row in Table 1 would simply just return nothing as there are no foreign keys that are tying that row down.
Something like this would be immensely useful when it comes to tracking down what tables/rows are currently using a particular starting row.
Thanks!
I think what you're looking for can be accomplished by:
SELECT a, t2=COUNT(d), t3 = COUNT(f), t4 = COUNT(g)
FROM [1] LEFT JOIN [2] ON 1.a=2.d
LEFT JOIN [3] ON 2.c = 3.f
LEFT JOIN [4] ON 4.g = 3.e