Using OVER() if customer has watch gladiator then 1 else 0 SQL SERVER

前端 未结 4 484
攒了一身酷
攒了一身酷 2021-01-27 08:27

I think I need some guidance as to what is wrong in my query. I am trying to do

Watched_Gladiator=CASE WHEN FilmName IN (CASE WHEN FilmName LIKE \'%Gladiator%\'         


        
4条回答
  •  不知归路
    2021-01-27 08:38

    The following does what you want for all films:

    select r.*,
           (case when row_number() over (partition by filmname order by date) = 1
                 then 1 else 0
            end) as IsWatchedFirstAndGladiator
    from results r;
    

    For just Gladiator:

    select r.*,
           (case when filmname = 'Gladiator' and row_number() over (partition by filmname order by date) = 1
                 then 1 else 0
            end) as IsWatchedFirst
    from results r;
    

提交回复
热议问题