Staff Table
Payment Table
Here I can't upload the availability table. It consists of Availability_ID,Staff_ID and Shift_ID columns.
I want to Create a view which shows all details of all staff and their availability details. The view should include the last name and first name of the staff member, the full name of their mentor (if any), their hourly salary and the days and times they are available for work? How can I create?
Try this query, you will get all the columns of your three tables.
SELECT * FROM Staff Inner Join Payment ON Staff.Payment_Id = Payment.Payment_Id Innjer Join Availability on Availability.Staff_Id = Staff.StaffId
Related
I have two tables:
A Billing table, and a Customer table.
The Billing table and customer table both share a common attribute of Customer Number.
Billing Table
I'm trying to create a view that will retrieve the customer code and bill number for the most recent invoice date. I'm having trouble ordering my query.
This is what I have so far.
CREATE VIEW RECENT_ORDER
AS
SELECT
c.Customer_Num, b.Bill_Num
FROM CUSTOMER c
INNER JOIN BILLING b ON c.Customer_Num = b.Customer_Num
WHERE c.Fname='Jess' AND c.Lname='Hanks'
HAVING MAX(b.Bill_Date);
I have also tried putting the 'HAVING' portion as a WHERE statement.
This should get your answer:
CREATE VIEW RECENT_ORDER
AS
SELECT c.customer_num, b.bill_num
FROM customer c
JOIN billing b ON c.customer_num = b.customer_num
WHERE b.bill_date =
(SELECT MAX(bill_date) FROM billing WHERE customer_num = b.customer_num)
AND c.Fname='Jess' AND c.Lname='Hanks'
Though normally I wouldn't expect to create a view that limits results to just one customer!? Remove the last line AND c.Fname ... if you intend to get the most recent result for any/all customers. Note that you may get multiple results for a customer if they have more than one invoice on the same date.
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
Tables are explained in detail as below:
I have 3 tables:
Table A:
It serves as the master table for information about the employees.
EmployeeId(Primary key)
Employee Designation
EmployeeName (More columns of employee data which is not relevant to this particular query)
Table B:
It serves as table where all employees who are accounted for are stored. For ex an employee who has reported sick or is on leave or has pregnancy leave, etc. Bottom line an employee which is not available
EmployeeID (primary key) (also referencing master table A as foreign key)
AccountedFor
AccountedFordurationFrom (datetime)
AccountedForDurationTo (datetime)
Table C:
It serves as a table where excused data of employees are present. For ex we have our organization's time table spread as events, 1st event is morning time conference, then 2nd is silence working time, 3rd is brainstorming sessions etc. Now if an employee is excused for a particular event, it is entered here.
EmployeeID
EventCode
Excuse_DurationFrom
Excuse Duration To
Any specific details
Here EmployeeID and ExcusedForEventCode are both composite primary keys as it is possible to have same employeeId for multiple excuses,but the combination is always unique.
We have built some custom attendance management system and would require the following details:
We need to find all those employees who are neither accounted for nor excused for a specific event(this will be provided through front end) for a time duration selected through the front end.
The result of the above query will subsequently be used to compare with a biometric attendance machine logs which gives
EmployeeId|LogDate(datetime)|EventCodes as a separate table input to our database (Master table A employeeId references this EmployeeId as foreign key)
It will be compared to find out true absentees for a particular event. ie All those employees who are neither accounted for, nor excuses for any particular event and who does not figure out in the biometric scan machine logs are absented for those time duration selected. We need the output of absentee like this EmployeeId|Employee Designation|Employee Name|EventName (have a separate table linking with EventCode)|Date&time (this would be per day per event report of employee who are absent from the selected time duration).
We have tried queries like:
select
employeemastertable.employeeid,
employeemastertable.Designation,
employeemastertable.Name,
EventCodes.EventCodeName as Eventexcusedfrom
from
employeemastertable
inner join
employeeexcusedforevents on employeemastertable.employeeid = employeeexcusedforevents.employeeid
inner join
EventCodes on employeeexcusedforEvents.ExcusedForEventCode = EventCodes.Eventcode
left join
employeeaccountedFor on employeemastertable.employeeid = employeeaccountedFor.employeeid
where
employeeexcusedforevents.ExcusedForEventCode != 1 (Morning conference)
and employeeaccountedFor.employeeid is null;
Names have been changed
I do understand this will give those employees who does not figure out in event Morning conference but even if I do left join instead of inner join between employeemastertable and employeeexcusedForevents and put employeeexcusedforevents.excusedforeventcode is null and employeeexcusedforevents.employeeid is null, I do get all those employees not present in the other two table, but the criteria of event is not satisfied. That means what if the employee is excused for the 2nd event as well in the organization. How would I cater for that in the above code? (PS this is only the 1st part of the equation I understand that, after this I need help for the other part also, where time duration and comparing with logs is concerned)?
I assume there will be just one row for the EventCode=1 in table EventCodes. Below I cross join the wanted event to the employee master table and then exclude any employees that are excused or accounted for.
-- employees neither accounted for nor excused for a specific event
SELECT
em.employeeid
, em.Designation
, em.Name
, ec.EventCodeName AS Eventexcusedfrom
FROM employeemastertable em
CROSS JOIN (
SELECT Eventcode, EventCodeName
FROM EventCodes
WHERE Eventcode = 1
) ec
WHERE NOT EXISTS (
SELECT NULL
FROM employeeexcusedforevents ee
WHERE em.employeeid = ee.employeeid
AND ec.Eventcode = ee.ExcusedForEventCode
)
AND NOT EXISTS (
SELECT NULL
FROM employeeaccountedFor eaf
WHERE em.employeeid = eaf.employeeid
)
;
I'm trying to create a view for my report for
the Staff Sales.
My Staff Fields are
Staff_ID - PRIMARY KEY
Staff_Name
My Sale Fields are
Sale_Date
Payment_ID#
Staff_ID#
Sale_ID - PRIMARY KEY
I'm trying to calculate how much sales each member of staff has made, How would I go about this? I have tried this but as you can see I'm not very good at views in sql.
SELECT tblSale.Staff_ID,
SUM(tblSale.Sale_ID*tblSale.Staff_ID) AS
SalesPerStaff FROM tblSale
INNER JOIN tblStaff ON tblSale.Sale_ID = tblStaff.Staff_ID
GROUP BY tblSale.Sale_ID
A view is just the same as a standard piece of T-SQL, it's just stored in the database ready to be called when you need it
CREATE VIEW MyViewName
AS
SELECT
st.Staff_ID
,st.Staff_Name
,COUNT(sa.Sale_ID) Sales
FROM Staff st
LEFT JOIN Sale sa
ON st.Staff_ID = sa.Staff_ID
GROUP BY
st.Staff_ID
,st.Staff_Name
That way, you can just call MyViewName and it will give you results. I've used a LEFT JOIN in case you have staff with no sales.
If your Sales table has a value column, you can change COUNT(Sale_ID) to SUM(Sales_Value) or whatever the field is called.
Just read your reply to main question. If your Sales table has something like 'Item_ID' then just add this after st.Staff_Name in the select list (remember to add to the end of the GROUP BY too).
I am using apex application building to create a report.
Table
staff
id name
201 john
302 sarah
787 bob
product
product_editor_id product_name
201 car
307 shoe
728 airbag
as you can see in the staff table I have staff details and in the product I have the staff id to show the staff that created it.
in my report I have selected everything from the staff table, but you see where I have staff id i don't want to show the id but instead their name
I have rewritten a query for this. The reason why I am asking this question is that in the previous query I had something like this
select * from staff Inner join
company_car ON
staff.staff_car_id=company_car.vehicle_id_staffid
if that query doesn't make sense. Basically I did this to only display staff that has a company car and the vehicle_id_staffid is the same as the staff id.
anyways on top for that query I need to add my new query to it which is
select distinct name from staff
left join product on
staff.id=product.product_editor_id
I tried
select * from staff Inner join
company_car ON
staff.staff_car_id=company_car.vehicle_id_staffid
UNION
select distinct name from staff
left join product on
staff.id=product.product_editor_id
but apex said ORA-01789: query block has incorrect number of result columns
I want the result from it to be different. The first query should have any problem with the second query because they are but doing their own thing.
Probably the easiest way would be to create original report with staff_id in it.So
create new report as new page or as region on the page.
-something like :
Select * from company_car
So you get report that will have staff_id as one of the columns.Then you edit page properties and go to report.
Select report attributes and from the list of columns you have in the report you chose staff_id column edit ( pencil icon on the left).
Go to display as and select 'Display as Text based on LOV dont save state'
List of value will appear. Go to list of values section
and type :
select id r, name d from staff;
or
select id r, name||' '||surname d from staff;
in here you need to link staff_id with name and their surname which will be displaying on the report instead of meaningless id. Alternatively you can create static LOV and use it to populate this column or you can try to build JOIN statement.