| | |
MS SQL query using Max
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
sql Syntax (Toggle Plain Text)
SELECT SDModelProdPrices.EffectiveDate, Products.ProdCode, Products.longDescr, SDModelProdPrices.UnitPrice, SDModels.Descr AS 'Model' FROM SPLive.dbo.ProdSubCategories ProdSubCategories, SPLive.dbo.Products Products, SPLive.dbo.SDModelProdPrices SDModelProdPrices, SPLive.dbo.SDModelProds SDModelProds, SPLive.dbo.SDModels SDModels, SPLive.dbo.Subdivisions Subdivisions WHERE SDModelProds.SDModelProdID = SDModelProdPrices.SDModelProdID AND SDModelProds.SDModelID = SDModels.SDModelID AND Products.ProductID = SDModelProds.ProductID AND ProdSubCategories.ProdSubCatID = Products.ProdSubCatID AND Subdivisions.SubdivisionID = SDModels.SubdivisionID AND ProdSubCategories.Descr = 'Value Series Products' AND SDModelProdPrices.EffectiveDate = MAX(SDModelProdPrices.EffectiveDate) GROUP BY Products.ProdCode, Products.longDescr, SDModelProdPrices.UnitPrice, SDModels.Descr, SDModelProds.Discontinued HAVING SDModels.Descr = 'Bedford' AND SDModelProds.Discontinued = 0 ORDER BY Products.ProdCode
Last edited by peter_budo; Jun 15th, 2008 at 12:25 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
•
•
•
•
...
WHERE...
AND SDModelProdPrices.EffectiveDate = MAX(SDModelProdPrices.EffectiveDate)
...
sql Syntax (Toggle Plain Text)
SELECT TOP 1 ...... ...... ORDER BY SDModelProdPrices.EffectiveDate desc
tesu
Last edited by tesuji; Jun 7th, 2008 at 12:41 pm.
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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
Return Data
ProductA 12.00 12/10/2002
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
sql Syntax (Toggle Plain Text)
SELECT TOP 1 Product, price, DATE FROM table WHERE SDModelProds.SDModelProdID = SDModelProdPrices.SDModelProdPriceID GROUP BY product, DATE ORDER BY DATE
Return Data
ProductA 12.00 12/10/2002
Last edited by peter_budo; Jun 15th, 2008 at 12:26 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Feb 2008
Posts: 42
Reputation:
Solved Threads: 13
MS SQL Syntax (Toggle Plain Text)
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
www.ex-Soft.tk
![]() |
Similar Threads
- SQL SERVER 2000 Selecting a record based on an aggregate function (MS SQL)
- SQL Query question over multiple tables (Oracle)
- Help with odd Query (MS SQL)
- retrieving a particular value with a sql query (PHP)
- Need Help on SQL Query for select statement (MS SQL)
- PHP/SQL query help (PHP)
Other Threads in the MS SQL Forum
- Previous Thread: Problem with SQL statement in Microsoft Access
- Next Thread: dynamic sql query...
Views: 4395 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last age autogrowth business connectingtodatabaseinuse count cursor data database dateadd datediff datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit live loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday





