Selecting multiple max() values using a single SQL statement

前端 未结 3 1230
梦谈多话
梦谈多话 2020-12-07 04:57

I have a table that has data that looks something like this:

data_type, value
World of Warcraft, 500
Quake 3, 1500
Quake 3, 1400
World of Warcraft, 1200
Fina         


        
3条回答
  •  失恋的感觉
    2020-12-07 05:16

    If you want to return the max value for each data_type in a separate column, then you should be able to use an aggregate function with a CASE expression:

    select
      max(case when data_type='World of Warcraft' then value end) WorldofWarcraft,
      max(case when data_type='Quake 3' then value end) Quake3,
      max(case when data_type='Final Fantasy' then value end) FinalFantasy
    from yourtable;
    

    See SQL Fiddle with Demo

提交回复
热议问题