Member_Id Interaction Status
971053019 1 1
971053019 2 1
971053019 3 1
971053019 4 0
971053019 1 1
971053019 2 0
971053019 1 0
971053019 1 0
971053019 1 1
971053019 2 1
971053019 3 0
971053019 1 0
For the above table i need to generate the 1's to the status column
The key note here is..., we need to find the maximum value in the interaction column ...for that MAX value we need to update the status column as 0... and for the remaining rows it should be 1.
I have tried with SQL Aggregated Max Value function by doing grouping but i am unable to make the expected result set. Kindly any one help me with the SQL Script.
I think this is a kind of gaps-and-island problem. You want to set the flag on rows interaction
is not greater that the "following" interaction
.
For the question to make sense, you need a column that defines the ordering of the rows. This is not showing in your sample data, so I assumed id
. Then, you can use lead()
:
select
member_id,
interaction,
case when lead(interaction, 1, interaction) (over(partition by member_id order by id) > interaction
then 1
else 0
end as status
from mytable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.