Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 stored proc

Author  Topic 

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-05-12 : 00:59:35
Hi I have the following sql query which shows me the average sales for users in my organisation.
I'd like to create it as a Stored procedure where I can just enter a Year parameter. because at the moment i have to manually edit the dates and then run it.

SELECT Userid, Count (*) As Sales , SUM(CAST(Duration as float))as TotSalesAmount,AVG(CAST(Duration as float))as AVGSalesAmount, AVG(cast(ondate - createon as int) ) as AVGSalesCycle from conthist
WHERE Srectype = 'S' and Ondate >= '20000101' and Ondate <= '20101231'
GROUP BY Userid
Order by AVGSalesAmount desc

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 01:46:19
Give two parameter to stored procedure:
@startyear int,
@endyear int
and use them like this:

WHERE Srectype = 'S'
and Ondate >= dateadd(yy,@startyear-1900,0)
and Ondate <= dateadd(dd,-1,dateadd(yy,@endyear-1900+1,0))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 2010-05-12 : 02:00:58
will try this thanks
Go to Top of Page
   

- Advertisement -