Mysql query group by week with specific start and end - mysql

I searched stack overflow and have come so close yet have not found if this is possible.
I want to group a weekly query from 11:30 am Friday to 11:29 am Friday for each week.
adding days and hours only partially work. I need to keep this in a mysql query not coding in php, etc.

You need to give more information on your table structure and some sample date. If I have understood correctly then below is simple pseudo code which you can start use to start framing your requirement .
select something from table where date between 1:30 am Friday and 11:29 am
group by trunc(date).
Note :
- Format the date in where condition using to_Date.
- Week definition might be different in database so you need to manipulate.

Related

Add one date to date in MySql

I am setting up a MySQL (Maria) where I am going to upload demo data for an application. In the demo date there will be many different dates from the last 6 months.
What I need to do is to keep all dates the same number of days from today all the time. So a date that is x number of days earlier or later than today keep that interval constant.
The demo data will be used in a PHP application so it is maybe an option to run PHP code to change the dates every day, or is it best done any other way?
I have been trying to just add +1, but it does not work on dates.

Access Query Criteria return weekend range if monday, else return yesterday

Using Access 2007
This is my first question and i apologize in advance if anything is asked "the wrong way".
I have a huge database regarding invoices at work.
We want to have a counter showing "How many invoices did we recieve yesterday, if it's monday, how many did we recieve on friday, saturday and sunday?"
The counter itself is no issue, but the query criteria to make it get multiple days is really annoying me (lack of knowledge)
It seems to be subtracting 3 days regardless of what day it is.
Why is the following not working in query criteria? (Not returning friday, saturday and sunday values when monday and yesterdays results if NOT monday?)
IIf(Weekday(Date()=2);<=Date()-3;Date()-1)
Edit: I'm using a danish version of Access 2007, which is why there's ; instead of ,
You can't use the result of IIf() as full criteria expression, but you can use it as criteria parameter. Use a WHERE clause like this:
WHERE (DateDiff("d",[InvoiceDate],Date()) <= IIf(Weekday(Date())=2,3,1))
AND (DateDiff("d",[InvoiceDate],Date()) >= 1)
In the query design editor both criteria will be combined into one column (German Access here).
Omit AND (DateDiff("d",[InvoiceDate],Date()) >= 1 if you want to include invoices from today.

How do I write the Criteria in Query Design to equal last month's data?

I've looked on a couple different forums and I'm unable to find what I need they all have it listed in SQL View, and that is NOT what I want. I would like to do this in the Query Design as it is much easier for me.
I simply would like to make a query to display certain parameters of the database I maintain. And those parameters would equal last month's data.
I previously was able to successfully make a query displaying all information for the past year but can't figure out how to display just this past months.
The past year Criteria:
>DateAdd("yyyy",-1,Date())
Since that worked I tried doing this but it would not work for me:
>DateAdd("mmmm",-1,Date())
I'm sure it's something simple that I'm just not seeing here. Any help or recommendations are welcome.
Referencing the link provided by Fionnuala I have come up with
>DateAdd("m',-30,Date())
The problem being is that it queries all results for the past 30 days from Today's date. I wish to display only data from October ! While this can be done easily. I don't wish to manually go in this query every month and change certain parameters. I would like it to be automatic so or next month December I click on the query and it displays all 30 days of November's data. And there is no manual process of going back in and changing any of the criteria.
In the Query Designer you can use a Criteria: like this
>=DateSerial(Year(Date()),Month(Date())-1,1) And <DateSerial(Year(Date()),Month(Date()),1)
The corresponding SQL statement is
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(Year(Date()),Month(Date())-1,1)
And (Donations.DonationDate)<DateSerial(Year(Date()),Month(Date()),1)));
If it was run on November 18, 2014 then it would effectively be
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(2014,10,1)
And (Donations.DonationDate)<DateSerial(2014,11,1)));
Notes:
This query should be sargable and take advantage of an index on the date field (if one exists).
In case anyone is concerned about "month wrap-around", the DateSerial() function takes care of that for us. If the query was run in January 2015 then the first WHERE condition would indeed be ... >=DateSerial(2015,0,1) but that's okay because it returns 2014-12-01.

Access 2010 Query for current month

I have built an Access database to keep track of Quality Assurance Monitors for our team. Our team has several leads that oversee the reps on the team.
I want to build a query the will return all of the QA's for a specified lead for the current month. I have criteria for specifying a lead and have got that to work, but every time I try to set the criteria for the current month, it ends up returning no results.
Searching Google has repeatedly suggested using the Month(Now()), but that doesn't work either.
How can I write this query?
Answer inspired by #Justin-rentmeester but refined to always return the desired results:
Add the following to the WHERE clause of your query:
MyDateField BETWEEN
DateSerial(Year(Date()),Month(Date()),1)
AND
DateAdd("s", -1, DateAdd("m", 1, DateSerial(Year(Date()),Month(Date()),1))), DateSerial(Year(Date()),Month(Date()),1)
I reccommend that you use SQL view to add this criterium.
This approach also works with dates that include times, and with months with less than 31 days.
Put this code on criteria field- Between Date()-Day(Now()-1) And Date()
I edit my answer!
Assuming workdate is your date field.
A shorter where clause can be
where Year(workDate) = Year(Date()) and Month(workDate) = Month(Date())
This will filter all records for the current month.
If you want to further filter for records up to today add
and workdate <= Date()
This may not be necessary if you are not storing future dated records in your table.
Good luck.
To do this in the Design view in Access, select all of the columns you want in the report and on the date column you want to restrict to the current month, for the Criteria put: Between
DateSerial(Year(Date()),Month(Date()),1)
And
DateSerial(Year(Date()),Month(Date()),31)

Sorting by month in MS Access

I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.