Simple Nested SQL statement

73 views Asked by At

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.

2

There are 2 answers

0
Nicholas Post On BEST ANSWER

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.

0
Scott Hunter On

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

SELECT Project, MAX(Step) FROM Table GROUP BY Project