ms access query to join date table to Timesheet table - ms-access

My end result is a report of multiple employees that includes days worked in a date range. In my report, I need to have all dates in the range showing on the report whether the employee worked it or not.
So I have a table with dates worked and hours for each empl. I also created a table with dates far into the future. I think I need a query that joins the two tables and puts all dates for the date range - that will be prompted. added to the table of employee time worked.
Does anyone have any ideas how to join these tables and get all dates for each employee in the two week range that will be prompted?
First table Dates has only 1 column with dates way into the future.
Second table has Empl Name, Date worked, and hours worked.

You will need to use a nested query (aka. sub query) to create all combinations of employees and calendar days then you can do a left to your second table (timesheet table).
select *
from (select distinct empl_name, cal_date from first_tbl, second_tbl
where cal_date between [start date] and [end date]) as nested
left join second_tbl
on nested.empl_name = table_2.empl_name
and nested.cal_date = table_2.work_date

Related

Access - Only take row from table of max year

I have a table in Access that holds students previous qualifications. It has multiple lines per students dependent on the number of previous qualifications they have. The data has the year of the qualification in it.
I want to create a sub-query that only has the most recent qualification in it (i.e. the latest year). I have tried max and last in the query for the year, but because I am bringing out other fields, it is still pulling out all the qualifications rather than just the latest one.
StudentID Qualificationlevel QualificationType MaxOfYearAwarded
10203 Postgraduate/Masters MSc 2016
10203 Undergraduate BSc 2013
So in the example above, I want to ONLY have the top row being pulled as it is 2016 and therefore later than 2013.
Any help would be appreciated.
Quite a simple task
Just use a subquery as a condition.
To group the query, use a WHERE condition in the subquery. Note that, since we're querying the same table twice, we will need to use table aliases
SELECT *
FROM MyTable t
WHERE t.YearAwarded = (
SELECT Max(s.YearAwarded)
FROM MyTable s
WHERE s.StudentID = t.StudentID
)

entries in multiple dates in access

I have a access table that has quarterly pricing data starting from 20100131 and goes on as 20100430, 20100731.... 20170131, 20170430. For each pricing date, there are many loans. Some loans stay in the portfolio, some loans are removed and some added for each pricing period. I would like to find the list of loans that exist in all periods and see their price for each period. So i have the "Loan_Number" field and "Price_Date" field. I would like to find the Loan Numbers that exist in all price date points. I appreciate the help.
Thanks!
Would have been nice to see some effort from you but I was intrigued with the challenge so here is what I accomplished.
1 - Need a dataset of all possible combinations of loan numbers and date values. So if you have a LoanNumbers table and a Periods table, create a Cartesian query called AllPairs:
SELECT LoanNumbers.Loan_Number, Periods.Price_Date FROM LoanNumbers, Periods;
If you don't have those tables, generate datasets with queries, assuming the data table has at least one record for every loan number and at least one record for every period:
SELECT DISTINCT Table1.Price_Date FROM Table1;
SELECT DISTINCT Table1.Loan_Number FROM Table1;
2 - Join AllPairs to data table for a 'find unmatched' query called LoanNoPeriod:
SELECT AllPairs.Loan_Number, AllPairs.Price_Date, Table1.Loan_Number, Table1.Price_Date
FROM AllPairs LEFT JOIN Table1 ON (AllPairs.Price_Date = Table1.Price_Date) AND (AllPairs.Loan_Number = Table1.Loan_Number)
WHERE (((Table1.Price_Date) Is Null));
3 - Final query:
SELECT * FROM Table1 WHERE Loan_Number NOT IN (SELECT AllPairs.Loan_Number FROM LoanNoPeriod);
Be aware these type of queries can perform very slowly and with very large datasets might not be practical.

Access - DLookup in a table field?

For this exercise I have two tables.
Table A: Employee Record and Table B: Timesheet Record.
Timesheet Record tells me which employee has entered how many hours for a job, as well as other unimportant information.
What I need to know is how much can we bill the client for those hours on that entry?
I have an Hourly Rate set in Employee Record - however, I'm not entirely sure how to pull this information across. In a perfect world, I'd simply do a DLookUp into a cell (Putting "Hourly Rate" alongside "Hours" in a single entry), which a final calculated cell that would multiply the two.
Alternatively I could do a calculated cell from "Hours" in Timesheet Record, multiplied by "Hourly Rate" in the Employee Record, but I'm not sure how I would limit the result to the specific employee of record in Timesheet Record.
Any help would be much appreciated.
PS: Using Access 2010.
Create a select query where you join TableA.Id on TableB.EmployeeId, Group By TableA.Id and TableB.ProjectId, and sum TableB.Hours as BillingHours.
This BillingHours you multiply with HourlyRate as BillingAmount.

How to get Number of Employees Joined or Resigned by Quarterly in SQL

How to get Number of Employees Joined or Resigned by Quarterly in SQL
I have a Table called Mst_Employee
fields are
Emp_No, Emp_JoiningDate, Emp_ResignedDate, Emp_Status
Edit: Every half a year, not quarterly.
The easiest way to do analysis like this would be to use the DATEPART function in T-SQL. Assuming all you want is to know a specific quantity for all quarters in all years on your table, an example code would be:
SELECT
DATEPART(YEAR,Emp_JoiningDate) as [Year],
DATEPART(QUARTER,Emp_JoiningDate) as [Quarter],
COUNT(1) as [Emp Count]
FROM Mst_Employee
GROUP BY DATEPART(YEAR,Emp_JoiningDate),DATEPART(QUARTER,Emp_JoiningDate)
ORDER BY 1,2
This will show all the numbers of employee's joined in the quarter. The query can easily be modified to also show resigned employee's in that quarter, or you could use a separate query to show this data.
Just an additional comment, as you're in the Employee Table, you don't need to directly state "Emp_" under all of your attributes.

Sql Query Optimization - Remove Not In Operator

I am storing employee attendance on a table 'attendance' having the following structure:
EmpID, CDate
Attendance system insert this table everyday with employee-id of all employees present on that particular day.
I need to find out absent statement of a particular employee. I can do this easily by selecting all distinct date that are not in - dates where the employee is present.
Is there any way I can remove the not in operator on that sql statement. Please help
Here is the sql query for employee with EmpId 01:
select distinct CDate
from attendance
where CDate not in (
Select CDate from attendance where EmpID='01')
The problem isn't in the NOT IN clause, it is the subquery.
You want something more like:
SELECT DISTINCT a1.CDate, if (EmpID=NULL, false,true) as Present
FROM attendance as a1
LEFT JOIN attendance as a2 USING (CDate)
WHERE a2.EmpID='01'
This is a cartesian join which pulls all of the dates, then joins the employee attendance status on that. Should be significantly faster than your subquery.
Updated, with tested code:
SELECT DISTINCT a1.CDate, IF (a2.EmpID IS NULL, false,true) as Present
FROM attendance AS a1
LEFT JOIN attendance AS a2 ON (a1.CDate = a2.CDate AND a2.EmpID='01')
My bad on the previous answer. I should have put the subselection into the ON instead of an aggregate.
You could change your mechanism to store data for each employee, each day.
Yes, it'll add a lot of rows, but how can you be sure that you'll get all dates from logged data? what if theres nobody at work today? no one will have absense?
If you'd go with:
EmpID, CDate, Present
1, {date}, 0|1
then you'd have simpler and faster query traded for table size:
select CDate from attendance where EmpID = 1 and status = 0;