MS Access Create Date Range based on Start Date and End Date - ms-access

I have a macro which records employee absences using a dynamic calendar I created in a userform. When the employees choose their absence type, they can either enter a start date and end date (for absences longer than 1 day) or they can click on the calendar's cells to choose specific dates. This information is then sent to an Access database where the information is stored and used for generating reports.
My problem is that people find it easier to click on days which follow to apply their absences rather than manually typing in the start and end dates. I now see that the design could have been improved...
The effect this has on the database is there are many "redundant" records in the database. For those who are visual, here is an example what the database is now like and what it could be like:
Current way (bad way):
EmployeeName AbsenceType StartDate EndDate
============ =========== ========== ==========
Employee1 Vacation 2018-09-24 2018-09-24
Employee1 Vacation 2018-09-25 2018-09-25
Employee1 Vacation 2018-09-26 2018-09-26
Employee1 Vacation 2018-09-27 2018-09-27
Employee1 Vacation 2018-09-28 2018-09-28
Hopeful way (better way):
EmployeeName AbsenceType StartDate EndDate
============ =========== ========== ==========
Employee1 Vacation 2018-09-24 2018-09-28
Can anyone suggest how to go about doing this?
Thanks in advance.

You can use a Cartesian query like this:
SELECT DISTINCT
[Tens]+[Ones] AS Factor,
10*Abs([Deca].[id] Mod 10) AS Tens,
Abs([Uno].[id] Mod 10) AS Ones
FROM
MSysObjects AS Uno,
MSysObjects AS Deca;
Save it as qdyFactor and create another query to generate the dates:
SELECT DISTINCT
PersonID,
DateAdd("d",[Factor],[Start Date]) AS [First Date],
[End Date]
FROM
tblTable,
qdyFactor
WHERE
qdyFactor.Factor Between 0 And DateDiff("d",[Start Date],[End Date]);

Related

How to Group result from mySQL

My Table:
datetime. employment. name
2019-11-25 12:32:12. office. Michael Jackson
2020-01-31 12:32:22. production. Jenny Darling
2019-12-25 12:32:12. office. Michael Jackson
etc.
This is a "time registering" table, so names must be DISTINCT for each mounth. (How many unique names is there every month grouped by month and employment)
Now i'd like to create a table that will show how many employees there was in every month by year.
So the table will look like:
Year & Month. Employment. Number (Unique names)
-------------------------------------
2019-01. Office. 50
2019-01 Production. 35
2019-02. Office. 45
2019-02. Production. 36
And so on for this and prev year (2019 & 2020)
Something like:
SELECT * FROM table COUNT(DISTINCT(name)) AS number GROUP BY datetime AND employment
You seem to want aggregation... but your query is invalid in several regards. I think you want:
select
date_format(datetime, '%Y-%m') yr_month,
employment,
count(distinct name) no_unique_names
from mytable
group by yr_month, employment
This gives you on row per year/month and employment, with the corresponding count of distinct names.

Interest query for dates less total payment date

I have the following table "detallepre".
Cuota DateCuota DatePaid Capital Interest Status
------ ---------- ---------- ------- ------- --------
1 2018-05-07 2018-05-07 722.62 265.78 -1
2 2018-06-06 2018-06-06 741.67 246.73 -1
3 2018-07-06 2018-07-07 768.64 219.76 -1
4 2018-08-05 2018-07-07 2305.92 400.00 -1
5 2018-09-04 2018-07-07 5543.42 646.63 -1
Where Quota Date is the scheduled date to pay and Paid Date is the date that the payment was made, according to the table the 1 and 2 installment was paid on the scheduled date and the 3,4 and 5 installment was paid on the same day 2018-07 -07, was the total payment of interest, the State is -1 for Paid and 1 for Debt. I want to obtain the sum of interest until the end of the month that I indicate, with an exception that I will explain at the end. For example, if I consult until June, I use the following query.
SELECT IFNULL (SUM (Interest), 0) Interest FROM detailpre WHERE DateQuota <= '2018-06-30'
result: 512.51 correct
Consult until July:
SELECT IFNULL (SUM (Interest), 0) Interest FROM detailpre WHERE DateQuota <= '2018-07-30'
result: 732.27 correct
Consult until August, this is where I have a problem.
SELECT IFNULL (SUM (Interest), 0) Interest FROM detailpre WHERE DateQuota <= '2018-08-30'
result: 1132.27 Incorrect (I say incorrect because it is not the expected result)
What I hope to get is 732.27 since the total payment date (when all the remaining installments were paid) was in July 2018-07-07, and should not continue adding the interest since the fees are paid, as it should be my query if I do it within a stored procedure, where it would indicate the Date parameter, as far as I would like it to do the calculation, sorry for my bad English, Thank you
UPDATE
I will try to explain better, what I want is to obtain the total interest until the date that I indicate (which is usually the end of the month), in the case that I consult until the month of August, September, etc onwards, I hope to obtain 732.27 as in July , since the quota 4 and 5, corresponding to August and September were already paid in July, therefore they would not add those interests and there would only be 732.27,
maybe the sql query that I put is not the correct one for this case, I use MySQL Version: 5.6.34
Thank you

Create a date table between 2 dates

I am a newbie in programming and trying to add some automation in my team to help with daily operation.
I try to create a function to create interest payment schedule according to given start date, end date and intervals. For example, for a one-year security with start date 2017/01/14, maturity date 2018/01/14, payment frequency is every 3 months. it has 4 interest period: 2017/01/14 - 2017/04/14, 2017/04/14 - 2014/07/14, 2017/07/14 - 2017/10/14, 2017/10/14 - 2018/01/14. I want to create a date table to display these 4 periods in Access.
The record should look like below:
seq startdate enddate
1 2017/01/14 2017/04/14
2 2017/04/14 2017/07/14
3 2017/07/14 2017/10/14
4 2017/10/14 2018/01/14
Could anyone help me with this?
Thanks a lot.
You can use the MSysObjects table and a Cartesian query to create this:
PARAMETERS
Period Text ( 255 ),
Periods Short,
FirstDate DateTime;
SELECT DISTINCT
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)+1 As Sequence,
DateAdd([Period],[Sequence]-1,[FirstDate]) AS [DateStart],
DateAdd([Period],[Sequence],[FirstDate]) AS [DateEnd]
FROM
MSysObjects AS Uno,
MSysObjects AS Deca
WHERE
10*Abs([Deca].[id] Mod 10)+Abs([Uno].[id] Mod 10)<[Periods]
Run this with the parameters:
Period: q
Periods: 4
FirstDate: 2017-04-14

Need help for join and some calculations on a MySql insert

I'll try to provide some context so you can understand what I'm trying to achieve here. My company uses open source software to manage the employees leaves (Jorani, feel free to google it :) ).
There are different types of leave (holidays, sick leave, etc.) and we want to calculate the days "not used" from the holidays of 2016 and "copy" them to another type of leave called "Remaining Holidays 2016".
The important tables are:
entitleddays (here you specify how many days of each type you give to an employee)
id employee startdate enddate type days description
661 3 2016-01-01 2017-02-28 1 14.00 Holidays 2016
1296 3 2016-01-01 2016-12-31 4 18.00 Sick leave 2016
leaves (this table has information about the leaves taken by the employees)
id startdate enddate status employee cause duration type
2436 2016-08-01 2016-08-01 3 78 OK from managers 1.00 1
2766 2016-09-05 2016-09-12 3 63 Holidays 6.00 1
So basically we have:
Entitled leaves:
Data stored in the entitleddays table shown above. In our example let's say I have 14 days for my 2016 holidays.
Taken leaves:
Leaves taken by the user, stored in the table called leaves shown above. For our example let's say I took a day off the first of August and 6 days on September.
Available leaves:
Available days are calculated: entitled days minus "taken leaves". For this examplee, 14 entitled days - 7 = 7 days. So I still have seven days available for holidays :D
So my goal is to insert these 7 days for this user as entitled days for the new type: "Remaining days from 2016" and do this for every user. So the solution that comes up to my mind is to do something like this for every user:
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
SELECT id, '2017-01-01', '2017-02-31', '8', (entitled holidays for 2016 minus all the taken leaves of this type), 'Remaining holidays from 2016'
FROM users
Where 8 is the new type of leave where I want to copy the days (Remaining holidays from 2016).
For example I can get the taken holidays from 2016 for a specific user doing this:
SELECT SUM(duration)
FROM leaves
WHERE employee=3 AND status=3 AND type=1
Note: Type 1 is the type of leave "Holidays 2016" and status 3 means that the leave request was accepted.
I can probably achieve all of this in a single SQL instruction but it can also be split in more if simpler or easiest to manage/understand.
Many thanks in advance.
This is how you can handle the calculation:
sum the entitleddays in a subquery by grouping the datasets in its table per employee
maybe even group by year? In this case I just filtered for 2016 via WHERE-clause
sum the taken holidays in a subquery, again by grouping per employee
group by year or filter directly for the one you need
join this subquery onto the other resultset of the other query
calculate (entitled days - taken leaves) in the outer query
Query:
SELECT
entitled.employee,
'2017-01-01',
'2017-02-31',
'8' AS type,
entitled.days - takenDays.days,
'Remaining holidays from 2016'
FROM
(
SELECT
employee,
SUM(days) AS days
FROM
entitleddays
WHERE
startdate >= '2016-01-01'
AND type = 1
GROUP BY
employee
) AS entitled
LEFT JOIN (
SELECT
employee,
SUM(duration) AS days
FROM
`leaves`
WHERE
startdate >= '2016-01-01'
AND type = 1
GROUP BY
employee
) AS takenDays ON takenDays.employee = entitled.employee
I am not sure if this is how you want to calculate the sums for the days of entitleddays and taken days. The query just checks if startdate >= '2016-01-01'.
Also you mentioned a table users in your attempt but didn't provide details for the table, so I left it out. I guess you could use it as a basis otherwise. In the current query the grouped result of entitleddays is the basis.
For the insert
INSERT INTO entitleddays (employee, startdate, enddate, type, days, description)
SELECT
entitled.employee,
'2017-01-01',
'2017-02-31',
'8' AS type,
entitled.days - takenDays.days,
'Remaining holidays from 2016'
FROM
(
SELECT
employee,
SUM(days) AS days
FROM
entitleddays
WHERE
startdate >= '2016-01-01'
AND type = 1
GROUP BY
employee
) AS entitled
LEFT JOIN (
SELECT
employee,
SUM(duration) AS days
FROM
`leaves`
WHERE
startdate >= '2016-01-01'
AND type = 1
GROUP BY
employee
) AS takenDays ON takenDays.employee = entitled.employee

How to get the Year in between two dates

I have
leave table
EmpNo
EmpName
LeaveStart
LeaveEnd
I want to show the leave taken by year and i used datediff(dd,LeaveStart,LeaveEnd)
it will shows the no. of days have taken.
but i want to show yearly wise.
E.g
2014 - 10 days
2015 - 16 days
how & where i can use year function becoz have leavestart & leaveend
suppose if employee taken a leave
LeavStart- 28-Dec-2015
LeaveEnd - 05-Jan-2016
then result should
2015 - 4 days
2016 - 5 days
then how to use query.
please help on this
May be something like this
SELECT Year(LeaveStart),YEAR(LeaveEnd),DATEDIFF(DAY, MIN(LeaveStart), Max(LeaveEnd))
FROMtbl_Subject_OrderAssignedDetails
GROUP BY Year(LeaveStart),YEAR(LeaveEnd)