Pivot function conversion from Oracle to T-Sql

braxx 456 Reputation points
2020-11-10T16:18:34.913+00:00

I having a problem properly converting below sql statement from Oracle to SQL Server.
Error: Incorrect syntax near '67'.

SELECT
  PROD_ID,
  RDS_SECTOR,
  RDS_SUB_SECTOR,
  RDS_CATEGORY,
  RDS_SEGMENT,
  RDS_BRAND,
  RDS_SUB_BRAND,
  RDS_FORM_GLOBL
FROM
  (
    select
      PROD_ID,
      PROD_ATTR_TYPE_ID,
      ATTR_VAL
    from
      [BSR_STG_Q].[Cdl_Prod_Attr_Dim] ad
    where
      CURR_IND = 'Y'
      AND PROD_ATTR_END_DATE = '9999-12-31 00:00:00'
  ) pv PIVOT (
    max(ATTR_VAL) for PROD_ATTR_TYPE_ID in (
      67 AS RDS_SECTOR,
      108 AS RDS_SUB_SECTOR,
      63 AS RDS_CATEGORY,
      61 AS RDS_SEGMENT,
      66 AS RDS_BRAND,
      418 AS RDS_SUB_BRAND,
      65 AS RDS_FORM_GLOBL
    )
  )

I glad if someone could help

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Tom Cooper 8,481 Reputation points
    2020-11-10T16:56:53.3+00:00
         SELECT
           PROD_ID,
               [67] AS RDS_SECTOR,
               [108] AS RDS_SUB_SECTOR,
               [63] AS RDS_CATEGORY,
               [61] AS RDS_SEGMENT,
               [66] AS RDS_BRAND,
               [418] AS RDS_SUB_BRAND,
               [65] AS RDS_FORM_GLOBL
         FROM
           (
             select
               PROD_ID,
               PROD_ATTR_TYPE_ID,
               ATTR_VAL
             from
               [BSR_STG_Q].[Cdl_Prod_Attr_Dim] ad
             where
               CURR_IND = 'Y'
               AND PROD_ATTR_END_DATE = '9999-12-31 00:00:00'
           ) pv PIVOT (
             max(ATTR_VAL) for PROD_ATTR_TYPE_ID in (
               [67],
               [108],
               [63],
               [61],
               [66],
               [418],
               [65]
             )
           ) pvt
    

    Tom

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,221 Reputation points
    2020-11-11T01:44:31.167+00:00

    Hi @BartoszWachocki-4076,

    Thank you so much for posting here.

    Tom already provided the correct answer.

    You could refer more details and examples from below article:
    Using PIVOT and UNPIVOT

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues October--Users always get connection timeout problem when using multi subnet AG via listener. Especially after failover to another subnet

    0 comments No comments

  2. braxx 456 Reputation points
    2020-11-12T10:13:05.793+00:00

    Looks good to me. Thanks a lot!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.