Click here to Skip to main content
15,886,104 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am currently trying to do a report and pull the data from SQL

The problem i am having is that i cant seem to get the information i require


I have a view called listStage.
this view represents 5stages(tables) in my db
my statement is

SQL
Select max(date),Product,stage from liststage group by product,stage

Problem with this is it doesnt just return the maximum date in the view, i just need it to show the maximum date,product and stage.

The query returns the maximum date at each stage.

How can i get it to show the just maximum date,with product and stage?
Posted

Try
SQL
SELECT Date, Product,stage FROM liststage 
WHERE Date = (SELECT MAX(Date) FROM liststage)

--OR

SELECT TOP 1 Date, Product,stage FROM liststage 
ORDER BY Date DESC
 
Share this answer
 
v3
Comments
isi19 19-Feb-13 4:17am    
Thanks , but I need to show more than the last record with the maxdate, i need to select all the products,the max date and the stage ,

See the products go to stages, and when they are completed at a stage they go to another stage,to track history of where the product is i created a view listStage,so now i want to view all the products whats the current stage bassed on the date it was completed
__TR__ 19-Feb-13 6:25am    
I did not understand your requirement. If you are looking to get the Product,stage and the date it was completed the query posted in your question might be correct.
It would be better if you can post some sample data and let us know the issue you are facing with the query you have written.
SELECT TOP 1 Date, Product,stage FROM liststage ORDER BY Date DESC
 
Share this answer
 
Is the 'date' column datatype is DATETIME if not try this one

SELECT MAX(CAST([date] AS DATETIME)) AS MaxDate,
       Product,
       stage
FROM   liststage
GROUP BY
       product,
       stage
ORDER BY
       MAX(CAST([date] AS DATETIME)) DESC


Hope this will work.
 
Share this answer
 
v2
Comments
John Y. 19-Feb-13 9:18am    
Does the view then have all the products with all its various dates and stages? And you just want to see the products at their most recent date/stage?
If want to get max date and max stage, try this:
SQL
SELECT [Product], MAX([date]), MAX([stage])
FROM liststage
ORDER BY [product]
GROUP BY [product]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900