I have a staff timesheet table where i have timestamp of when those records are created. I now want to generate the report so that my start date is Tuesday and end date is next Monday, which is 1 week. Now i need to generate all the records grouped by this weeks time but will be next set of tuesday to monday.
This is like normal GROUP BY WEEK(Timestamp) but the WEEK numbers are not the default ones i need to generate the reports in this custom duration. I have a query working for this which groups the record efficiently by Week 1, week 2, week 3 etc.. which is picked from default mysql calendar i guess. How can i change that to generate reports grouped by custom weeks ?
Can you tel me how the following works as how the dates are picked up ?
SELECT WEEK(pw.date) AS Date,DATE_FORMAT(pw.date,'%d-%m-%Y') AS post_date,
SUM(wages) AS amount,SUM(pw.hours) AS hours,SUM(pw.minutes) AS minutes
FROM pos_sessions pw
GROUP BY YEAR(pw.date), WEEK(pw.date) ORDER BY pw.date DESC
I think this Other Solution is what you are looking for...
Related
I am generating a report which displays how long the plant was running for the day.
Since the operator shuts down the system at lunchtime, I have 2 records for the plant operation hours for each day. I want to display only one record that contains the production Start Time (6:00 am) and production End Time (around 4:00 pm). I have got a table Runtime_Combined that has an auto-incrementing index.
I want to select start date (e.g. 9/1/2021 6:04 AM, which has runtime_combined_ndx = 1) and end date (e.g. 9/1/21 4:23 PM, which has runtime_combined_ndx = 2).
SELECT ProductionStartDate, ProductionEndDate
FROM Runtime_Combined
WHERE month(ProductionStartDate) = month (ProductionStartDate)
And day(ProductionStartDate) = day( ProductionStartDate)
You can use aggregation:
SELECT date(ProductionStartDate), sum(runtime_combined_ndx)
FROM Runtime_Combined
GROUP BY date(ProductionStartDate);
Use grouping. Something like:
SELECT MIN(ProductionStartDate) AS Start, MAX(ProductionEndDate) As End
FROM Runtime_Combined
WHERE <....>
GROUP BY DATE(ProductionStartDate)
I am working on a database which includes sales information; the table for this is fairly basic:
ContractID(PK), CustomerID(FK), SalesAgent(FK), Value(int), SalesDate(Date)
And I have a requirement to produce a monthly sales report - sounds simple; group the dates by month.
However, the client has a non-calendar monthly sales structure - effectively, most months are 28 days, December is 42 days, and April & August are 35 days - this means that all months start on a Monday, and the extra weeks are allocated to Easter, High Summer, and Christmas, when business is usually slower.
So effectively I have a calendar like this:
Month. StartDate, EndDate
1. 20210104, 20210131
2. 20210201, 20210228
3. 20210301, 20210328
4. 20210329, 20210502
5. 20210503, 20210530
6. 20210531, 20210627
7. 20210628, 20210725
8. 20210726, 20210829
9. 20210830, 20210926
10. 20210927, 20211024
11. 20211025, 20211121
12. 20211122, 20220102
13. 20220103, 20220130
14. 20220131, 20220227
etc.
What's the best way to allocate each sale to a period above to group for reporting - I was initially thinking of having the above table as a CTE within my query, then SELECT based on the SalesDate being before and after the start and End Dates in the above, but what join do I then use to link that to the main query?
Is there no way around this other than to run the query for each specific period as listed above, or can this be done with a UNION?
(hope this makes sense)
Join your sales data and calendar, template
with calendar(Month, StartDate, EndDate) as(
..
)
select ..
from calendar
join sales on sales.date between calendar.StartDate and calendar.EndDate
group by .. calendar.Month ..
I need to get the row where the due_date field has the last month in every year.
For eg: if I have 3 entries with due_date field like 2014-5-21,2014-6-21,2014-7-21
I need the last row in year 2014, that will be 2014-7-21, like wise in 2015 and the following years.
Can someone help me out with this.
I tried but nothing worked out
SELECT distinct(year(due_date)) FROM `vw_mortgage_repayment_schedule_org`
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
and month(due_date) = max(month())
I need all the last rows for the given mortgage of every year eg- 2014,2015,2016 etc
I think if you group by the year of the due_date, that might just about give you what you need, given that we search for the max month in the select, and group by the year. Possibly. Can we have your table structure?
SELECT year(due_date), month(max(due_date)), max(due_date)
FROM `vw_mortgage_repayment_schedule_org`
where mortgage_id ='AREM-1408614735-VLASFAQ8VI'
GROUP BY year(due_date)
finding records within a week, i tried this
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate) = YEARWEEK(CURRENT_DATE)
but it returned the records as like calendar week. I.E. the records were between sunday and saturday of a current week.
how can i write a code to set different start of week? and how to display records within custom start of week to custom end of week?
You can include a value for the mode argument which will set what the first day of the week is. If no mode is included the default is to select Sunday as the first day.
See http://www.techonthenet.com/mysql/functions/yearweek.php for a list of the different modes available.
as #gerard says, use YEARWEEK with the optional {mode} value set to 1 - for week starting monday e.g.
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate,1) = YEARWEEK(CURRENT_DATE,1)
NOTE: WEEK has a table with the other possible values for {mode}
I really don't know how to ask this question or title it but here I go. I work in a school system and I have created a database for some psychologists to use to track their referrals. By state rules,they have 60 days from the date of their first meeting to finish the process. Weekends still count but HOLIDAYS DO NOT. I do not really know how to use the calender we have so that we have an accurate Calculation. For instance, with Holidays accounted for, if a kid was started today, he would need to have everything finished on 1/18/2013 That is 60 days from now based on our schedule. Does anyone have any idea where I should start?
Edit
Ok, so I now have a Calender table. Here is my issue. I have my column that I used to indicate which days are used in calculating my 60 days. Weekends can be used in that calculation. HOWEVER, they cannot be used in the result. If the 60th day lies on a Sunday or Saturday, then the date would need to go to the Friday before. I guess my first issue is really, how do I limit my calculation to the dates in my calender table?
This can be easy with a calendar table.
PARAMETERS start_date DateTime;
SELECT TOP 1 sub.the_date
FROM
(
SELECT TOP 60 the_date
FROM tblCalendar
WHERE
the_date>=[start_date]
AND work_day=True
ORDER BY the_date
) AS sub
ORDER BY sub.the_date DESC;
That query is based on the assumption you have set work_day to True for the dates you want evaluated. IOW, work_day will be False only for your organization's holidays.
For sample code to create and load your calendar table, see the CreateTable_calendar() and LoadCalendar() procedures at Using Start Date and End date in Access query. To initially assign all dates including weekend days as work days, make this change in LoadCalendar().
'rs!work_day = Not (Weekday(dte) = vbSunday Or _
' Weekday(dte) = vbSaturday)
rs!work_day = True
Finally, manually edit the table to change work_day to False for your holidays.
You can check the weekday to ensure you have not chosen a weekend:
SELECT TOP 1 CalDate, WDay
FROM (SELECT Top 60 c.CalDate,Weekday([Caldate]) AS WDay
FROM Calendar c
WHERE c.Holiday=False) a
WHERE WDay Not In (1,7)
ORDER BY CalDate DESC