I need some help. I want to find a birthday in a selected month, for example is this.
SELECT * FROM students WHERE DOB between '1777-01-01' AND '3000-01-31';
They only get the year. How can I get the value of month I selected?
To get a particular month regardless of year, you may want to do this:
SELECT DATE_FORMAT(DOB, '%e %M') AS birthday
FROM students
WHERE MONTH(DOB) = 1
ORDER BY DAY(DOB)
for January. This will scan your table and pick up all the January DOB values and order them by the day in January of their birthdays.
Related
So, I have a table that holds records of employees performing certain tasks.
A task has an id, a taskname, an employee and a startDate and endDate.
Using the following query that was provided to me by #MatBailie here (thanks!), I am able to grab how many days every employee has spent on every task in the current year:
SELECT
taskname,
employee,
startDate,
endDate,
SUM(DATEDIFF(startDate, endDate)+1) AS total_days,
FROM
schedule
WHERE
startDate<='2023-12-31'
AND
endDate>='2023-01-01'
GROUP BY
employee,
taskname
However, sometimes a task overlaps two years. For example, when a task has a startDate of the 22nd of December, and an endDate of the 10th of January, the total duration of this task is 10 days in the current year, and 10 days in the next. This is where the problem arises, because it counts all 20 days as if they were in this year because the event complies with the startDate and endDate requirements (the 'WHERE' clauses) and then the entire duration is added to the SUM.
So my question is: how can I modify my query so that it only counts the amount of days (in the SUM(DATEDIFF)) that fall within a specified timerange (i.e. the current year or quarter).
Thanks in advance for your help!
SELECT
taskname,
employee,
SUM(
DATEDIFF(
LEAST( enddate, '2023-12-31'),
GREATEST(startdate, '2023-01-01')
)
+1
) AS total_days,
FROM
schedule
WHERE
startDate <= '2023-12-31'
AND
endDate >= '2023-01-01'
GROUP BY
employee,
taskname
I have a table as follow, now I use the group by date and it only group my data based on the year. I want to group the data based on the specific day, month, and year.
Do you know how we can do that?
Use:
group by day(`date`),month(`date`),year(`date`)
Check for more info
I'm working to write a MySQL query that outputs the number of new users created by week.
My user table is:
id | created_at
My query:
SELECT YEAR(created_at) AS Year, DATE_FORMAT(created_at, '%b %e') AS Week, COUNT(*) AS total
FROM users
GROUP BY Year, Week;
The problem:
Years: I would like the most recent year to be at the top, currently the oldest year is at the top of the output.
Weeks: The week column is not sorted based on the calendar. For example, the last records shows: 2019 | May 9 | 100
Where I'd like the year and week sorted.
I think you'll find the function YEARWEEK() helpful, not only for the grouping, but also for the ordering.
Also, your use of DATE_FORMAT() doesn't look right, because you're outputing the %e, which is the day of the month, yet you're grouping by week?
SELECT DATE_FORMAT(created_at, '%Y %b %v') AS date, COUNT(*) AS total
FROM users
GROUP BY YEARWEEK(created_at)
ORDER BY YEARWEEK(created_at) DESC;
In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.
I have a list of users and their birthdays. I need to print the list of birthdays for each month and grouped by day. For example:
Monday 24th (2): Herpina Derpina, John Doe
Tuesday 27th (1): Nicolas Cage
Friday 28th (1): Michael Jackson
How could I achieve that with one single SQL call? If I group them this way, I will lose records:
SELECT COUNT(*) FROM user GROUP BY MONTH(birthday)
Any ideas? (I'm using mysql + php)
You can't get the count which is an aggregate function and the detailed records in the same query. If the names are essential then you will have to manually construct the counts in PHP and then display the records under the required format.
SELECT MONTH(birthday) month,
FORMAT(birthday, '%W %D') day,
COUNT(user) count,
GROUP_CONCAT(user SEPARATOR ',') users
FROM table
GROUP BY MONTH(birthday), FORMAT(birthday, '%W %D')