0

So I have a table that looks like this:

thing date date_type
1 1/12 start_date
1 1/14 end_date
2 2/14 start_date
2 2/15 end_date

And I'm trying to compress everything down to look like this:

thing start_date end_date
1 1/12 1/14
2 2/14 2/15

What is the best way to write this query? I'm relatively new to SQL so I'm not sure what the best way to word this is. I was thinking something like this:

SELECT thing, date AS start_date, date AS end_date
FROM t1 ON...

I'm sure there's a simple way to do this, I'm just unfamiliar with SQL.

  • Does this answer your question? [SQL - How to transpose?](https://stackoverflow.com/questions/3392956/sql-how-to-transpose) – TheWildHealer Jun 16 '21 at 07:42

1 Answers1

1

Just use conditional aggregation:

select thing,
       max(case when data_type = 'start_date' then date end) as start_date,
       max(case when data_type = 'end_date' then date end) as end_date
from t
group by thing;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786