1

This is kinda hard to explain so I'll do this step by step. Below is the table i've created.

id  | item_1 | item_2 | item_3|
32  |   1    |   43   |  54   |
32  |   54   |   32   |  32   |
67  |   42   |   45   |  12   |

As you can see, the first 2 rows has the same id, my goal is, get the sum of the first row which is (1+43+54), and the sum of second row which is (54+32+32), then add both rows with the same ID's and sort them up from highest to lowest. Can someone help me with this?

2
  • sum of the first row which is (1+42+54)? it should be (1+43+54), right? Commented Jul 7, 2013 at 4:10
  • Oh! didn't see that, my fault! Commented Jul 7, 2013 at 4:11

2 Answers 2

6

I think what you are looking for is

 select 
      id, 
      sum(item_1+item_2+item_3) as item_sum 
 from yourtable 
 group by id 
 order by item_sum desc;
Sign up to request clarification or add additional context in comments.

1 Comment

This solved my problem, thanks! I'll mark your answer correct in 7 min
4

I would do it as follows:

SELECT 
    ID, 
    SUM(Total) as TotalSum
FROM 
    (
        SELECT ID, ITEM_1 + ITEM_2 + ITEM_3 as Total 
        FROM 
            MyTable
    )
GROUP BY ID
ORDER BY TotalSum DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.