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 2012 Forums
 Transact-SQL (2012)
 Return records with lower Average Sale Price compa

Author  Topic 

jtw5074
Starting Member

4 Posts

Posted - 2013-06-28 : 12:33:21
I am trying to create a query that will return the results in which the Average Sale Price (ASP) from 2013 is lower than the ASP from 2012. My attempt is below. Any suggestions? Thanks very much.

SELECT
[Year]
,[SKU Number]
,[Country]
,[Units]
,[Sales]
,[ASP]
FROM [Wirt].[dbo].[Basal_Media_2]
ORDER BY [SKU Number]
,[Country]
,[Year]
WHERE [ASP] for [YEAR] = '2012' > [ASP] for [YEAR] = '2013'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-28 : 12:40:00
[code]
SELECT
[Year]
,[SKU Number]
,[Country]
,[Units]
,[Sales]
,m.[ASP]
FROM [Wirt].[dbo].[Basal_Media_2] m
CROSS APPLY (SELECT [ASP]
FROM [Wirt].[dbo].[Basal_Media_2]
WHERE [SKU Number] = m.[SKU Number]
AND [Country] = m.[Country]
AND [Year] = m.[Year] -1
)m1
WHERE m.[ASP] < m1.[ASP]
AND m.[Year] = 2013
ORDER BY [SKU Number]
,[Country]
,[Year]
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

jtw5074
Starting Member

4 Posts

Posted - 2013-06-28 : 13:00:01
Thank you sir, that did the trick.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-28 : 13:22:02
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -