I have CustomerSession table in sql. Need to do flagging based on some scenario

I have CustomerSession table in sql.
I want to do flagging for those user who have visited page 4 as 1 and rest as 0.

I have CustomerSession table in sql. Need to do flagging based on some scenario

result will be like this:

I have CustomerSession table in sql. Need to do flagging based on some scenario

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can do this with a simple exists check

select *, 
    case when exists (
        select * from CustomerSession t2 
        where t.user=t2.user and t2.page=4
    ) then 1 else 0 end flag_4
from CustomerSession t

Method 2

@Innaya, I’m a bit confused as to your question, but I’ll give it a shot… If what you are looking for is a query…

SELECT Session, [User], Page, flag_4 = CASE
   WHEN EXISTS (SELECT 1 FROM CustomerSession cs2 WHERE cs2.[User] = cs1.[User] and Page = '4') THEN 1 ELSE 0 END
FROM CustomerSession cs1

Fiddle


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x