I am new to this, so I apologise if I am not completely clear...
Background:
Ten staff members, all with the role Teacher assigned to them.
The StaffDetails table has 6 columns - StaffID, StaffName, DateOfBirth, Gender, HolidayDaysRemaining & StaffRole
I would like the teachers to see all of their own details, but only the StaffID, StaffName and StaffRole columns of other members of staff.
The code I already have is explained below:
Firstly, a view that shows the three columns that all staff members can see (StaffID, StaffName, StaffRole):
CREATE VIEW StaffDetailsForStaffMutual AS
SELECT sd.StaffID, sd.StaffName, sd.StaffRole
FROM StaffDetails sd;
Secondly, a view that will hold the columns DateOfBirth, Gender, and HolidayDaysRemaining. These will only be viewable by the person logged in (as shown by sub-query). The StaffID attribute is only included so that a common column is in both views - it will be present only once in the final table.
CREATE VIEW StaffXDetailsForStaffX AS
SELECT sd.StaffID, sd.DateOfBirth, sd.Gender, sd.HolidayDaysRemaining
FROM StaffDetails sd
WHERE StaffID IN
(SELECT USER
FROM DUAL);
My problem is, is that I cannot find a way to join these two views to make a single table. In a perfect world, if a staff member with ID 301 logged on and queried the whole of the table, it would display six columns, the first row would have all of their data in, the second row would have three columns of StaffID 302's data in and so on and so forth.
Any help here would be greatly appreciated!
(Finally, just to show that I've had a go, I tried this query:
CREATE VIEW StaffDetailsForStaff AS
SELECT sdfsm.StaffID, sdfsm.StaffName, sdfsm.StaffRole,
sxdfsx.DateOfBirth, sxdfsx.Gender, sxdfsx.HolidayDaysRemaining
FROM StaffDetailsForStaffMutual sdfsm, StaffXDetailsForStaffX sxdfsx
WHERE sdfsm.StaffID = sxdfsx.StaffID;
but when logged on as 301 and queried, it showed only 301's data and no-one else's!)
You probably need an outer join
CREATE VIEW StaffDetailsForStaff AS
SELECT sdfsm.StaffID, sdfsm.StaffName, sdfsm.StaffRole,
sxdfsx.DateOfBirth, sxdfsx.Gender, sxdfsx.HolidayDaysRemaining
FROM StaffDetailsForStaffMutual sdfsm LEFT JOIN StaffXDetailsForStaffX sxdfsx
ON sdfsm.StaffID = sxdfsx.StaffID;
Related
To create a many-to-many relationship, I have three tables:
tblEmployee, contains employees
tlkpPermission, contains 11 different possible permission groups an employee can be part of
tblEmployeeXPermission, combines the EmployeeID with one or more PermissionID
What I’m trying to create is a query that shows what permission groups an employee is NOT part of.
So, if EmployeeID 12345 is associated with PermissionID 1,2,3,4,5, but NOT 6,7,8,9,10,11 (in the EmployeeXPermission table) then I want the query to show EmployeeID 12345 is not part of PermissionID 6,7,8,9,10,11.
Of all the JOINs and query options, I can only get a query to show which PermissionIDs an employee is associated with, but not the PermissionIDs the employee is not associated with.
Any help would be appreciated.
Thanks
You need to start with all combinations of employees and permissions, and this type of join is CROSS JOIN, but MsAccess SQL does not have it in the new SQL syntax. You can use the old syntax of listing your tables in the FROM clause, comma separated, and provide the join condition, if any, in the WHERE clause:
SELECT
EmployeeId,
PermissionID
FROM
tblEmployee as E,
tlkpPermission as P
where not exists (
select 1
from tblEmployeeXPermission X
where X.EmployeeId=E.EmployeeId
and X.PermissionId=P.PermissionId
)
Here the part up to the WHERE clause would give you all employee - permission combinations, and the WHERE clause removes those occuring in the tblEmployeeXPermission, leaving you with the ones you want.
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
I am in the process of creating an organizational charts for my company, and to create the chart, the data must have a unique role identifier, and a unique 'reports to role' identifier for each line. Unfortunately my data is not playing ball and it out of my scope to change the source.
I have two source tables, simplified in the image below. It is important to note a couple of things in the data.
An employees manager in the query needs to come from the [EmpData] table. The 'ReportsTo' field is only in the [Role] table to be used when a role is vacant
Any number of employees can hold the same role, but for simplicity lets assume that there will only ever be one person in the 'Reports to' role
Using this sample data, my query is as follows:
/**Join Role table with employee data table.
/**Right join so roles with more than one employee will generate a row each
SELECT [Role].RoleId As PositionId
,[EmpData].ReportsToRole As ReportsToPosition
,[Role].RoleTitle
,[Empdata].EmployeeName
FROM [Role]
RIGHT JOIN [EmpData] ON [Role].RoleId=[EmpData].[Role]
UNION
/** Output all roles that do not have a holder, 'VACANT' in employee name.
SELECT [Role].RoleId
,[Role].ReportsToRole
,[Role].RoleTitle
,'VACANT'
FROM [Role]
WHERE [Role].RoleID NOT IN (SELECT RoleID from [empdata])
This almost creates the intended output, but each operator roles has 'OPER', in the PositionId column.
For the charting software to work, each position must have a unique identifier.
Any thoughts on how to achieve this outcome? I'm specifically chasing the appended -01, -02, -03 etc. highlighted yellow in the Desired Query Output.
If you are using T-SQL, you should look into using the ROW_NUMBER operator with the PARTITON BY command and combining the column with your existing column.
Specifically, you would add a column to your select of ROW_NUMBER () OVER (PARTITION BY PositionID ORDER BY ReportsToPosition,EmployeeName) AS SeqNum
I would add that to your first query, and then, in your second, I would do something like SELECT PositionID + CASE SeqNum WHEN 1 THEN "" ELSE "-"+CAST(SeqNum AS VarChar(100)),...
There are multiple ways to do this, but this will leave out the individual ones that don't need a "-1" and only add it to the rest. The major difference between this and your scheme is it doesn't contain the "0" pad on the left, which is easy to do, nor would the first "OPER" be "OPER-1", they would simply be "OPER", but this can also be worked around.
Hopefully this gets you what you need!
CREATE TABLE users
()
CREATE TABLE avatars
()
CREATE TABLE weapons
()
I need two queries for these tables that I created and I am having trouble doing it, any help would be greatly appreciated.
For all Avatars produce a list showing the avatar name, the total number of weapons it currently holds and the total cost of all weapons currently held. I want no more extra information shown apart from these three main pieces, so I need to get information from table 2 and 3.
For any given User email (i.e. entered at runtime) list the owned Avatar names in reverse order of date of birth and the weapons (including weapon name, range and cost) held by that Avatar.
Any help would be greatly appreciated. I am using SQL*Plus. The tables are not very efficient so any improvements would be welcomed but mainly I need help with these queries.
select
max(a.name),
count(1),
sum(w.cost)
from
avatars a
inner join weapons w on w.weapon_id=a.no_wp_id
group by
a.no_wp_id
;
select
a.name,
w.weapon_name,
w.range,
w.cost
from
users u
inner join avatars a on a.user_id=u.id
inner join weapons w on w.weapon_id=a.no_wp_id
where
u.email='given#email.com'
order by
a.dateOfBirth desc
;
If your queries depend on email being unique, you should have a unique key on it.
Your weapons table does not appear to have a range column on it, but you implied it does in your requirement for query #2. I've included w.range anyway.
In avatars, no_wp_id should probably be named something like avatar_id or just id, and in weapons, weapon_id should be named avatar_id.
You should go through an SQL tutorial. Here's one: http://www.tutorialspoint.com/sql/sql-overview.htm.
I'm currently working on a personal project and I could use a little help. Here's the scenario:
I'm creating a database (MS Access) for all of the movies myself and some friends have ever watched. We rated all of our movies on IMDb and used the export feature to get all of the movie data and our movie ratings. I plan on doing some summary analysis on Excel. One thing I am interested in is the most common movie genre that each person watched. Below is my current scenario. Note that the column "const" is the movies' unique IDs. I also have individual tables for each person's ratings and the following tables are the summary tables that make up the combination of all the movies we have watched.
Here's the table I had: http://imgur.com/v5x9Dhg
I assigned each genre an ID, like this: http://imgur.com/aXdr9XI
And here is a table where I have separate instances for each movie ID and a unique genre: http://imgur.com/N0wULo8
I want to find a way to count up all of the genres that each person watches. Any advice? I would love to provide any additional information that you need!
Thank you!
You need to have at least one table which has one row per user and const (movie watched). In the 3 example tables you posted nothing shows who watched which movies, which is information you need to solve your problem. You mention having "individual tables for each person's ratings," so I assume you have that information. You will want to combine all of them though, into a table called PERSON_MOVIE or something of the like.
So let's say your second table is called GENRE and its columns are ID, Genre.
Let's say your third table is called GENRE_MOVIE and its columns are Const and ID (ID corresponds to ID on the GENRE table)
Let's say the fourth table, which you did not post, but which is required, is called PERSON_MOVIE and its columns are person, Const, rating.
You could then write a query like this:
select vw1.*, ge.genre
from (select um.person, gm.id as genre_id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) vw1
inner join (select person, max(num_of_genre) as high_count
from (select um.person, gm.id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) x
group by person) vw2
on vw1.person = vw2.person
and vw1.num_of_genre = vw2.high_count
inner join genre ge
on vw1.genre_id = ge.id
Edit re: your comment:
So right now you have multiple tables reflecting people's ratings of movies. You need to combine those into a table called PERSON_MOVIE or something similar (as in example above).
There will be 3 columns on the table: person, const, rating
I'm not sure if access supports the traditional create table as select query but ordinarily you would be able to construct such a table in the following way:
create table person_movie as
select 'Bob', const, [You rated]
from ratings_by_bob
union all
select 'Sally', const, [You rated]
from ratings_by_sally
union all
select 'Jack', const, [You rated]
from ratings_by_jack
....
If not, just combine the tables manually and add a third column as shown indicating what users are reflected by each row. Then you can run my initial query.