SQL to get data from 3 tables - sql

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.

Related

Oracle SQL - need to flip values, but don't know how

select kasutaja_nimi, eesnimi, perenimi, r_nimetus, seeria_nr, max(paigalduse_aeg) as paigaldus
from kasutaja ka
right join riistvara ri on ka.id = ri.id
right join r_paigaldus r on ka.id = r.kasutaja_id
group by kasutaja_nimi, eesnimi, perenimi, seeria_nr, r_nimetus;
This is the output, but I need those values changed. I have ID primary keys for both - kasutaja and riistvara, but I don't know how to match kasutaja ID 1 with riistvara ID 2 and vice versa.
And the output should be like this:
The R_NIMETUS and SEERIA_NR fields are different on my output what I get with my code.
you can't cross the data because you haven't a logic link between two tables.
You have only one solution, you must change your table structure:
Scenario 1
If you have a parent - child relation, please add a foreign key on child table
Scanerio 2
If you have a n:m relationship, please create a middle table with fks to parent and child table.
So in your query you can use JOIN operations to show correctly your results

SQL Query Schema and Data into One Row from 2 Databases

We have data that was merged by accident in our production site, but we still have the data separated in our test site/database. I'd like to be able to query customer data from both databases to compare based on a customer's uniqueidentifer. What I'd like to see in the query results is the schema tables, columns, and primary uniqueidentifier keys of tables where the table has a matching foreign column containing the customer's key.
For instance, if the customer has an invoice the customer cst_key would be in the inv_cst_key of the ac_invoice. I need the primary key of that table, which would come from the inv_key column of that row. So if the customer had two invoices, two inv_key's would list as separate rows.
I installed ApexSQL Search to search the database uniqueidentifier columns for the customer key and it provided the table and foreign columns where the customer's cst_key existed (e.g.: inv_cst_key), but I still need the primary key (e.g.: inv_key) of that table row which the customer key resides. I tried using those search results to build something with excel (piecing tables, operators, columns, etc.) and copy/paste it to SSMS, but the query pulls millions of results the way it's setup...
DECLARE #cstkey uniqueidentifier
SET #cstkey = 'xxxxxxxxxxxxxxxxxx'
SELECT cst_key,
inv_key,
-- many more columns
FROM co_customer
LEFT JOIN ac_invoice ON cst_key = inv_cst_key
-- more LEFT JOINS
WHERE cst_key = #cstkey
Also, I know how to query data from 2 databases, but I don't know how to query so that I can see table and column names in a row next to the column's data.
DECLARE #cstkey uniqueidentifier
SET #cstkey = 'xxxxxxxxxxxxxxxxxx'
SELECT cst1.cst_key AS cst_key_1, inv1.inv_key AS inv_key_1,
cst2.cst_key AS cst_key_2, inv2.inv_key AS inv_key_2
FROM db1name.dbo.co_customer cst1
LEFT JOIN db1name.dbo.ac_invoice inv1 (NOLOCK) ON inv1.inv_cst_key = cst1.cst_key
INNER JOIN db2name.dbo.co_customer cst2 (NOLOCK) ON cst1.cst_key = cst2.cst_key
INNER JOIN db2name.dbo.ac_invoice inv2 (NOLOCK) ON inv2.inv_cst_key = cst2.cst_key
WHERE cst1.cst_key = #cstkey
AND cst2.cst_key = #cstkey
I'd like the results to look something like this...
DB1 | T1 | PC1 | PC1 Data Key | FC1 | FC1 Data Key || DB2 | T2 | ...
--------------------------------------------------------------------
DB = Database, T= Table, PC = Primary Column, FC = Foreign Column
Btw, the FC1 Data Key would also be the cst_key as mentioned above.
Thanks in advance for any assistance.

Joining 3 SQL tables without foreign keys relationships

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.

How do I delete record through a Join

I have an Access database where I wish to delete a record from a table using its referential entigrity to another table. For example I have the following two tables;
CI_Aliases with fields - CI_Ref (Primary Key) with a value of 3
and Aliase_ID (Foreign Key) with a value of 5
Aliases_Table with fields - Aliase_ID (Primary Key) with a value of 5
and Aliase with a value of "AMSS"
I have tried the following DELETE statement but I get the message "Cannot delete records from the specified table" - what am I doing wrong?
DELETE FROM Aliases_Table a
INNER JOIN CI_Aliases c
ON a.Aliase_ID = c.Aliase_ID
WHERE c.CI_Ref = 3
I should confirm that it is the record in the Aliases_Table i wish to delete but using the CI_Aliase primary key of "3"
Use the alias first, then the FROM clause. The syntax is a bit unintuitive
DELETE a.*
FROM Aliases_Table a
WHERE a.Aliase_ID IN (
SELECT
c.Aliase_ID
FROM CI_Aliases c
WHERE c.CI_Ref = 3
)

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