mySQL statement to compare two months data

Dear Experts,
I'd like to compare the total sales number by merchants and by mounth.
I use PHP and mySQL.

when I use below select statement it works perfectly fine but it only gives me November figures
SELECT merchant,sum(total) as mytotal, month FROM `sales` where month='11' group by merchant order by mytotal desc

Open in new window


How can I compare the two months data (10-11 months)? I'd like to see the merchants whom sales declines compared these two months.
How should I write the mySQL statement?
LVL 1
BRMarketingAsked:
Who is Participating?
 
Pawan KumarDatabase ExpertCommented:
Pls try this -

select * from 
(
	SELECT bayikodu, sum(toplam) NovSum, MAX(ay) ay2 FROM `satislar` 
	where ay='11' group by bayikodu
)t
INNER JOIN 
(
	SELECT bayikodu, sum(toplam) OctSum , MAX(ay) ay1 FROM `satislar` 
	where ay='10' group by bayikodu
)k ON t.bayikodu = k.bayikodu

 

Open in new window

1
 
Pawan KumarDatabase ExpertCommented:
Please use like this -

JOIN you can change based on your need /* INNER , LEFT or RIGHT or FULL JOIN */

select t.merchant,k.merchant,t.Novmytotal,k.Octmytotal,Novmonth,Octmonth from 
(
	SELECT merchant,sum(total) as Novmytotal, 11 Novmonth 
	FROM `sales` 
	where month='11' 
	group by merchant
)t
FULL JOIN 
(
	SELECT merchant,sum(total) as Octmytotal, 10 Octmonth
	FROM `sales` 
	where month='10' 
	group by merchant 
)k ON t.merchant = k.merchant

Open in new window

0
 
BRMarketingAuthor Commented:
Dear Pawan Kumar,

my actual table names are like below, I used your code on this, but it didn't work. Maybe I have done something wrong.

I select here 3 data. First one is the merchant id, second one is the sum(total), and the third one is the month which is in number format.

my working code is below. thank you

SELECT bayikodu, sum(toplam), ay FROM `satislar` where ay='11' group by bayikodu order by sum(toplam) desc

Open in new window

0
 
BRMarketingAuthor Commented:
thank you so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.