Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-3752

Add PIVOT operator to SQL

Rank to TopRank to BottomVotersWatch issueWatchersConvert to sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.26.0
    • Component/s: None

      Description

      Oracle SQL has PIVOT and UNPIVOT operators for cross-tab support.

      For example,

      SELECT *
      FROM   (SELECT customer_id, product_code, quantity
              FROM   pivot_test)
      PIVOT  (SUM(quantity) AS sum_quantity FOR (product_code) IN ('A' AS a, 'B' AS b, 'C' AS c))
      ORDER BY customer_id;
      
      CUSTOMER_ID A_SUM_QUANTITY B_SUM_QUANTITY C_SUM_QUANTITY
      ----------- -------------- -------------- --------------
                1             10             20             30
                2             40                            50
                3             60             70             80
                4            100
      
      4 rows selected.
      

      In Calcite we could implement this as a prepare-time rewrite, something like this:

      SELECT customer_id,
             SUM(DECODE(product_code, 'A', quantity, 0)) AS a_sum_quantity,
             SUM(DECODE(product_code, 'B', quantity, 0)) AS b_sum_quantity,
             SUM(DECODE(product_code, 'C', quantity, 0)) AS c_sum_quantity
      FROM   pivot_test
      GROUP BY customer_id
      ORDER BY customer_id;
      

        Attachments

        Issue Links

          Activity

          $i18n.getText('security.level.explanation', $currentSelection) Viewable by All Users
          Cancel

            People

            • Assignee:
              julianhyde Julian Hyde
              Reporter:
              julianhyde Julian Hyde

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h

                  Issue deployment