TSQL CASE with if comparison in SELECT statement

前端 未结 3 1670
走了就别回头了
走了就别回头了 2021-02-01 01:57

I would like to use CASE statement in SELECT.

I select from user table, and (as one attribute) I also use nested SQL:

SELECT 
   registrationDate, 
   (S         


        
3条回答
  •  温柔的废话
    2021-02-01 02:06

    Should be:

    SELECT registrationDate, 
           (SELECT CASE
            WHEN COUNT(*)< 2 THEN 'Ama'
            WHEN COUNT(*)< 5 THEN 'SemiAma' 
            WHEN COUNT(*)< 7 THEN 'Good'  
            WHEN COUNT(*)< 9 THEN 'Better' 
            WHEN COUNT(*)< 12 THEN 'Best'
            ELSE 'Outstanding'
            END as a FROM Articles 
            WHERE Articles.userId = Users.userId) as ranking,
            (SELECT COUNT(*) 
            FROM Articles 
            WHERE userId = Users.userId) as articleNumber,
    hobbies, etc...
    FROM USERS
    

提交回复
热议问题