Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Displaying monthwise sales details

Posted on 2006-04-11
5
Medium Priority
?
363 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:sureshraina
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16427005
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
0
 

Author Comment

by:sureshraina
ID: 16427160
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16427234
then you have to use the crosstab function (link is above), as this is a dynamic query (variable number of columns)
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 2000 total points
ID: 16427686
this is how you can create dynamic query

declare @startMonth as tinyint,
      @query as varchar(4000)
set @startMonth=  month('2006-03-0')
set @query ='select dealer_id '
while @startmonth <= month('2006-07-01')


begin
      select @query  = @query + ', sum ( case when datepart(month, order_date) =' +cast(startmonth as varchar(2))+ ' then order_price else 0 end ) as ' +
          case  @startMonth
            when 1 then 'Jan' when 2 then 'Feb' when 3 then 'Mar' when 4 then 'Apr'
            when 5 then 'May' when 6 then 'Jun' when 7 then 'Jul' when 8 then 'Aug'
            when 9 then 'Sep' when 10 then 'Oct' when 11 then 'Nov' when 12 then 'Dec' end
            
set @startmonth =@startmonth + 1
end

set @query = @query +' FROM yourtable
where order_date >= convert(datetime, ''2006-03-02'', 120) and order_date < convert(datetime, ''2006-07-01'', 120)
group by dealer_id'

exec ('@query')
0
 

Author Comment

by:sureshraina
ID: 16448468
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question