1

I have a table that looks like this:

 date | title | count
------+-------+-------
04/20 |  foo  |  420
04/20 |  bar  |  194
04/21 |  foo  |  23
04/21 |  bar  |  100

I want to get a result like this:

 date | foo | bar 
------+-----+-----
04/20 | 420 | 194 
04/21 | 23  | 100

How can I do this in Redshift SQL? Thanks!

pnuts
  • 58,317
  • 11
  • 87
  • 139
Ian Macalinao
  • 1,608
  • 3
  • 20
  • 30
  • 1
    possible duplicate of [PostgreSQL Crosstab Query](http://stackoverflow.com/questions/3002499/postgresql-crosstab-query) – jpw Jun 24 '15 at 17:20
  • 1
    You shouldn't used protected words for field names (date and count) – Tingo Jun 24 '15 at 17:35

1 Answers1

2

I would think the simplest way to do it would be to use case when with group by like so:

SELECT "date",
       SUM(CASE WHEN title = 'foo' THEN "count" END) AS foo,
       SUM(CASE WHEN title = 'bar' THEN "count" END) AS bar
FROM yourTable
GROUP BY "date"
ORDER BY "date"
Stephan
  • 5,891
  • 1
  • 16
  • 24