I have a set of data spanning across 3 days. Based on the first column from the left, is it possible to have a query that calculates the average of the values in the third column from the left based on each day?
The end result will be two columns:
1/1/2008 | 1.605
2/1/2008 | 1.59
3/1/2008 | 1.56
I think the logic will be something like in a loop:
Based on the day in the first column, if it is the same day, count the number of rows and add up the values of the third column
Use the sum of the values of the third column and divide by the number of rows to get the Average
day + 1
But how can we implement a loop in MS Access?
Here is the set of data:
Edit:
If I want to group by the date, where the day is between yesterday's 6PM to today's 6PM?
The BETWEEN clause is to check for records that are between yesterday's 18:00:00 and today's 18:00:00. Example, for 1/1/2008, the record will start from 1/1/2008 6:00PM to 2/1/2008 6:00PM. For 2/1/2008, the record will start from 2/1/2008 6:00PM to 3/1/2008 6:00PM`. So on and so forth...
I have a code snippet that checks for this:
([In process analysis result].[Date Time]) Between Date()-1+#12/30/1899 18:0:0# And Date()+#12/30/1899 18:0:0#)
But it only groups for one day before. How can I apply for the set of data?
Edit 2:
This is the query that I have done, but it is still not correct. Any idea what is wrong?
SELECT DateValue([Date Time]) As DateValue, Avg([MFR g/10min]) AS [AvgOfMFR g/10min]
FROM [In process analysis result]
WHERE ((([Date Time])>Now()-365) AND (([Operation Grade-Load]) Like "EX*") AND (([Date Time]) Between [Date Time]-1+#12/30/1899 18:0:0# And [Date Time]+#12/30/1899 18:0:0#))
GROUP BY DateValue([Date Time]);
SELECT the AVG of the value and GROUP BY the date
Your logic looks basically correct. The where clause looks off. Start with this:
SELECT DateValue(DateAdd("h", 6, [Date Time])) As DateValue,
Avg([MFR g/10min]) AS [AvgOfMFR g/10min]
FROM [In process analysis result]
WHERE [Date Time] > DateAdd("yyyy", -1, DateAdd("h", 6, Now())) AND
[Operation Grade-Load] Like "EX*"
GROUP BY DateValue(DateAdd("h", 6, [Date Time]));
I'm not sure what the BETWEEN condition is supposed to be doing. If
you want a particular date range, just use date constants.
Edit: Date/time should be offset by 6 hours as in the above edit. And a proper 1-year-back expression should be used. Not sure if current time should be shifted 6 hours too (shown); if not, just remove the DateAdd of Now().
Related
I am working with a dataset that has a continues date field and I want to aggregate it at a monthly level where the month ends on the 15th day of the month. So each snapshot date would go from the 15th of the month to the 14th of the following month.
Example: Snapshot Date = 7/15/2021 would correspond with the date range of 6/15/2021 through 7/14/2021.
Is there an easy way to do this for all months in the table using SQL Server.
Just subtract 14 days and convert to a year/month format. One trick is to move everything to the last day of the month:
select eomonth(dateadd(day, -14, datecol)), count(*)
from t
group by eomonth(dateadd(day, -14, datecol));
I'm looking to create a recurring date calculator that will correspond to EVENTS on a specific calendar date. Each event has its own rule.
For example, for EVENT A rule is "Occurs Tuesday monthly with the start date Tuesday 9-17-2019. I'm using a time dimension table with every date populated until 2025. Using these dates I was using in the WHERE clause
WHERE dayname = 'tuesday' and ( DATEDIFF(DAY, '2019-09-17', Calendar_Date) % 28 ) = 0 to get monthly tuesdays.
But I'm running into issues when there are two 5 Tuesdays in a month. 3-3-2020 and 3-31-2020. Need date populated to be 4-7-2020 instead of 3-31-2020.
Can anyone help with a solution?
With your query, you're pretty close. All you need to do is to then sub-select
SELECT MIN(CalendarDate) AS CalendarDate
FROM (your query goes here) AS d
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate);
I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...
I am using SQL Server Mgmt Studio. I need to create a formula that will only select records when the month and day of a date/time field, LAW_TAEEMASTER.MASTR_ENTRY, is less than or equal to the current month and day. For example, If LAW_TAEEMASTER.MASTR_ENTRY = 8/20/2015 and current date = 7/6/2017, exclude. If LAW_TAEEMASTER.MASTR_ENTRY = 12/21/2014 and current date = 1/15/2017, include. Date formatting is my weakness. Here is what I have tried:
select LAW_TAEEMASTER.MASTR_ENTRY
from LAW_TAEEMASTER
WHERE day(LAW_TAEEMASTER.MASTR_ENTRY) <= DAY(GETDATE()) and MONTH(LAW_TAEEMASTER.MASTR_ENTRY) <= MONTH(getdate())
and
select LAW_TAEEMASTER.MASTR_ENTRY
from LAW_TAEEMASTER
WHERE AND DATEPART(DAY,LAW_TAEEMASTER.MASTR_ENTRY) <= DATEPART(DAY,GETDATE()) AND DATEPART(MONTH,LAW_TAEEMASTER.MASTR_ENTRY) <= DATEPART(MONTH,GETDATE())
but these just evaluate the month and day separately and as a number, greater than or less than, so it's excluding records. How can I get SQL to recognize these as part of a date and just ignore the year?
I would do this using month() and day():
where month(LAW_TAEEMASTER.MASTR_ENTRY) * 100 + day(LAW_TAEEMASTER.MASTR_ENTRY) < month(getdate()) * 100 + day(getdate())
If performance is an issue, I would recommend a computed column with an index.
Explain the logic and code:
Use CONVERT(varchar(5),<your_date> ,110)). The 110 arguement means USA standard time and have an ouput like mm-dd-yyyy. Then we only take the first five character which is the month and day of the date.
110 = mm-dd-yyyy
read more about cast and convert here
my sample table:
MASTER_ENTRY
2017-08-30
2017-07-05
2017-07-04
Then I executed this query
SELECT LAW_TAEEMASTER.MASTR_ENTRY
FROM LAW_TAEEMASTER
WHERE CONVERT(varchar(5),LAW_TAEEMASTER.MASTR_ENTRY ,110) <= CONVERT(varchar(5),GETDATE() ,110)
result:where month and day equal or earlier current month and day
MASTER_ENTRY
2017-07-05
2017-07-04
I hope this helps and welcome to StackOverflow. If you find this answer or any other answer solves your problem please mark it as the solution. This will help the community and fellow programmers who run into the same problem in the future. Thanks.
I need to run a weekly report based on the arrival date. How can I set the arrival date in the where clause so that I can get the result only for each week. The hard part is I DO NOT want to modify the dates each week. I need the permanent where clause for the date. I have to provide a list of customers who arrived every week and I just want to run the same script without changing the week dates.
Please assist.
Thanks.
SELECT * FROM TABLE WHERE
(ARRIVAL_DATE>DATEFROMPARTS(YEAR(GETDATE()-7), MONTH(GETDATE()-7), DAY(GETDATE()-7)))//7 days before starting at midnight
AND
(ARRIVAL_DATE<DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE()))) //NOW in the YYYY, MM,DD format
This will get everything that happened in the current calendar week (Mon-Sun)
SELECT * FROM Table1
WHERE ArrivalDate BETWEEN
CAST(DATEADD(dd,(((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ,getdate()) as date) AND
CAST(DATEADD(dd,6+((((DATEPART(dw,getdate())+##DATEFIRST) % 7)+5) % 7 ),getdate()) as date)
Edit - Removed extra E in BETWEEN