How to get information from table - sql

I have an sql table that has 2 columns, one is id_User from a table named applicationUsers and the other one id_Schedule from the table schedule, when i insert information on them i have to do it by the id, but also i have tha table showed on a datagridview, it shows the 2 colums correct, but how can i get for example i have in the main table 1 1, that the name of the user 1 is David and the name of the schedule is special.
How can i show the name from the user 1(David) and the name from the schedule 1(special) in the datagridview?

You would need to execute a query that joins all three tables, e.g.
SELECT u.UserName, s.ScheduleName
FROM User u INNER JOIN UserSchedule us
ON u.UserID = us.UserID INNER JOIN Schedule s
ON s.ScheduleID = us.ScheduleID
Populate a DataTable from that query and bind it to a DataGridView and you'll see the user name and schedule name in the two columns.

Related

Comparing 3 tables in an Access Database

I am trying to compare a series of tables within an access database, 2 local and one linked.
Table A (local) contains UserID, Title, Position; Table B (linked) contains UserID, Title, and Position from the previous week (records could possibly change on a week to week basis); Table C (local) contains UNIQUE UserID's and Titles.
I need to ensure that all UserID's contained in Table C still exist
in Table A.
I need to ensure that all UserID's contained in Table C have not had
a change in Title or Position from the previous week. If so Add to a temp table.
I'd prefer to use Access VBA or SQL in accomplish this task and the information will be displayed in a report.
Basically the same logic for both examples. use a left join to to identify mismatches.
Identify missing users in A
Insert into TableA (userID,Title)
select TableC.UserID, TableC.Title
from TableC
left join TableA on TableC.UserID=TableA.UserID
where TableA.UserID is null
Identify changes from B to A
insert into temp (userID,title,position)
select c.userID,c.title,c.position
from TableA a
left join tableB b on b.userid=a.userID and b.title=a.title and b.position=a.position
where b.userID is null

How to link tables correctly in SQL to add roles to staff?

Currently I have a staff table with columns:
Staff_Id, first_name, Surname.
My second table is:
Id, management_role.
When I link the tables each staff member gets added to every management role. So for example a person in first table called Jim is added three times as manager, supervisor, intern and this happens for every staff.
Some things to consider that are your ID columns are primary keys for their respective tables. If not are every value in the column is unique? Also are ids not
From your description you might be using a cross join here. The thing you need is inner join so it joins the matching id's together.
So you can do
SELECT *
FROM staff_table as st
INNER JOIN management_table as mt
ON st.Staff_Id = mt.ID

How to use a pivot in access

use three tables (TB_HOPE_INDUSTRY, TB_M_INDUSTRY, TB_PROFILE),
I want to get a red outline table.
How can I make a sql query?
Please help me.
You could create a query that uses all 3 tables, and
Join TB_PROFILE and TB_HOPE_INDUSTRY on both PROFILE_ID fields.
Join TB_M_INDUSTRY and TB_HOPE_INDUSTRY on both INDUSTRY_CD fields
SELECT the columns PROFILE_ID and NAME from table TB_PROFILE
Add the column INDUSTRY_NO from table TB_HOPE_INDUSTRY
Add the column INDUSTRY_NAME from table TB_M_INDUSTRY
Click CROSSTAB on the ribbon
Then, define the following settings fot the Function and Crosstab properties in the design grid:
group by PROFILE_ID and INDUSTRY_NO
select the First function for the columns NAME and INDUSTRY_NAME
change the name of the NAME column to something like PROFILE
Display PROFILE as row header, INDUSTRY_NO as column header, (first of) INDUSTRY_NAME as value.
Do not display PROFILE_ID (just use it for grouping)
The resulting SQL query should be something like this:
TRANSFORM First(TB_M_INDUSTRY.INDUSTRY_NAME) AS FirstOfINDUSTRY_NAME
SELECT First(TB_PROFILE.NAME) AS PROFILE
FROM TB_PROFILE INNER JOIN
(TB_M_INDUSTRY INNER JOIN TB_HOPE_INDUSTRY ON TB_M_INDUSTRY.INDUSTRY_CD = TB_HOPE_INDUSTRY.INDUSTRY_CD)
ON TB_PROFILE.PROFILE_ID = TB_HOPE_INDUSTRY.PROFILE_ID
GROUP BY TB_PROFILE.NAME
PIVOT TB_HOPE_INDUSTRY.INDUSTRY_NO;

SQL Column Comparison

I have two queries I need in one table, and haven't been able to find results in searching.
In the first table there is a UserID, which is a number, along with what the actual user id is and user's name.
My issue is that in every other query the user is only referred to as this number. I'm needing help in how to translate this UserID value into the actual User's name in my other queries.
Table with User's ID and User's Name:
SELECT *
FROM [Table].[dbo].[User]
Table example where the user is only referred to as this number, along with all other tables:
SELECT *
FROM [Table].[LoginStatus]
You need to join the two tables:
SELECT u.[UserName], l.*
FROM [LoginStatus] l
JOIN [Users] u ON u.id = l.user_id

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]