sql query

I am trying to write a query where I can get YTD totals for current year and previous Year for a Customer.  So I have 2 tables 1 called _sales15 and the other is _sales14.  This is what I have but I am not getting the write answer.  It's almost like it multiplying the answer by 10.

select _sales15.cmp_name, _sales15.sum(total) as total15, _sales14.sum(total) as total14
from _sales15 left join _sales14 on _sales15.cmp_name= _sales14.cmp_name
where _sales15.month <= 10..


Any Ideas where I am going wrong?
sharris_glascolAsked:
Who is Participating?
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.

Brian CroweDatabase AdministratorCommented:
The problem is that when you are joining records out by the cmp_name you are duplicating rows for each match.  You need to aggregate the tables first.

WITH cte14 AS
(
	SELECT cmp_name, SUM(total) AS total14
	FROM _sales14
	GROUP BY cmp_name
),
cte15 AS
(
	SELECT cmp_name, SUM(total) AS total15
	FROM _sales15
	GROUP BY cmp_name
)
SELECT T14.cmp_name, T14.Total14, T15.Total15
FROM cte14 AS T14
LEFT OUTER JOIN cte15 AS T15
	ON T14.cmp_name = T15.cmp_name

Open in new window

0
ste5anSenior DeveloperCommented:
What SQL Server version do you use?
0
sharris_glascolAuthor Commented:
That seems to work but how do I get the total if I only want to see YTD end of October
0
Brian CroweDatabase AdministratorCommented:
Add WHERE clauses to each of the cte's

WITH cte14 AS
(
	SELECT cmp_name, SUM(total) AS total14
	FROM _sales14
        WHERE [month] <= 10
	GROUP BY cmp_name
),
cte15 AS
(
	SELECT cmp_name, SUM(total) AS total15
	FROM _sales15
        WHERE [month] <= 10
	GROUP BY cmp_name
)
SELECT T14.cmp_name, T14.Total14, T15.Total15
FROM cte14 AS T14
LEFT OUTER JOIN cte15 AS T15
	ON T14.cmp_name = T15.cmp_name

Open in new window

0

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
Vitor MontalvãoMSSQL Senior EngineerCommented:
sharris_glascol, do you still need help with this question?
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
SSRS

From novice to tech pro — start learning today.

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.