Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL query - getting totals by month

http://sqlfiddle.com/#!2/6a6b1

The scheme is given above.. all I want to do is get the results as the total of sales/month... the user will enter a start date and end date and I can generate (in PHP) all the month and years for those dates. For example, if I want to know the total number of "sales" for 12 months, I know I can run 12 individual queries with start and end dates, but I want to run only one query where the result will look like:

Month     numofsale
January - 2  
Feb-1
March - 23
Apr - 10

and so on...

or just a list of sales without the months, I can then pair it to the array of months generated in the PHP ...any ideas...

Edit/schema and data pasted from sqlfiddle.com:

CREATE TABLE IF NOT EXISTS `lead_activity2` (
  `lead_activity_id` int(11) NOT NULL AUTO_INCREMENT,
  `sp_id` int(11) NOT NULL,
  `act_date` datetime NOT NULL,
  `act_name` varchar(255) NOT NULL,
  PRIMARY KEY (`lead_activity_id`),
  KEY `act_date` (`act_date`),
  KEY `act_name` (`act_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;

INSERT INTO `lead_activity2` (`lead_activity_id`, `sp_id`, `act_date`, `act_name`) VALUES
(1, 5, '2012-10-16 16:05:29', 'sale'),
(2, 5, '2012-10-16 16:05:29', 'search'),
(3, 5, '2012-10-16 16:05:29', 'sale'),
(4, 5, '2012-10-17 16:05:29', 'DNC'),
(5, 5, '2012-10-17 16:05:29', 'sale'),
(6, 5, '2012-09-16 16:05:30', 'SCB'),
(7, 5, '2012-09-16 16:05:30', 'sale'),
(8, 5, '2012-08-16 16:05:30', 'sale'),
(9, 5,'2012-08-16 16:05:30', 'sale'),
(10, 5, '2012-07-16 16:05:30', 'sale');
like image 261
NMF SDF Avatar asked Oct 31 '12 07:10

NMF SDF


People also ask

How do I count months in MySQL?

select period_diff(date_format(now(),"%Y%m"),date_format(created,"%Y%m")) from customers where.. Gives a number of calendar months since the created datestamp on a customer record, letting MySQL do the month selection internally.

How do I get month from date in MySQL?

MONTH() function in MySQL is used to find a month from the given date. It returns 0 when the month part for the date is 0 otherwise it returns month value between 1 and 12.


3 Answers

SELECT DATE_FORMAT(date, "%m-%Y") AS Month, SUM(numofsale)
FROM <table_name>
WHERE <where-cond>
GROUP BY DATE_FORMAT(date, "%m-%Y") 

Check following in your fiddle demo it works for me (remove where clause for testing)

SELECT DATE_FORMAT(act_date, "%m-%Y") AS Month, COUNT(*)
FROM lead_activity2
WHERE <where-cond-here> AND act_name='sale'
GROUP BY DATE_FORMAT(act_date, "%m-%Y") 

It returns following result

MONTH   COUNT(*)
07-2012 1
08-2012 2
09-2012 1
10-2012 3
like image 103
Salil Avatar answered Oct 13 '22 21:10

Salil


You can try query as given below

select  SUM(`SP_ID`) AS `Total` , DATE_FORMAT(act_date, "%M") AS Month, Month(`ACT_DATE`) AS `Month_number` from `lead_activity2`  WHERE `ACT_DATE` BETWEEN '2012-05-01' AND '2012-12-17' group by Month(`ACT_DATE`)

Here 2012-05-01 and 2012-12-17 are date input from form. and It will be return you the sum of sales for particular month if exist in database.

thanks

like image 27
Er. Anurag Jain Avatar answered Oct 13 '22 21:10

Er. Anurag Jain


Try this query -

SELECT
  MONTH(act_date) month, COUNT(*)
FROM
  lead_activity2
WHERE
  YEAR(act_date) = 2012 AND act_name = 'sale'
GROUP BY
  month

Check WHERE condition if it is OK for you - act_name = 'sale'.

If you want to output month names, then use MONTHNAME() function instead of MONTH().

like image 37
Devart Avatar answered Oct 13 '22 20:10

Devart