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?

4

2 回答 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;
于 2013-07-07T04:12:24.540 回答
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
于 2013-07-07T04:11:37.023 回答