Quantcast
Channel: Teradata Forums - All forums
Viewing all articles
Browse latest Browse all 27759

Transpose Rows to columns with 1....N columns - response (3) by cwindland

$
0
0

I recently created the following dynamic SQL to generate collect statistics statements.  It basically converts row data into column data via an aggregate function and a derived table.
Here are all three statements.  Feel free to change them for your personal needs.
-- - DYNAMIC MULTICOLUMN STATISTICS
-- - Handles up to a 9 column stat.  Keep in mind only the first 16 bytes are collected prior to version 14.
SELECT
    TRIM
        (
      'COLLECT STATISTICS ON ' || TRIM(C.DatabaseName)|| '.'|| TRIM(C.TableName) ||' COLUMN ( '||
      CASE
          WHEN C.STATISTICS_COLUMN_1 IS NOT NULL
              THEN TRIM(C.STATISTICS_COLUMN_1)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_2 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_2)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_3 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_3)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_4 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_4)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_5 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_5)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_6 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_6)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_7 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_7)
          ELSE ''
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_8 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_8)
          ELSE ''          
      END ||
      CASE
          WHEN C.STATISTICS_COLUMN_9 IS NOT NULL
              THEN ', '||TRIM(C.STATISTICS_COLUMN_9)
          ELSE ''
      END
        )|| ' );' AS "DYNAMIC_STAT"
FROM
 (
  SELECT
   TRIM(DatabaseName) AS DatabaseName,
   TRIM(TableName)    AS TableName,
   StatisticsID,
   MAX
   (
    CASE
     WHEN ColumnPosition = 1
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_1,
   MAX
   (
    CASE
     WHEN ColumnPosition = 2
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_2,
   MAX
   (
    CASE
     WHEN ColumnPosition = 3
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_3,
   MAX
   (
    CASE
     WHEN ColumnPosition = 4
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_4,
   MAX
   (
    CASE
     WHEN ColumnPosition = 5
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_5,
   MAX
   (
    CASE
     WHEN ColumnPosition = 6
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_6,
   MAX
   (
    CASE
     WHEN ColumnPosition = 7
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_7,
   MAX
   (
    CASE
     WHEN ColumnPosition = 8
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_8,
   MAX
   (
    CASE
     WHEN ColumnPosition = 9
      THEN TRIM(ColumnName)
    END
   ) AS STATISTICS_COLUMN_9
  FROM
   "DBC".MultiColumnStats
  WHERE DatabaseName = <DatabaseName>
  AND TableName = <TableName>
  
  GROUP BY
   1,
   2,
   3
 ) C
ORDER BY
 1
-- - DYNAMIC MULTICOLUMN INDEX STATISTICS
-- - Handles up to a 35 column index
SELECT
 TRIM
  (
   'COLLECT STATISTICS ON ' || TRIM(C.DatabaseName)|| '.'|| TRIM(C.TableName) ||' COLUMN ( '||
   CASE
    WHEN C.ColumnName_1 IS NOT NULL
     THEN TRIM(C.ColumnName_1)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_2 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_2)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_3 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_3)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_4 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_4)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_5 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_5)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_6 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_6)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_7 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_7)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_8 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_8)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_9 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_9)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_10 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_10)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_11 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_11)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_12 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_12)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_13 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_13)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_14 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_14)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_15 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_15)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_16 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_16)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_17 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_17)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_18 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_18)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_19 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_19)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_20 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_20)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_21 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_21)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_22 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_22)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_23 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_23)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_24 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_24)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_25 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_25)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_26 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_26)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_27 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_27)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_28 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_28)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_29 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_29)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_30 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_30)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_31 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_31)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_32 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_32)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_33 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_33)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_34 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_34)
    ELSE ''
   END ||
   CASE
    WHEN C.ColumnName_35 IS NOT NULL
     THEN ', '||TRIM(C.ColumnName_35)
    ELSE ''
   END
  )|| ' );' AS "DYNAMIC_STAT"
FROM
 (
  SELECT
   TRIM(DatabaseName) AS DatabaseName,
   TRIM(TableName)    AS TableName,
   IndexNumber,
   MAX
   (
    CASE
     WHEN ColumnPosition = 1
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_1,
   MAX
   (
    CASE
     WHEN ColumnPosition = 2
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_2,
   MAX
   (
    CASE
     WHEN ColumnPosition = 3
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_3,
   MAX
   (
    CASE
     WHEN ColumnPosition = 4
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_4,
   MAX
   (
    CASE
     WHEN ColumnPosition = 5
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_5,
   MAX
   (
    CASE
     WHEN ColumnPosition = 6
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_6,
   MAX
   (
    CASE
     WHEN ColumnPosition = 7
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_7,
   MAX
   (
    CASE
     WHEN ColumnPosition = 8
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_8,
   MAX
   (
    CASE
     WHEN ColumnPosition = 9
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_9,
   MAX
   (
    CASE
     WHEN ColumnPosition = 10
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_10,
   MAX
   (
    CASE
     WHEN ColumnPosition = 11
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_11,
   MAX
   (
    CASE
     WHEN ColumnPosition = 12
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_12,
   MAX
   (
    CASE
     WHEN ColumnPosition = 13
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_13,
   MAX
   (
    CASE
     WHEN ColumnPosition = 14
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_14,
   MAX
   (
    CASE
     WHEN ColumnPosition = 15
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_15,
   MAX
   (
    CASE
     WHEN ColumnPosition = 16
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_16,
   MAX
   (
    CASE
     WHEN ColumnPosition = 17
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_17,
   MAX
   (
    CASE
     WHEN ColumnPosition = 18
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_18,
   MAX
   (
    CASE
     WHEN ColumnPosition = 19
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_19,
   MAX
   (
    CASE
     WHEN ColumnPosition = 20
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_20,
   MAX
   (
    CASE
     WHEN ColumnPosition = 21
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_21,
   MAX
   (
    CASE
     WHEN ColumnPosition = 22
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_22,
   MAX
   (
    CASE
     WHEN ColumnPosition = 23
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_23,
   MAX
   (
    CASE
     WHEN ColumnPosition = 24
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_24,
   MAX
   (
    CASE
     WHEN ColumnPosition = 25
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_25,
   MAX
   (
    CASE
     WHEN ColumnPosition = 26
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_26,
   MAX
   (
    CASE
     WHEN ColumnPosition = 27
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_27,
   MAX
   (
    CASE
     WHEN ColumnPosition = 28
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_28,
   MAX
   (
    CASE
     WHEN ColumnPosition = 29
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_29,
   MAX
   (
    CASE
     WHEN ColumnPosition = 30
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_30,
   MAX
   (
    CASE
     WHEN ColumnPosition = 31
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_31,
   MAX
   (
    CASE
     WHEN ColumnPosition = 32
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_32,
   MAX
   (
    CASE
     WHEN ColumnPosition = 33
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_33,
   MAX
   (
    CASE
     WHEN ColumnPosition = 34
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_34,
   MAX
   (
    CASE
     WHEN ColumnPosition = 35
      THEN TRIM(ColumnName)
    END
   ) AS ColumnName_35
  FROM
   DBC.IndexStats
  WHERE DatabaseName = <DatabaseName>
  AND TableName = <TableName>
  AND IndexType <> 'J'
  GROUP BY
   1,
   2,
   3
 ) C
ORDER BY
 1
-- - Dynamic Single Column Statistics
 SELECT
 TRIM(
   'COLLECT STATISTICS ON '
   || TRIM(DatabaseName)
   || '.'
   || TRIM(TableName)
   || ' COLUMN ( '
   || ColumnName
  )
   ||' );'
AS "StatisticsStatement"
FROM
 dbc.columnstats
WHERE DatabaseName = <DatabaseName>
AND TableName = <TableName>
ORDER BY ColumnName;
 


Viewing all articles
Browse latest Browse all 27759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>