Beginner SQL section: avoiding repeated expression

后端 未结 5 990
-上瘾入骨i
-上瘾入骨i 2021-02-14 15:45

I\'m entirely new at SQL, but let\'s say that on the StackExchange Data Explorer, I just want to list the top 15 users by reputation, and I wrote something like this:

         


        
5条回答
  •  野的像风
    2021-02-14 15:52

    You could simply re-write the WHERE clause

    where reputation > 10000
    

    This won't always be convenient. As an alternativly, you can use an inline view:

    SELECT
      a.DisplayName, a.Id, a.Reputation, a.RepInK 
    FROM 
       (
            SELECT  TOP 15  
              DisplayName, Id, Reputation, Reputation/1000 As RepInK 
            FROM 
              Users 
            ORDER BY Reputation DESC 
        ) a
    WHERE 
      a.RepInK > 10 
    

提交回复
热议问题