SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Return records with lower Average Sale Price compa
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jtw5074
Starting Member

4 Posts

Posted - 06/28/2013 :  12:33:21  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 06/28/2013 :  12:40:00  Show Profile  Reply with Quote

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]


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

jtw5074
Starting Member

4 Posts

Posted - 06/28/2013 :  13:00:01  Show Profile  Reply with Quote
Thank you sir, that did the trick.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 06/28/2013 :  13:22:02  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000