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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
thank you so much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.