I have a table with columns (Id, Prod, Id) and I must create a (Flag) like this: This (Flag) is created by grouping by (Id2) and assigning 0 for the lowest (Id) and 1 otherwise. I tried using group by to no avail. How can I do that? Answer You can use a case expression with a windowed function: Example