Answer the question
In order to leave comments, you need to log in
How to correctly implement a construct with a variable in Postgresql IF?
And so, there is a table of the form:
Table1
| id | column1 | column2 |
| 1 | sss | sss|
| 2 | sss | sss|
| 3 | sss | sss|
| 4 | sss | ses|
| 5 | ses | ses|
| 6 | ses | sew|
| 7 | sew | seq|
| id | column1 | column2 | column3 |
| 1 | sss | sss| 1 |
| 2 | sss | sss| 1 |
| 3 | sss | sss| 1 |
| 4 | sss | ses| 2 |
| 5 | ses | ses| 2 |
| 6 | ses | sew| 3 |
| 7 | sew | seq| 4 |
select 1::int as I into temp table myvar; # создал переменную; воспользовался temp таблицей
select
"id",
"column1",
"column2",
IF(("column2" <> "column1"), v.I++, v.I) AS "track" #как бы мог выглядеть синтаксис с инкрементацией,
# переменную v.I по условию увеличивать, то есть увеличить на 1, если условие верно.
from table1 , myvar v
Answer the question
In order to leave comments, you need to log in
select id,
column1,
column2,
count(
case when column1 <> column2
then 1
else null end
) over(order by id)
from table1;
There is a special window function for this case:
select id,
column1,
column2,
dense_rank() over(order by column1, column2)
from table1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question