0

I'm just starting to learn SQL and I have a question that may seem simple. Given the following data:

step 1 - project 1
step 2 - project 1
step 3 - project 1
step 1 - project 2
step 2 - project 2
step 1 - project 3
step 1 - project 4
step 2 - project 4
step 3 - project 4
step 4 - project 4

I'm trying to find the last step for each project. I think I should be using a nest SQL statement but I'm not sure of the proper way to do it.

SimLaf
  • 3
  • 1
  • What's the data look like in the table? Columns\Datatypes. – Elias Sep 06 '13 at 13:48
  • Are the values stored as 'step 1' and 'project 1'? Or are those the column names and the values are integers? – Nicholas Post Sep 06 '13 at 13:48
  • 1
    you can use Group by clause to achieve results. – Vijay Sep 06 '13 at 13:48
  • Welcome to the world of the Structured Query Language! For future reference, for questions of this nature, it's often helpful to provide what your data actually looks like in the table. It's somewhat difficult to understand the relations otherwise. – K.Niemczyk Sep 06 '13 at 13:49

2 Answers2

2

Your query would look something like this:

SELECT project, max(step) as step
FROM Table_Name
GROUP BY project

The GROUP BY groups all projects together and the max() returns the max value. However, if your values are strings, you may have to remove the text and convert them to integers to properly sort and retrieve the proper max() value.

Nicholas Post
  • 1,857
  • 1
  • 18
  • 31
0

Assuming your table has 2 columns (Step, Project), then you can use

SELECT Project, MAX(Step) FROM Table GROUP BY Project
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101