Limit some specific day in a week? - mysql

I have bunch of dates in my database, and now I want to use WHERE to limit a specific day in a week for eg. the date I have rn is 2019-01-01 etc. and I only want to see Monday, Tuesday and Wednesday among those date. How do I do that?
The code I showed below does not work just as a reference what I'm doing rn
CREATE VIEW weekend_reservation AS
SELECT CONCAT(last_name,',',first_name) AS "guest's name",
CONCAT(DATE_FORMAT(check_in_date,"%W,%M %d,%Y")) AS 'check_in_date',
CONCAT(DATE_FORMAT(check_out_date,"%W,%M %d,%Y")) AS 'check_out_date'
FROM guests JOIN reservations
USING(guest_id)
WHERE check_in_date BETWEEN 'Friday' AND 'Sunday'
ORDER BY check_in_date,check_out_date,last_name,first_name;

Well, I wouldn't do it just by changing the SELECT to have the day-of-the-week.
Instead:
CREATE VIEW weekend_reservation AS
SELECT CONCAT_WS(',', g.last_name, g.first_name) AS "guest's name",
DATE_FORMAT(r.check_in_date, '%W, %M %d,%Y')) AS check_in_date,
DATE_FORMAT(r.check_out_date, '%W,%M %d,%Y')) AS check_out_date
FROM guests g JOIN
reservations r
USING (guest_id)
WHERE DAYOFWEEK(r.check_in_date) IN (6, 7, 1)
ORDER BY check_in_date, check_out_date, last_name, first_name;
Notes:
CONCAT() after the DATE_FORMAT() does nothing.
The DAYOFWEEK() function returns days, with Sunday as 1 and Saturday as 7.
You should qualify all column names (i.e. use table alias), especially when a query has more than one table reference.
CONCAT_WS() is handy. It handles NULL values better than CONCAT().

Related

How to sum month in Google Big Query?

I am using Google Big Query and I am trying to to sum the data from each month (which was given on a daily basis).
SELECT sum(EXTRACT(MONTH FROM date)) as month, region_name, avg(stringency_index) as stringency_index, sum(deaths) as deaths FROM `bigquery-public-data.covid19_govt_response.oxford_policy_tracker`
WHERE (stringency_index is not null) and (region_name = "New York" OR region_name = "Florida") AND (date BETWEEN '2020-05-01' AND '2020-12-30')
GROUP BY region_name, date
ORDER BY EXTRACT(MONTH FROM date);
But it still is showing 30ish rows for each month, making me believe that it isn't summing the months, but still giving me the data by data. Would I use a TRUNC function? The problem is the column label for date in this dataset is date, so I don't know how to TRUNC(MONTH as date) if date is both a column name and a variable.
Any help is appreciated.
Thank you,
Yoni
First, I would recommend truncating the date. Then:
SELECT date_trunc(date, month) as month, region_name,
avg(stringency_index) as stringency_index,
sum(deaths) as deaths
FROM `bigquery-public-data.covid19_govt_response.oxford_policy_tracker`
WHERE stringency_index is not null and
region_name IN ('New York', 'Florida') AND
date BETWEEN '2020-05-01' AND '2020-12-30'
GROUP BY region_name, month
ORDER BY month;
The problem with your query is that you are aggregating by date. You could fix it by aggregating by month; I think the complete date is safer -- although COVID data has not yet existed for an entire year, so right now, you don't have to worry about data from months in different years.

calculate Age from year, month and day fields

I have a table of people and I need to know how many of them are actual minors.
I have the following query:
SELECT count(*) as minors from
FilesMain a INNER JOIN Sides b
ON a.FileID = b.FileID
INNER JOIN SideData c
ON b.SideDataID = c.SideDataID
WHERE a.StatusCode IN (100,101) AND (YEAR(CURDATE()) - BirthYear<17)
Basically in the query above, I am calculating current date year minus BirthYear field.
I have the persons birth date separated to year, month and day in 3 different fields. please don't ask why. I inherited the data. What would be the correct way to use the Month and Day fields as well to get a more specific result. Just using Year will treats someone born January first and December 31 the same.
Thanks
... AND TIMESTAMPDIFF(YEAR,
CONCAT_WS('-', BirthYear, BirthMonth, BirthDay),
CURRENT_DATE) < 17
Also you may add generated column:
ALTER TABLE tablename
ADD COLUMN DOB DATE
GENERATED ALWAYS AS (CONCAT_WS('-', BirthYear, BirthMonth, BirthDay));
and use this column instead of the above expression.

Query for fetching the count of records each month

I am trying to fetch the count of records entered in each month of the financial year
For example, I have declared a column called issue in varchar because the data what I am taking is issues of the particular machine. And for example, let's say one issue is raised in July month I enter the data as 'Jul 19-1' and the again issue is raised in the month of September again I go back to the issue happened in July and enter the data as 'sep19-2'.
So in the backend, it takes as jul19-1 sep19-2
What can be the query that I can write for counting the number of issues raised in each month
I tried the below query but
SELECT COUNT(month_nc)
FROM `ncr`
WHERE month_nc='Jul18-1'
In some months there will be only one issue so I can the count of the month given in the above query
What will be the query if I want to fetch the count of each month
id issue issue_month
1 bearing jul18-1
sep18-2
2 motor jul18-2
3 battery apr18-3
ps: issue_month is declared in varchar(10)
Here are two methods. One using strings:
select left(issue_month, 5), count(*)
from t
group by left(issue_month, 5), count(*)
This will not order the values correctly.
You can convert to a date to order properly:
order by str_to_date(concat('01', left(issue_month, 5)), '%d%b%y')
Or, represent the dates correctly:
select str_to_date(concat('01', left(issue_month, 5)), '%d%b%y') as yyyymm, count(*)
from t
group by yyyymm
order by yyyymm;
Here is what you can do to split your issue_month into "month_year" and "issue_count"
yourTable
select id,
issue,
issue_month,
REGEXP_SUBSTR(issue_month, '[^-]+', 1) as month_year,
REGEXP_SUBSTR(issue_month, '[^-]+', 1,2 ) as issue_count
from yourTable;
Now you can aggregate the issue_count across issues or year_months or any other field in your table.
For example, to get the sum of all the issues for any given month_year
select
month_year,
sum(issue_count) issue_count
from
(select
id, issue, issue_month,
REGEXP_SUBSTR(issue_month, '[^-]+', 1) as month_year,
REGEXP_SUBSTR(issue_month, '[^-]+', 1,2 ) as issue_count
from yourTable) foo
group by month_year;

mysql select query if the curdate between two dates of every month

I am trying create a mysql query which will return the result only if like
Today's date is between 25th and end of the month.
I couldn't find a solution or idea how to go about it.
example:
SELECT m.member_name
FROM member m, `club_name` c
WHERE m.cabinet = 1 and c.wmmr_report=0 and m.club_name=c.id and date(curdate) between ...
Thanks
Actually you need only the day, not the whole date, so you may use
SELECT m.member_name
FROM member m, `club_name` c
WHERE m.cabinet = 1 and c.wmmr_report=0 and m.club_name=c.id and DAY(NOW()) >24
Date and Time Functions

How to solve this "cursor" type query in MySQL?

I have the following data in my table. BTW ... this is a DD/MM/YYYY format:
Date
18/09/2012
17/09/2012
13/09/2012
11/09/2012
10/09/2012
09/09/2012
25/08/2012
24/08/2012
The result what I want are:
Date
18/09/2012
13/09/2012
11/09/2012
09/09/2012
25/08/2012
The rule:
It starts from the latest date (18/09/2012) and check the next one down (17/09/2012). If there is a date then removed that from the list because it requires to have 1 day apart. Then goes to 13/09/2012 and then check 12/09/2012 and didn't find and then move to next one so on and so on. Basically you can't have date close each other (min 1 day apart).
Now I can do this on cursor if it's on TSQL however since I'm working on MySQL, is there any such thing in MySQL? Or perhaps any sub-queries approach that can solve this query?
I'm appreciated your feedback.
Try this solution -
SELECT date FROM (
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
) t
WHERE start_date = date
The subquery finds out start days (18, 13, 11...), then WHERE condition filters records. Try to run the subquery to understand how it works -
SELECT
date, #d := IF(#d IS NULL OR DATEDIFF(#d, date) > 1, date, #d) start_date
FROM
dates,
(SELECT #d:=null) t
ORDER BY
date DESC
SELECT
"MyTable1"."Date"
FROM
"MyTable" AS "MyTable1"
LEFT JOIN "MyTable" AS "MyTable2" ON
ADDDATE("MyTable1"."Date", INTERVAL 1 DAY) = "MyTable2"."Date"
WHERE
"MyTable2"."Date" IS NULL
ORDER BY
"MyTable1"."Date" DESC
As long as I know about mysql query will be quit tricky and buggy if some how you manage to write the one. I suggest go for cursor, here is the syntax of the cursor,
here is the syntax of the cursor