问题
I'm very new to SQL...
I have data like below in table called versioniteration
. I want build a query in SQL (oracle) to get the out as in the second table. Please guide me how to do this?
Here the part get altered many times in given version, each time the changes made the iteration increases.
The part can also change to new version then the Iteration will be assigned (number).
| Name | Version | Iteration |
|===============|===============|============|
| Part 1 | A | 1 |
|:------------------------------------------:|
| Part 1 | A | 2 |
|:------------------------------------------:|
| Part 1 | A | 3 |
|:------------------------------------------:|
| Part 1 | B | 0 |
|:------------------------------------------:|
| Part 1 | B | 1 |
|:------------------------------------------:|
| Part 1 | B | 2 |
|:------------------------------------------:|
| Part 2 | D | 1 |
|:------------------------------------------:|
| Part 2 | D | 2 |
|:------------------------------------------:|
| Part 2 | D | 3 |
|:------------------------------------------:|
| Part 2 | C | 0 |
|:------------------------------------------:|
| Part 2 | C | 1 |
|:------------------------------------------:|
| Part 2 | C | 2 |
|:------------------------------------------:|
The out should be:
| Name | Version | Iteration |
|===============|===============|============|
| Part 1 | A | 3 |
|:------------------------------------------:|
| Part 1 | B | 2 |
|:------------------------------------------:|
| Part 2 | C | 2 |
|:------------------------------------------:|
| Part 2 | D | 3 |
|:------------------------------------------:|
回答1:
How said Gordon Linoff is basic group by
query. You should understand how work group by and add other features to query. For example, your task is group by
+ MAX
. Also look at this question.
Your query:
SELECT name
, version
, MAX(iteration)
FROM table1
GROUP BY name, version
来源:https://stackoverflow.com/questions/35353222/how-to-get-all-version-and-latest-iteration-of-object-in-the-sql