MySQL selecting date range but also between - mysql

My db is made of groups of entries (by user) with a row for each day of the week and also groups where there is only 1 row per week of the year. This week may start Sat, Sun or Mon.
The sql groups all these rows by user id and works fine for the entries where the user has a row for every day
The problem I have is selecting the users rows where there is only one entry per week
Basically if the rows date is 11th Feb 2012 then I need to be able to select that row if the start date criteria falls on that date or within that following week and all rows upto but not including the row where the date column is after the end date
I'm trying everything like dateadd in the sql but I just cannot get it to add these rows in.
Hope I've made myself clear.
Say I have two entries in the db
2013-02-02
2013-02-09
I have a start date of 2013-02-05 and an end date of 2012-02-13
I need to get those two row as:
the start date falls on or within the week of 2013-02-02
and I also need 2013-02-09 as the end date falls on or within the week of that date.
Hope that makes it a bit clearer.

Not sure exactly what your question is asking.
If your field is of mysql date or datetime type, and you wanted to find if there was an entry for a given week could you not use MySQL WEEK Function to find all entries for the given week, you may also need to include a restriction on YEAR too.
You could also include the following week, but you may encounter problems. The main problem being week 52+1 of 2012 wont give week 1 of 2013, but week 1 of 2012.

Related

Is there a way for me to get the previous 3 months of data, but for every row? SQL

I want to create a table that has two columns, date and count.
Date is basically just date since we started accepting payment until today (let's say 2021-09-01 to today).
Count represents the # of active merchants (i.e. anyone that has processed ANY transaction in the past 3 months)
I understand that in order to get the last 3 months of data from today, I need to do
created between date_add ('month', -3, current_date) and current_date
Is there a way that I can do that for every single row, so instead of using current_date, I can use the date that is in the date column?

Getting rows from the same week and month

I'm being given a datetime String where I need to have two separate queries, one that gets all of the records in that week, and one that gets all the records in that month.
so for the example the week query: today is 2021-08-25 06:00:00 how would I get the records in that column where the earliest would be
2021-08-23 00:00:00 and the latest would be at the end of the week 2021-08-29 23:59:59
Same for a day in the month
Is using greater + less than the correct way to approach this requirement?
If you don't care about performance, you could just use:
where year(start_timestamp) = year(#date) and
week(start_timestamp) = week(#date)
And similarly:
where year(start_timestamp) = year(#date) and
month(start_timestamp) = month(#date)
Note that MySQL cannot use an index on this. This also only uses start_timestamp. Your question doesn't explain at all how two timestamps are supposed to fit into the criteria. And the definition of week is whatever week() decides it is.

Repeat table by month from a date range using SSRS

I have here some question regarding on the above subject.
I have a parameter which is Start Date (01/01/2013)
and End Date (03/31/2013)
and a table which displays the data.
Here is my question
Can I repeat the table by month?
so if my date range is 01/01/2013 to 03/31/2013
I should have 3 tables for January - February - March
Is this possible?
Use a List control, grouped on Month Name or Month Number (or anything to designate a month), put a table within the List control to house your data for each month.

My SQL query to list dates that have no records

I have an online calendar system that I use for tracking my band's gigs - I'd like to construct a query that will display all Fridays and Saturdays that don't currently have a record assigned to them.
eg,
if I have a record in the DB for Friday 23rd Aug and Friday 30th Aug (records being gigs that are booked), what would the query cirteria be to output Saturday 24th Aug (as it has no record)?
Select * from ['giglist']
where ['gigdate'is in 'friday','saturday']
and ['gigdate' doesn't have a record]
I will probably set the days of the week as variables so that the user can run the query for any day or selection of days.
Thanks,
Darren
if assuming from your question there is a field gigdate of date type that keeps date information and a seperate record field.
Then query would be,
select DAYNAME(gigdate), DAYOFMONTH(gigdate), MONTHNAME(gigdate) from giglist where
DAYNAME(gigdate) in ('Friday', 'Saturday') and
recordfield is NULL;
It's better to use single date type field and just store date only, as mysql has powerful set of date functions to help you out for your needs.

Populate SQL column with days of the week within a date range

I currently have a table in my database which lists a range of dates, going from May until October, in the following format: YYYY-MM-DD
What I would like to do is create a new column with the days of the week, matching the date. I thought of two options;
Update all rows, in column 'day', starting with monday, then the next tuesday, next wednesday, etc, etc. and when reached sunday, loop the sequence until end of table.
Read the date in column 'date' and update the column 'day' with the matching day of the week.
I think option 1 is the most easy, and very well possible, because there are no dates being skipped. But I have no idea how to do this and couldn't find anything similar searching the web.
Any help would be greatly appreciated! Thank you
As a general rule, I would say: don't do it. The day of the week is derived directly from the date; storing it separately in the database breaks one of the basic rules of normalisation (no derived data).
There are MySQL functions to find the day of the week - dayofweek() and weekday().
Try this:
update table
set day = DATENAME(dw, date)