User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,994 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 1446 | Replies: 5
Reply
Join Date: Jun 2008
Posts: 3
Reputation: mhaskell is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mhaskell mhaskell is offline Offline
Newbie Poster

MS SQL query using Max

  #1  
Jun 7th, 2008
I need to write a query that will pull my products and price. However each of the products have multiple prices and I want the price with the most recent date. I have tried the following query but it doesn't work. Can someone assist me. I'm fairly new to sql queries.
  1. SELECT
  2. SDModelProdPrices.EffectiveDate,
  3. Products.ProdCode,
  4. Products.longDescr,
  5. SDModelProdPrices.UnitPrice,
  6. SDModels.Descr AS 'Model'
  7. FROM
  8. SPLive.dbo.ProdSubCategories ProdSubCategories,
  9. SPLive.dbo.Products Products,
  10. SPLive.dbo.SDModelProdPrices SDModelProdPrices,
  11. SPLive.dbo.SDModelProds SDModelProds,
  12. SPLive.dbo.SDModels SDModels,
  13. SPLive.dbo.Subdivisions Subdivisions
  14. WHERE
  15. SDModelProds.SDModelProdID = SDModelProdPrices.SDModelProdID
  16. AND SDModelProds.SDModelID = SDModels.SDModelID
  17. AND Products.ProductID = SDModelProds.ProductID
  18. AND ProdSubCategories.ProdSubCatID = Products.ProdSubCatID
  19. AND Subdivisions.SubdivisionID = SDModels.SubdivisionID
  20. AND ProdSubCategories.Descr = 'Value Series Products'
  21. AND SDModelProdPrices.EffectiveDate = MAX(SDModelProdPrices.EffectiveDate)
  22. GROUP BY
  23. Products.ProdCode,
  24. Products.longDescr,
  25. SDModelProdPrices.UnitPrice,
  26. SDModels.Descr,
  27. SDModelProds.Discontinued
  28. HAVING
  29. SDModels.Descr = 'Bedford'
  30. AND SDModelProds.Discontinued = 0
  31. ORDER BY Products.ProdCode
Last edited by peter_budo : Jun 15th, 2008 at 11:25 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: MS SQL query using Max

  #2  
Jun 7th, 2008
Originally Posted by mhaskell View Post
...
WHERE...
AND SDModelProdPrices.EffectiveDate = MAX(SDModelProdPrices.EffectiveDate)
...

Aggregate functions are not allowed in WHERE clause. So aggregate functions cannot be used for selecting specific rows. However, you can use them in HAVING clause. For MS Sql Server one solution to get the most recent date values is:
  1. SELECT TOP 1 ......
  2. ......
  3. ORDER BY SDModelProdPrices.EffectiveDate DESC
krs,
tesu
Last edited by tesuji : Jun 7th, 2008 at 11:41 am.
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: mhaskell is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mhaskell mhaskell is offline Offline
Newbie Poster

Re: MS SQL query using Max

  #3  
Jun 7th, 2008
I think I'm closer would you mind to help me further?
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: mhaskell is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mhaskell mhaskell is offline Offline
Newbie Poster

Re: MS SQL query using Max

  #4  
Jun 7th, 2008
Data information
ProductA 12.00 12/10/2002
ProductA 12.50 01/15/2005
ProductA 12.01 01/02/2008
ProductB 15.12 12/01/2005
ProductB 16.00 01/09/2008
ProductB 16.24 01/15/2008



I want to return

ProductA 12.01 01/02/2008
ProductB 16.24 01/15/2008


I have tried this query it returns the oldest date of all of the products

  1. SELECT TOP 1 Product, price, date
  2. FROM TABLE
  3. WHERE SDModelProds.SDModelProdID = SDModelProdPrices.SDModelProdPriceID
  4. GROUP BY product, date
  5. ORDER BY date

Return Data
ProductA 12.00 12/10/2002
Last edited by peter_budo : Jun 15th, 2008 at 11:26 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: MS SQL query using Max

  #5  
Jun 7th, 2008
. . . ORDER BY date DESC
Reply With Quote  
Join Date: Feb 2008
Posts: 14
Reputation: huangzhi is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
huangzhi huangzhi is offline Offline
Newbie Poster

Re: MS SQL query using Max

  #6  
Jun 9th, 2008
create table #tmp (Code varchar(255), Price money, Date smalldatetime)

insert #tmp (Code, Price, Date)
select 'ProductA', 12.00, '12/10/2002' union all
select 'ProductA', 12.50, '01/15/2005' union all
select 'ProductA', 12.01, '01/02/2008' union all
select 'ProductB', 15.12, '12/01/2005' union all
select 'ProductB', 16.00, '01/09/2008' union all
select 'ProductB', 16.24, '01/15/2008'

select	T.*
  from	#tmp T
  where Date = (select max(Date) from #tmp X where X.Code = T.Code)

drop table #tmp
Hence Wijaya
www.ex-Soft.tk
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 9:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC