Count totals by year and month - mysql

I have a table that looks like this:
id,created,action
1,'2011-01-01 04:28:21','signup'
2,'2011-01-05 04:28:21','signup'
3,'2011-02-02 04:28:21','signup'
How do I select and group these so the output is:
year,month,total
2011,1,2
2011,2,1

Try this:
SELECT DATE_FORMAT(created, '%Y') as 'year',
DATE_FORMAT(created, '%m') as 'month',
COUNT(id) as 'total'
FROM table_name
GROUP BY DATE_FORMAT(created, '%Y%m')

SELECT YEAR(created) as year_val, MONTH(created) as month_val ,COUNT(*) as total
FROM testing
GROUP BY YEAR(created), MONTH(created)

Related

how to set order by in between two union querys

SELECT month,sum(pcs) as pcs,sum(carat) as carat,sum(amount) as amount from (
SELECT
DATE_FORMAT(StockInwardDate, '%b-%Y') AS Month,
COUNT(*) as pcs,
ROUND(SUM(Carat),2) as carat,
ROUND(SUM(TotalAmount),2) as amount
FROM tbl_sales
GROUP BY DATE_FORMAT(StockInwardDate, '%m-%Y')
UNION
SELECT
DATE_FORMAT(StockInwardDate, '%b-%Y') AS Month,
COUNT(*) as pcs,
ROUND(SUM(Carat),2) as carat,
ROUND(SUM(TotalAmount),2) as amount
FROM tbl_stock
GROUP BY DATE_FORMAT(StockInwardDate, '%m-%Y')
) as tbl
GROUP BY tbl.Month
ORDER by tbl.Month DESC
This above is my query but tbl.Month in order by which is equals to DATE_FORMAT(StockInwardDate, '%m-%Y') But instead of DATE_FORMAT(StockInwardDate, '%m-%Y') I want to get order by StockInwardDate only this
Is there any way to get This in order by StockInwardDate

SELECT query order by month

Here is my query what i have tried:
SELECT DATENAME(MONTH,timeofpurchase) as 'Month',
COUNT(*) 'Transactions', SUM(totalprice) as 'Total Sales'
FROM transactions
GROUP BY DATENAME(MONTH,timeofpurchase)
ORDER BY DATENAME(MONTH,timeofpurchase) DESC
A simple workaround I can think of is to build another table that stores 1 and January, 2 and February, and so on. Then you do a join and order by the numerical column.
Check for more information: ORDER BY FIELD
This should help:
SELECT DATENAME(MONTH,timeofpurchase) as 'Month',
COUNT(*) 'Transactions', SUM(totalprice) as 'Total Sales'
FROM transactions
GROUP BY DATENAME(MONTH,timeofpurchase)
ORDER BY FIELD(MONTH,'January','February','March','April',...,'December') DESC;

Sum of Counts from successive dates

I need to get the total number of an item for every month.
So far, with the following code :
Select Count(mpay_collector_company.id) As `Number of Collector Companies`,
Month(mpay_collector_company.created_at) As Month,
Year(mpay_collector_company.created_at) As Year
From mpay_collector_company
Group By Month(mpay_collector_company.created_at),
Year(mpay_collector_company.created_at)
I have the following response :
#|Year|Month
------------
5|2014|11
3|2014|12
3|2015|1
7|2015|2
6|2015|3
2|2015|4
5|2015|6
1|2015|7
And instead of the number for each month I'd like to have the sum from the beginning for each month, which would be something like this :
Sum|Year|Month
--------------
5|2014|11
8|2014|12
11|2015|1
18|2015|2
24|2015|3
26|2015|4
31|2015|6
32|2015|7
Any ideas ?
EDIT : This request will be implemented as a view, so sub-requests are pretty much a no-go :x
Something like this could work:
Select Count(distinct previous_mpay_collector_company.id)+ Count(distinct mpay_collector_company.id) As `Number of Collector Companies`,
Month(mpay_collector_company.created_at) As Month,
Year(mpay_collector_company.created_at) As Year
From mpay_collector_company
Left Join mpay_collector_company previous_mpay_collector_company
On Year(mpay_collector_company.created_at) > Year(previous_mpay_collector_company.created_at)
OR (Month(mpay_collector_company.created_at) > Month(previous_mpay_collector_company.created_at)
And Year(mpay_collector_company.created_at) >= Year(previous_mpay_collector_company.created_at))
And previous_mpay_collector_company.id <> mpay_collector_company.id
Group By Month(mpay_collector_company.created_at),
Year(mpay_collector_company.created_at);
The trick would be self joining to all of the previous months and counting from the joined table.
You can first select distinct year.months and then check count for each value
select year,month ,
( SELECT count(*) FROM mpay_collector_company WHERE
Year(mpay_collector_company.created_at) <= temp1.year
AND
Month(mpay_collector_company.created_at) <= temp1.month
)
from (
Select DISTINCT
Month(mpay_collector_company.created_at) As month,
Year(mpay_collector_company.created_at) As year
From mpay_collector_company
) as temp1
ORDER BY year,month
if you want to "reset" for each year , just change comparison to EQUAL between years and thats it
select year,month ,
( SELECT count(*) FROM mpay_collector_company WHERE
Year(mpay_collector_company.created_at) = temp1.year
AND
Month(mpay_collector_company.created_at) <= temp1.month
)
from (
Select DISTINCT
Month(mpay_collector_company.created_at) As month,
Year(mpay_collector_company.created_at) As year
From mpay_collector_company
) as temp1
ORDER BY year,month
Using a correlated subquery to count the number of rows with a date before the current should work too:
Select
Count(id) As "Number of Collector Companies",
Month(created_at) As Month,
Year(created_at) As Year,
(
Select Count(*)
From mpay_collector_company
Where EXTRACT(YEAR_MONTH from created_at) <= EXTRACT(YEAR_MONTH from m.created_at)
) as running_count
From mpay_collector_company m
Group By Year(created_at), Month(created_at);
Sample SQL Fiddle showing it in action
Select
(Select Sum(Count(Z.id)) FROM mpay_collector_company z
where z.created_at <= A.created_at) as '#',
Month(A.created_at) As Month,
Year(A.created_at) As Year
From mpay_collector_company A
Group By Month(mpay_collector_company.created_at),
Year(mpay_collector_company.created_at)
This should do it for ya,

How to preserve grouping in subquery

Hello i have a sql query like this:
SELECT
COUNT(*) AS total,
COUNT(referrer) AS referrer,
DATE_FORMAT(created_at, "%Y") AS YEAR,
DATE_FORMAT(created_at, "%m") AS MONTH,
DATE_FORMAT(created_at, "%d") AS DAY
FROM
users
GROUP BY
EXTRACT(DAY FROM created_at),
EXTRACT(MONTH FROM created_at),
EXTRACT(YEAR FROM created_at)
ORDER BY
created_at ASC
This gives me table with totals sorted by day (month and year). what i need is to add a subquery to display one more count() which has a condition. Something like:
(SELECT count(*) FROM users WHERE result = '1') as winners
the problem obviously is with the grouping of main query since i get same result without grouping the subquery for every row.
What would be the right way to execute such query?
How about
SELECT
COUNT(*) AS total,
COUNT(referrer) AS referrer,
SUM(case when result = '1' then 1 else 0 end) as winners
DATE_FORMAT(created_at, "%Y") AS YEAR,
DATE_FORMAT(created_at, "%m") AS MONTH,
DATE_FORMAT(created_at, "%d") AS DAY
FROM
users
GROUP BY
EXTRACT(DAY FROM created_at),
EXTRACT(MONTH FROM created_at),
EXTRACT(YEAR FROM created_at)
ORDER BY
created_at ASC;
Assuming created_at is a timestamp, we could re-write your query with just one field to group by as follows:
SELECT
COUNT(*) AS total,
COUNT(referrer) AS referrer,
SUM(case when result = '1' then 1 else 0 end) as winners
DATE_FORMAT(created_at, "%Y") AS YEAR,
DATE_FORMAT(created_at, "%m") AS MONTH,
DATE_FORMAT(created_at, "%d") AS DAY
FROM
users
GROUP BY date(created_at)
ORDER BY
created_at ASC;
Please note I do not have mysql to hand.
You can join both queries with UNION
SELECT
COUNT(*) AS total,
COUNT(referrer) AS referrer,
DATE_FORMAT(created_at, "%Y") AS YEAR,
DATE_FORMAT(created_at, "%m") AS MONTH,
DATE_FORMAT(created_at, "%d") AS DAY
FROM
users
GROUP BY
EXTRACT(DAY FROM created_at),
EXTRACT(MONTH FROM created_at),
EXTRACT(YEAR FROM created_at)
ORDER BY
created_at ASC
UNION
SELECT count(*) FROM users
GROUP BY
EXTRACT(DAY FROM created_at),
EXTRACT(MONTH FROM created_at),
EXTRACT(YEAR FROM created_at)
HAVING result = '1'

SQL Query with union and join

my set-up is like this
Table1: company_group - company_id, company_name
Table2: store - store_id, store_name
Table3: sales - sales_id, company_id, store_id, date, sales
Table4: wh_sales - wh_sale_id, company_id, store_id, date, sales
Table5: purchase - purchase_id, company_id, store_id, date, purchase
Now I am able to get the data for the first four tables using the select and union query, but I can't make out how and which join I should use to get the data for the table5 in the same table
I am using the query for the first four tables like
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'
now how can I include the Table5 so that I can get the result like
company_name store_name month sales purchase
company-a store-c December 40000 45000
Use the following query
select `company_name, `store_name`, MONTH, sum(`sales`) as sales, sum(purchase) as purchase from (
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales` , 0 as purchase
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, 0 as purchase
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH, 0 as sales, purchase
from company_group, store,purchase
where `company_group`.`company.id`=`purchase`.`company.id`
and `store`.`store.id`=`purchase`.`store.id`) a
group by company_name,store_name, 'MONTH'
The group by clause is not needed for the inner queries since there is no group function. I have moved it to the outermost query
SELECT t1.`company_name`, t1.`store_name`,t1.`MONTH`,t1.`sales`, `purchase`.`purchase` FROM (
select `company_group`.`company_id`, `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`
from company_group, store,sales
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_id`,`company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`
from company_group, store,wh_sales
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH') AS t1
LEFT JOIN `purchase`
ON t1.`company_id`=`purchase`.`company_id`
AND t1.`store_id`=`purchase`.`store_id`
Not sure if this is valid syntax specifically for MySQL, but you should get the idea anyway.
UPD: Rewritten the query fixing errors mentioned in comments
Sales vs whole & sales, displaying the purchase price for both right ?
Try this one.
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`sales`.`sales`, `purchase`.`purchase`
from company_group, store,sales, purchase
where `company_group`.`company.id`=`sales`.`company.id`
and `store`.`store.id`=`sales`.`store.id`
group by company_name,store_name, 'MONTH'
UNION
select `company_group`.`company_name, `store`.`store_name`, MONTHNAME(date) AS MONTH,`wh_sales`.`sales`, `purchase`.`purchase`
from company_group, store,wh_sales, purchase
where `company_group`.`company.id`=`wh_sales`.`company.id`
and `store`.`store.id`=`wh_sales`.`store.id`
group by company_name,store_name, 'MONTH'
Adding purchase.purchase to the SELECT clause and the purchase TABLE to the FROM clause provides access to the table's purchase price field. Which, according to your projected table's purchase column is essential AND the simplest method.