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
Final Fantasy, 100
Final Fantasy, 500
What I want to do is select the maximum of each of these values in a single statement. I know I can easily do something like
select data_type, max(value)
from table
where data_type = [insert each data type here for separate queries]
group by data_type
But what I want it to display is is
select data_type,
max(value) as 'World of Warcraft',
max(value) as 'Quake 3',
max(value) as 'Final Fantasy'
So I get the max value of each of these in a single statement. How would I go about doing this?
Once again, for more than just a few "data types", I suggest to use
crosstab()
:Returns:
More explanation for the basics:
PostgreSQL Crosstab Query
Dynamic solution
The tricky thing is to make this completely dynamic: to make it work for
At least the type is well known:
integer
in this case.In short: that's not possible with current PostgreSQL (including 9.3). There are approximations with polymorphic types and ways to circumvent the restrictions with arrays or hstore types. May be good enough for you. But it's strictly not possible to get the result with individual columns in a single SQL query. SQL is very rigid about types and wants to know what to expect back.
However, it can be done with two queries. The first one builds the actual query to use. Building on the above simple case:
This generates the query you actually need. Run the second one inside the same transaction to avoid concurrency issues.
Note the strategic use of
quote_literal()
andquote_ident()
to sanitize all kinds of illegal (for columns) names and prevent SQL injection.Don't get confused by multiple layers of dollar-quoting. That's necessary for building dynamic queries. I put it as simple as possible.
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:
See SQL Fiddle with Demo
If you want your data to be aggregated in single string, go with bluefeet example, if you need a recordset with a record for each type: