KNOWLEDGE BASE

Restructuring Data Using Custom SQL


Published: 10 Mar 2014
Last Modified Date: 28 Jul 2016

Question

How to restructure data using custom SQL.

Environment

Tableau Desktop

Answer

The following instructions can be reviewed in the workbook attached below.
  1. Connect to data source.
  2. Click Custom SQL.
  3. Click ... to edit the custom SQL statement.
  4. Enter a statement similar to the statement below:
    SELECT [Sheet1$].[Fruit Batch ID] AS [Fruit Batch ID],
      [Sheet1$].[Number of Bad Fruit_Bruised] AS [Value],
      "Bruised Fruit" AS [Reason]
    FROM [Sheet1$]
    
    UNION ALL
    
    SELECT [Sheet1$].[Fruit Batch ID] AS [Fruit Batch ID],
      [Sheet1$].[Number of Bad Fruit_Rotten] AS [Value],
      "Rotten Fruit" AS [Reason]
    FROM [Sheet1$]
    
    UNION ALL
    
    SELECT [Sheet1$].[Fruit Batch ID] AS [Fruit Batch ID],
      [Sheet1$].[Number of Bad Fruit_Too Big] AS [Value],
      "Too Big" AS [Reason]
    FROM [Sheet1$]
    
    UNION ALL
    
    SELECT [Sheet1$].[Fruit Batch ID] AS [Fruit Batch ID],
      [Sheet1$].[Number of Bad Fruit_Too Small] AS [Value],
      "Too Small" AS [Reason]
    FROM [Sheet1$]
  5. Click OK.
Did this article resolve the issue?