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

Time:10-06

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.

enter image description here

result will be like this:

enter image description here

CodePudding user response:

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

CodePudding user response:

@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

  • Related