N
N
Ninja Mate2016-11-23 22:37:33
MySQL
Ninja Mate, 2016-11-23 22:37:33

How to select the sum of records by month and add a column with the name of the month and year?

I want to make a query that gives a grouping by month-year (2016 jun ...) and has a column with the name of the month and year,
now there is this

SELECT Month(ps_datetime), COUNT(ps_id) x FROM `patient_services` GROUP BY YEAR(ps_datetime), MONTH(ps_datetime)

you need to add a column in which there will be MONTHNAME (ps_datetime) and YEAR (ps_datetime)
the name of the month can be repeated, so you need to do GROUP BY not only by month, but also by year
Help me figure it out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2016-11-23
@victorzadorozhnyy

SELECT COUNT(ps_id), EXTRACT(YEAR FROM ps_datetime) AS Year,
EXTRACT(MONTH FROM ps_datetime) AS Month
FROM `patient_services`
GROUP BY Year,Month
www.w3schools.com/sql/func_extract.asp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question