简体   繁体   中英

The sum of two columns and only show the third column

I am trying to write and execute the SQL query that returns the top three records with the highest "score", where the "score" is the sum of two columns (let's call them X and Y). The result should have one column named score.

Here is what I did

%%sql
select X,Y,(X + Y) as score from survey
ORDER BY score DESC
LIMIT 3

I got the right answer but I only want the score column, not the x and y column too. Done. XY score 4 9 13 4 8 12 3 7 10

SELECT X, Y, (X+Y) ... 

gives the 3 columns since you have SELECT 3 things. Instead just SELECT what you need in your case,

SELECT (X + Y) as score from survey
ORDER BY score DESC
LIMIT 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM