Reputation: 1751
I have a table structure as below on Greenplum database:
Wish to change it to the following structure so as to support pie charts on Tableau.
Could some one help me out ? Thanks!
Upvotes: 1
Views: 2079
Reputation: 77737
The following would work for a static, known beforehand, set of metrics:
SELECT
t.Date,
x.Metric,
CASE x.Metric
WHEN 'metric1' THEN metric1_week
WHEN 'metric2' THEN metric2_week
END AS week_val,
CASE x.Metric
WHEN 'metric1' THEN metric1_13week
WHEN 'metric2' THEN metric2_13week
END AS "13week_val"
FROM
atable AS t
CROSS JOIN
(VALUES ('metric1'), ('metric2')) AS x (Metric)
;
You could build a dynamic query off the above to account for an unknown number of metrics. For that, you would need to read the metadata (probably the INFORMATION_SCHEMA.COLUMNS
system view) to build the dynamic bits, which are the VALUES list and the two CASE expressions, before embedding them into the query.
Upvotes: 0
Reputation: 11921
Just to make sure you know about this Tableau feature:
Once you have devised the SQL select statement that will unpivot the data the way you'd like, then you can tell Tableau to use that instead of a select * by editing the data connection and selecting the Custom SQL option.
The generic way to unpivot in your situation is to union together several select statements, unless your database offers a more efficient alternative as described in the blog entry that Revanayya cited.
Upvotes: 0
Reputation: 1144
SQL Server 2008 : Convert column value to row
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
Upvotes: 1
Reputation: 7889
Upvotes: 1