Select multiple sums with MySQL query and display them in separate columns

后端 未结 6 1168
无人及你
无人及你 2021-01-02 03:38

Let\'s say I have a hypothetical table like so that records when some player in some game scores a point:

name   points
------------
bob     10
mike    03
mi         


        
6条回答
  •  余生分开走
    2021-01-02 04:14

    you can use pivot function also for the same thing .. even by performance vise it is better option to use pivot for pivoting... (i am talking about oracle database)..

    you can use following query for this as well.. -- (if you have only these two column in you table then it will be good to see output else for other additional column you will get null values)

    select * from  game_scores
    pivot (sum(points) for name in ('BOB' BOB, 'mike' MIKE));
    

    in this query you will get data very fast and you have to add or remove player name only one place

    :)

    if you have more then these two column in your table then you can use following query

     WITH pivot_data AS (
                SELECT points,name 
      FROM   game_scores
      )
      SELECT *
      FROM   pivot_data
    pivot (sum(points) for name in ('BOB' BOB, 'mike' MIKE));
    

提交回复
热议问题