An introduction to UNPIVOT.

UNPIVOT enables you to see columns as rows in a query result.

In our sales pipeline we have both the create date and the close date for each deal. If we run the following query:

SELECT created_date,
      FROM sales_pipeline
     WHERE deal_stage = "Won"

We would get back a table whose first several rows looked like this:

account opportunity_id created_date close_date
Cancity 1C1I7A6R 2016-10-20 2017-03-01
Isdom Z063OYW0 2016-10-25 2017-03-11
Bioholding NL3JZH1Z 2016-11-04 2017-03-10
Donquadtech 0LVWSWEW 2016-11-25 2017-03-06
Sumace NHDTSMX2 2017-02-22 2017-03-02
Zencorporation PNL17RE9 2017-02-22 2017-06-28
Zathunicon 5EKN0BFN 2017-02-22 2017-05-15
Mathtouch R4LH95QS 2017-02-22 2017-03-03
Genco Pura Olive Oil Company REDGEUCP 2017-02-22 2017-03-10
Ron-tech 6KT5HAR6 2017-02-22 2017-03-05
Toughzap 48AJ0X9E 2017-02-22 2017-06-15
Warephase RA1UBXY9 2017-02-22 2017-03-24

By modifying this query with an UNPIVOT we could create a timeline for all the deals with when they were created and closed:

    SELECT account, opportunity_id, created_date, close_date
      FROM sales_pipeline
     WHERE deal_stage = "Won"
UNPIVOT ( date FOR date_type IN (created_date AS 'CREATED', close_date AS 'CLOSED') )
 ORDER BY account, opportunity_id, date
account opportunity_id date_type date
Acme Corporation 04LU4OPA CREATED 2017-04-10
Acme Corporation 04LU4OPA CLOSED 2017-06-11
Acme Corporation 0AXGVKN8 CREATED 2017-02-24
Acme Corporation 0AXGVKN8 CLOSED 2017-05-28
Acme Corporation 10QXTLQX CREATED 2017-05-15
Acme Corporation 10QXTLQX CLOSED 2017-06-25
Acme Corporation 35GT4NM9 CREATED 2017-10-25
Acme Corporation 35GT4NM9 CLOSED 2017-11-05
Acme Corporation 3JMB4J8E CREATED 2017-10-05
Acme Corporation 3JMB4J8E CLOSED 2017-10-07
Acme Corporation 56ZJUSNG CREATED 2017-05-28
Acme Corporation 56ZJUSNG CLOSED 2017-06-07
Acme Corporation 5NW73ZRY CREATED 2017-05-06
Acme Corporation 5NW73ZRY CLOSED 2017-05-22
Acme Corporation 6RJE18RW CREATED 2017-03-19
Acme Corporation 6RJE18RW CLOSED 2017-03-24
Acme Corporation 7ASDRLM5 CREATED 2017-03-29