Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 conthistWHERE Srectype = 'S' and Ondate >= '20000101' and Ondate <= '20101231'GROUP BY UseridOrder 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 intand 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.