Count by months in MySQL database? - mysql

How do you count the total number of entries by month?
id, date
01, 2010-09-28
02, 2010-03-28
03, 2010-09-28
04, 2010-03-28
For example. How could you pick only dates from September and have them count as an output of 2?
Edit: Edited the dates to put them in the right format.

select month(date) , count(*)
from yourtable
group by month(date)

Still new to MySQL and wanted to test my own tables. Sorry to cause any trouble.
Here's the simple solution to my problem:
SELECT COUNT(DATE)
FROM TEST_TABLE
WHERE month(DATE) = 9;

Related

related to query using SQL

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.

MYSQL - Can't group a summed column

I have a table with faktura_kroner (decimal 9,2), and faktura_dato (BIGINT 11, which is a unix timestamp).
I am trying to get the sum of faktura_kroner per month so I can use it in a chart, but am struggling a bit to find the correct sql query. Also it is unique by year. So October 2016 should not be grouped with October 2017 as an example.
Any help appreciated..
SELECT sum(faktura_kroner) as sum_faktura,
MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month,
YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM
faktura
WHERE
user_id = 1
AND
virksomhet_id = 1
GROUP BY YEAR(FROM_UNIXTIME(faktura_dato)) DESC,
MONTH(FROM_UNIXTIME(faktura_dato)) ASC, faktura_dato DESC;
You can try below query -
SELECT sum(faktura_kroner) as sum_faktura
,MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year
FROM faktura
WHERE user_id = 1
AND
virksomhet_id = 1
GROUP BY MONTHNAME(FROM_UNIXTIME(faktura_dato)) as month
,YEAR(FROM_UNIXTIME(faktura_dato)) as year

How to select the compute the range value of a given date?

I have this table below. I want to count and select the values within a month range.
Here's my query
select count(*) as c, monthname(file_date) as mn
from baguio_patrolcase
where monthname(file_date) between 'January' and 'July'
group by monthname(file_date)
order by file_date
What I want to achive is that it will also count and select the values from February an June. How will I do this?
When you convert the date to a month name, you are comparing strings. Here are two options to do what you want:
where month(file_date) between 2 and 6
where file_date >= '2015-02-01' and file_date < '2015-07-01'
The second is better, because it allows the engine to use an index on file_date (if available).
Also, between keeps the end values (it is inclusive). So, if you want February through June, then use those months, rather than 1 and 7.
What I have understood, you can use in clause:
select count(*) as c, monthname(file_date) as mn
from baguio_patrolcase
where monthname(file_date) in('January', 'July')
group by monthname(file_date)
order by file_date
Add the name of all the required months in the in clause.
You can see the SQLFiddle Demo here.

vb.net select between two dates

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.

mysql - getting the last 10 unique months

I want to get the last 10 unique months where there have been events, stored as event_date as a mysql date. I am using coldfusion so could group the result but can't get that working as I would like.
This is what I have so far.
SELECT MONTH(event_date) AS month, YEAR(event_date) AS year, event_date from events
so ideally if there were no posts in August would be output as
september 2010
july 2010
june 2010
may
etc etc
Thanks.
R.
SELECT DISTINCT MONTH(event_date) AS month, YEAR(event_date) AS year from events ORDER BY year DESC, month DESC LIMIT 10;