I am trying to get Month wise trend of sales data from following table.
Table VendorTrading
:
Id TradeDate
1 2015-11-25
2 2015-12-10
2nd table CustomerProduct
:
VendorTradeId ProductName ProductQuantity
1 ABC 5
1 XYZ 0
1 QWE 3
2 ABC 2
2 XYZ 4
2 QWE 6
What I am trying to achieve is:
Product Jan-15 Feb-15 Mar-15 April-15 May-15 June-15 . . .
ABC ### #### ### #### #### ###
XYZ ### #### ### #### #### ###
QWE ### #### ### #### #### ###
Where parameter I want to pass to stored procedure is just year (2015)
This is what I have tried so far but doesn't retrieve expected results
select
CP.ProductName,
SUM( Cp.ProductQuantity ) as Quantity,
VT.Tradedate
from
VendorTrading VT
inner join
CustomerProducts CP on VT.Id = Cp.VendorTradingId
where
(VT.Tradedate between isnull(@StartDate, VT.Tradedate)
and isnull(@EndDate, VT.Tradedate))
group by
Cp.ProductName, VT.Tradedate
This is result of my query: