Link to home
Start Free TrialLog in
Avatar of sureshraina
sureshraina

asked on

Displaying monthwise sales details

Hello,

I have salesorder table in which all the sales orders are stored. here is the design

Order_id   Order_qty   dealer_id   order_price  order_date

how do I display sum(order_price) based on the two dates monthwise.

What I mean is say i have two dates 01/01/2005 and  04/01/2005 it should fetch records in the below mentioned manner


Dealer_id     Jan'05     Feb'05    Mar'05    Apr'05
1                  1000       2000      40000      50000
2                  3000       1000      2000       10000

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this is called crosstab process, for which you can use the SQL Server 2005 TRANSFORM + PIVOT syntax, or some procedure like this in general:
http://searchsqlserver.techtarget.com/tip/1,289483,sid87_gci1131829,00.html?FromTaxonomy=%2Fpr%2F301764

if you want it for 1 year (12 months), something like this:


select dealer_id
, sum ( case when datepart(month, order_date) = 1 then order_price else 0 end ) as Jan05
, sum ( case when datepart(month, order_date) = 2 then order_price else 0 end ) as Fev05
, sum ( case when datepart(month, order_date) = 3 then order_price else 0 end ) as Mar05
...
FROM yourtable
where order_date >= convert(datetime, '2005-01-01', 120) and order_date < convert(datetime, '2006-01-01', 120)
group by dealer_id
Avatar of sureshraina
sureshraina

ASKER

Thanks for your response.

BUT, based on the dates provided only that number of months should be displayed.
For example if 01/01/2005 and 06/01/2005 are provided only 6 months should be show not the 12 months and 0s from the seventh month.

One more example
say if the dates passed are 03/02/06 and 07/02/06  then only months March,April, may june, july should be shown.

Pls help
then you have to use the crosstab function (link is above), as this is a dynamic query (variable number of columns)
ASKER CERTIFIED SOLUTION
Avatar of imran_fast
imran_fast

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello imran_fast,

there is an issue with this. though we are declaring @query it is again giving me an error saying @query must be declared.

pls help