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.
Author |
Topic |
vandana
Starting Member
29 Posts |
Posted - 2013-10-15 : 01:53:35
|
Hi All,I am struck up in writing a query !!!!i have a table with product_name and introduction_date(when the product was first introduced)as columns. now i wana calculate average as below if item is sold in previous business year(suppose 2011-12) then avg should be avg price in businessyear(2010-11), if it is newly introduced(suppose 2013-14)then avg should be of current year(2013-14). Note:- business year Apr-marchThanks in advance |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-15 : 03:16:44
|
[code]SELECT product_name,AvgPriceFROM(SELECT product_name,ROW_NUMBER() OVER (PARTITION BY product_name ORDER BY DATEDIFF(yy,0,DATEADD(mm,-3,introduction_date))) AS Seq,AVG(Price) AS AvgPriceFROM tableWHERE Introduction_date > = DATEADD(yy,DATEDIFF(yy,0,GETDATE())-2,0)AND Introduction_date < DATEADD(yy,DATEDIFF(yy,0,GETDATE())+2,0)GROUP BY product_name,DATEDIFF(yy,0,DATEADD(mm,-3,introduction_date)))tWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|