MSSQL statement referencing across tables - sql

So I have two tables, disciplinary and employees. Disciplinary has a column that lists an employee ID (an investigator) and an attempt to import new columns that are drawn from the employee table that yield the employee first and last name based on the existing employee ID from the disciplinary table. Below is the SQL I have so far:
SELECT d.*
, inv.firstName as investigatorFirstName
, inv.lastName as investigatorLastName
FROM det_siu_disciplinary d
LEFT OUTER JOIN cpso_employees inv ON
inv.commissionNumber = d.investigatorEmployeeID
WHERE d.isDelete = 0
This statement successfully adds the joined columns with their new names, but all columns are null. My primary concern is my SQL being flat out wrong, as it's the part of this process that I have least experience with. These statements are part of a much larger query, so if at all possible I'd prefer to not write a new query...adding contingencies would be perfect!
Anyone that assists, thank you in advance :)

the primary key column "CommissionNumber" seems unlikely to me to be the primary key of a table that should contain "EmployeeID" values, in order to join to the foreign key columns of your d table.

Related

SQL Computed Column, CountIF 2 Tables

I have two tables
tblData_VendorMasterSSPaymentTerms
tblData_VendorMasterSSPaymentTermsCLM
tblData_VendorMasterSSPaymentTerms contains a field labled VMSSPayTerms_AribaContractID which the values exist in table tblData_VendorMasterSSPaymentTermsCLM
So in table tblData_VendorMasterSSPaymentTermsCLM I want to create a calculated column that counts how many records in tblData_VendorMasterSSPaymentTerms contains the Contract ID for that record.
This is what I have put together so far but it is still coming up with an error
SELECT Count(VMSSPayTerms_AribaContractID)
From tblData_VendorMasterSSPaymentTerms
Where VMSSPayTerms_AribaContractID=VMSSPayTermsCLM_ContractID
Can someone help me identify what I am doing wrong here?
You must join the tables, group by VMSSPayTermsCLM_ContractID and count:
select
c.VMSSPayTermsCLM_ContractID,
count(t.VMSSPayTerms_AribaContractID) counter
from tblData_VendorMasterSSPaymentTermsCLM c inner join tblData_VendorMasterSSPaymentTerms t
on t.VMSSPayTerms_AribaContractID = c.VMSSPayTermsCLM_ContractID
group by c.VMSSPayTermsCLM_ContractID

Retrieve columns multiple times using SQL

In Access 2007 I have a table named Registars with a list of people and a table named Related.
Registars has a Primary key of Reg_ID and a field of Reg_Surname and a field of Reg_Forename.
Related table has a field of Reg_Person_ID and a field of Rel_Person_ID where both are primary keys (or combination key) a third field is relation_Type, i.e. cousin, sister etc.
What I am trying to write is a SQL script that will interrogate these two tables and using each record in the Related table output the ID of the first person and then their Forename then their surname then the second persons ID then thgeir forename then their surname. That is;
Reg_Person_ID Reg_Forename Reg_Surname Rel_Person_ID Reg_Forename Reg_Surname
So far what I have tried using SQL hasn't worked. Below is a screen dump of the two tables with data and the desired output.
SELECT
reg.Reg_Person
,reg.Reg_Forename
,reg.Reg_Surname
,rel.Rel_Person_ID
,rr.Reg_Forename AS Rel_Forename
,rr.Reg_Surname AS Rel_Surname
,rel.Relation_Type
FROM
Registrars reg
LEFT OUTER JOIN
Related rel
ON reg.Reg_ID = rel.Reg_Person_ID
LEFT OUTER JOIN
Registrars rr
ON rel.Rel_Person_ID = rr.Reg_ID

Database design: table with NULL keys

I'm designing a table that will hold numeric values for 2-3 situations of data:
Situation 1: has Age and Sex, along with the numeric value
Situation 2: has only Age, along with the numeric value
Situation 3: has only Sex, along with the numeric value
I don't want to create 3 different tables. Instead, only one table, with the following columns:
AgeID (references a table that contains information about the Age)
SexID (references a table that contains information about the Age)
Value (the numeric value itself)
AgeID and SexID as Foreign Keys and linked to the appropriate tables.
The problem is: my query is always doing a INNER JOIN with Age and Sex tables. For Situation 1 it works well because values are present. For Situations 2 and 3 I don't get any data, because either AgeID or SexID is null.
What solution is the correct one?
Change something in the table design?
Use Entity-Attribute-Value table to be more generic?
Use LEFT JOIN instead of INNER JOIN for all queries involving the nullable columns??
Any other idea?
Could someone clarify?
Thanks!
Yes an outer Join, Left or right, the Inner join is meant to filter out everything that doesn't have a match in both tables.
Use a conditional INNER JOIN, like
INNER JOIN Table x ON
(AgeID IS NULL OR AgeID = x.AgeID)
AND (SexID IS NULL OR SexID = x.SexID)

SQL query where data from one table = data from another

I have 2 tables: person_concern and person. They both have a code column and person has a dept column. I want to select data from person_concern table where the dept column in person table = 30.
Basically the person table has a different code for each row and then the person is put in a department. So i can have multiple rows in the person table with the same dept field.
The person_concern table is for writing problem concerns for people. It has a code to know which person its referring to which id get from the person table. So i want to select data from that person_concern table where the code matches the code from the person table and that person is from a certain dept such as 30.
Hope that makes sense... Here's what ive tried so far but get an invalid number error.
select
PERSON_CONCERNS.CODE
PERSON_CONCERNS.ENTRY_DATE
PERSON_CONCERNS.ENTRY_OPR
PERSON_CONCERNS.DISCUSSION
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30
I think you are just missing the commas on your field names but need to see the actual code you are running along with the create table statements and some sample data to be sure.
select
PERSON_CONCERNS.CODE,
PERSON_CONCERNS.ENTRY_DATE,
PERSON_CONCERNS.ENTRY_OPR,
PERSON_CONCERNS.DISCUSSION,
from PERSON_CONCERNS
inner join PERSON on PERSON_CONCERNS.CODE = PERSON.CODE
where PERSON.DEPT = 30

MS Access Selecting Related Rows

I have 2 tables with a many-to-any relationship. For the example we will call the tables "Guys" and Girls" There is a junction table that contains the related primary keys...who has dated who.
If I want to find all the girls that Guy 1 has dated, I do a select on the junction table selecting all girls with guys.ID. This give me a RecordSet. Now to find the names of the girls, I need to select from the girls table a row using the key from each RecordSet row.
Isn't there an easier way? Since I've defined the relationships in Access I would think that there must be a way to build a single query. How do I do that?
SELECT girls.name
FROM (guys
INNER JOIN junct ON guys.guyID = junct.guyID)
INNER JOIN girls ON junct.girlID = girls.girlID
WHERE guys.guyID = [whatever id you're looking for]