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

前端 未结 4 479
攒了一身酷
攒了一身酷 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:43

    So you're saying that:

    SELECT Cust_Nr, Date, FilmName, 
           CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
    FROM YourTable
    WHERE YourColumn = @somevalue
    

    Doesn't work? Because according to the data you've given, it should.

    EDIT:

    Well based on Tim's comment, I would simply add this bit to the query.

       SELECT Cust_Nr, Date, FilmName, WatchedGladiator
       FROM
       (
          SELECT Cust_Nr, Date, FilmName, 
                 CASE WHEN FilmName LIKE '%Gladiator%' THEN 1 ELSE 0 END as WatchedGladiator
          FROM YourTable
          WHERE YourColumn = @somevalue
       ) as wg
       WHERE WatchedGladiator = 1
    

提交回复
热议问题