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
Related
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 single table and I need to count the number of unique viewers for each month.
SELECT month, location, COUNT(DISTINCT id) AS unique_viewers
FROM mytable
GROUP BY month, location
I need the results to only display the month, location, and the number of unique views based on the acct ids.
for example
Month ID Location
JAN 123 CA
JAN 456 CA
JAN 123 CA
I am having trouble counting based on the ID being distinct. I need my result to dislplay 2 unique views for Jan
I have a single table and I need to count the number of unique viewers for each month.
"for each month" translates to GROUP BY month in SQL.
"count the number of unique viewers" translates to COUNT(DISTINCT viewer) (or whatever that column is named).
The query:
SELECT month, COUNT(DISTINCT viewer) AS unique_viweers
FROM mytable
GROUP BY month
ORDER BY month;
I need the results to only display the month, location, and the number of unique views based on the acct ids.
The location? Which location? There can be multiple locations in a month. So this cannot be solved. You could display a list of locations instead using GROUP_CONCAT.
SELECT
month,
COUNT(DISTINCT viewer) AS unique_viweers,
GROUP_CONCAT(DISTINCT location ORDER BY location) as locations
FROM mytable
GROUP BY month
ORDER BY month;
You have altered your request, contradicting "I need to count the number of unique viewers for each month". It seems you rather want to count the number of unique viewers for each month and location.
Use COUNT(DISTINCT acct_id):
SELECT month, location, COUNT(DISTINCT acct_id) AS unique_viewers
FROM mytable
GROUP BY month, location
ORDER BY month, location;
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.
Thanks for trying to help. I have a table called posts where there is a row for each post. A post has a post_date (in unix timestamp format) and an author_id.
I am trying to get the number of unique authors that posted 5 or more times within a month grouped by month and year.
The query I have now for unique authors by month and year (without the filter for 5 or more posts within that month) is:
select
month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count(distinct p.author_id)
from posts p
group by month,year
order by year desc,month desc
Can you please help me add a filter that will only count authors that had 5 or more posts in that given month?
UPDATE
The following query works but is extremely slow, even when adding predicates for just this month and year. Can someone think of a better way to do it?
select
month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count(distinct p.author_id)
from posts p
where (
select count(*) from posts p2 where p2.author_id=p.author_id
and month(from_unixtime(p.post_date))=month(from_unixtime(p2.post_date))
and year(from_unixtime(p.post_date))=year(from_unixtime(p2.post_date))
)>=5
group by month,year order by year desc,month desc
You can use having count >5 .It will solve the problem
select month(from_unixtime(p.post_date)) as month,
year(from_unixtime(p.post_date)) as year,
count( p.author_id) c from posts p
group by author , month,year having c >5
order by year desc,month desc
I'm having a table with posts. Like (id int, date datetime).
How can I select average posts per day count for each month with one sql request?
Thank you!
This should do it for you:
select month, avg(posts_per_day)
from (select day(date), month(date) as month, count(*) as posts_per_day
from posts group by 1,2) x
group by 1
Explanation: Because you are doing an aggregate on an aggregate, there is no getting around doing a query on a query:
The inner query calculates the number per day and captures the month.
The outer query averages this count , grouping by month.
You can get the number of posts per month like this:
SELECT COUNT(*) AS num_posts_per_month FROM table GROUP BY MONTH(date);
Now we need the number of days in a month:
SELECT COUNT(*) / DATEDIFF(MAKEDATE(YEAR(date), MONTH(date)) + INTERVAL 1 MONTH, MAKEDATE(YEAR(date), MONTH(date))) AS avg_over_month
FROM table GROUP BY MONTH(date);
This will get the average number of posts per day during the calendar month of the post. That is, averages during the current month will continue to rise until the end of the month. If you want real averages during the current month, you have to put in a conditional to get the true number of elapsed days.