Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Crosstab two joined tables multiplies the data in each column ....

$
0
0

below is code that crosstabs monthy sales numbers and totals for each month.  So what I need to do is add a column that totals last years sales (table will be called [2013]) and add a column called 2013 YTD. I also need to add a calculation column that calculates growth from one year to another. So I join the 2013 table and add one last line that sums 2013 sales but i need only Jan-Mar at this point. in other words I just need to see the total sales for each dealer from 2013 Jan-Mar while still viewing Jan-Dec for the current year. So looking at the end product I need to see the Dealer Info, Jan, Feb, Mar, Apr......and at the end 2013 Sales and Growth. Thus, the Where clause. Unfortunately, each months sales are multiplied x 3 with or without the 2013 sum line. As soon as I join the tables the numbers get multiplied.

What am I missing??????


SELECT    substring([2014].Dealer,18,50)AS[Dealer Name],substring([2014].Dealer,9,1)AS[District],substring([2014].Dealer,11,6)AS[Dealer Code],

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 1 THEN [2014].[Sales Count]ELSE 0 END)ASJan,

           SUM(CASEWHEN[2014].[Transaction Sales Month]= 2 THEN [2014].[Sales Count]ELSE 0 END)ASFeb,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 3 THEN [2014].[Sales Count]ELSE 0 END)ASMar,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 4 THEN [2014].[Sales Count]ELSE 0 END)ASApr,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 5 THEN [2014].[Sales Count]ELSE 0 END)ASMay,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 6 THEN [2014].[Sales Count]ELSE 0 END)ASJun,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 7 THEN [2014].[Sales Count]ELSE 0 END)ASJul,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 8 THEN [2014].[Sales Count]ELSE 0 END)ASAug,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 9 THEN [2014].[Sales Count]ELSE 0 END)ASSep,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 10 THEN [2014].[Sales Count]ELSE 0 END)ASOct,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 11 THEN [2014].[Sales Count]ELSE 0 END)ASNov,

                    SUM(CASEWHEN[2014].[Transaction Sales Month]= 12 THEN [2014].[Sales Count]ELSE 0 END

ASDec,

                    SUM([2014].[Sales Count])AS[2014 Total],

FROM        dbo.[2014]

GROUPBYsubstring([2014].Dealer,18,50),substring([2014].Dealer,9,1),substring([2014].Dealer,11,6)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>