SQL query where data from one table = data from another - sql

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

Related

SQL Query Involving Data From Different Tables

I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!
This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';

New report from two tables with reportviewer doesnt have true result

I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]

MSSQL statement referencing across tables

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.

Update Table Column From Another Table?

So I have two tables. Say one table has a list of students, a student ID for each student, and a home address for each student.
Then you have another table that has a subset of the students in the first able, (and they are in a completely different order) with updated addresses and a student ID.
I need a query that can match the student ID of the two tables, and thereby update the address from the first table, using what is in the second table.
This is the query I tried, but no luck:
UPDATE Roster, UpdatedRoster
SET Roster.Address = (SELECT Address FROM UpdatedRoster WHERE Roster.StudentID = UpdatedRoster.StudentiD)
WHERE Roster.StudentID = UpdatedRoster.StudentiD
Any help here would be greatly appreciated.
Update: This is on Microsoft Access FWIW.
UPDATE Roster
SET Roster.Address = UpdatedRoster.Address
FROM Roster, UpdatedRoster
WHERE Roster.StudentID = UpdatedRoster.StudentiD

SQL Cross-Table Referencing

Okay, so I've got two tables. One table (table 1) contains a column Books_Owned_ID which stores a series of numbers in the form of 1,3,7. I have another table (table 2) which stores the Book names in one column and the book ID in another column.
What I want to do is create an SQL code which will take the numbers from Books_Owned_IDand display the names of those books in a new column. Like so:
|New Column |
Book 1 Name
Book 2 Name
Book 3 Name
I can't wrap my head around this, it's simple enough but all the threads I look on get really confusing.
Table1 contains the following columns:
|First_Name| Last_Name| Books_Owned_ID |
Table2 contains the following columns:
|Book_Name|Book_ID|
You need to do an inner join. This is a great example/reference for these
SELECT Book_Name FROM Table2
INNER JOIN Table1
ON Table1.Books_Owned_ID = Table2.Book_ID
EDIT SQL Fiddle
I will work on getting the column comma split working. It wont be a lot extra for this.
EDIT 2 See this answer to build a function to split your string. Then you can do this:
SELECT Book_Name FROM Table2
WHERE Book_ID IN(SELECT FN_ListToTable(',',Table1.Books_Owned_ID) FROM Table1 s)
The core of this centers around data normalisation... Each fact is stored only once (and so is "authoritative"). You should also get into the habit of only storing a single fact in any field.
So, imagine the following table layouts...
Books
Id, Name, Description
Users
Id, Username, EmailAddress, PasswordHash, etc....
BooksOwned
UserId, BookId
So if a single user owns multiple books, there will be multiple entries in the BooksOwned table...
UserId, BookID
1, 1
1, 2
1, 3
Indicates that User 1 owns books 1 through 3.
The reason to do it this way is that it makes it much easier to query in future. You also treat BookId as an Integer instead of a string containing a list - so you don't need to worry about string manipulation to do your query.
The following would return the name of all books owned by the user with Id = 1
SELECT Books.Name
FROM BooksOwned
INNER JOIN Books
ON BooksOwned.BookId = Books.Id
WHERE BooksOwned.UserId = 1
You need a function which takes a comma separated list and returns a table. This is slow and fundamentally a bad idea. Really all this does is convert this way of doing it to be like the data model I describe below. (see ProfessionalAmateur's answer for an example of this).
If you are just starting change your data model. Make a linking table. Like this:
Okay, so I've got two tables. One table (table 1) contains a column Books_Owned_ID which stores a series of numbers in the form of 1,3,7. I have another table (table 2) which stores the Book names in one column and the book ID in another column.
What I want to do is create an SQL code which will take the numbers from Books_Owned_IDand display the names of those books in a new column. Like so:
Person Table
|First_Name| Last_Name| Person_ID |
Book Table
|Book_Name|Book_ID|
PersonBook Table
|PersonID|BookID|
This table can have more than one row for each person.