如何找到 table 中的所有 NUMERIC 列并对它们执行 SUM()?
How do I find all the NUMERIC columns in a table and do a SUM() on them?
我在 Netezza、DB2 和 PostgreSQL 数据库中有一些 tables,我需要协调它们,我们想出的最好方法是对所有 NUMERIC Table 所有 3 个数据库的列。
有没有人有快速简单的方法来找到所有的 COLUMNS,这些 COLUMNS 要么是 NUMERIC 要么 INTEGER 要么 BIGINT 然后 运行 一个 SUM() 对所有这些?
为了比较结果,我也可以手动进行,或者如果有人有办法将这些结果统一捕获table并自动检查 SUM 中的差异?
对于 Netezza,我收到以下查询:
SELECT 0 AS ATTNUM, 'SELECT' AS SQL
UNION
SELECT ATTNUM, 'SUM(' || ATTNAME || ') AS S_' || ATTNAME || ',' AS COLMN
FROM _V_RELATION_COLUMN RC
WHERE NAME = '<table-name>'
AND FORMAT_TYPE= 'NUMERIC'
UNION
SELECT 10000 AS ATTNUM, ' 0 AS FLAG FROM ' || '<table-name>'
ORDER BY ATTNUM
仍在研究如何跨 DB2 和 PostgreSQL 执行此操作。
对于 DB2,您可以使用此元数据,这将帮助您找出每一列的数据类型
SELECT
COLUMN_NAME || ' ' || REPLACE(REPLACE(DATA_TYPE,'DECIMAL','NUMERIC'),'CHARACTER','VARCHAR') ||
CASE
WHEN DATA_TYPE = 'TIMESTAMP' THEN ''
ELSE
' (' ||
CASE
WHEN CHARACTER_MAXIMUM_LENGTH IS NOT NULL THEN CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(30))
WHEN NUMERIC_PRECISION IS NOT NULL THEN CAST(NUMERIC_PRECISION AS VARCHAR(30)) ||
CASE
WHEN NUMERIC_SCALE = 0 THEN ''
ELSE ',' || CAST(NUMERIC_SCALE AS VARCHAR(3))
END
ELSE ''
END || ')'
END || ',' "SQLCOL",
COLUMN_NAME,
DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, ORDINAL_POSITION
FROM SYSIBM.COLUMNS
WHERE TABLE_NAME = 'insert your table name'
AND TABLE_SCHEMA = 'insert your table schema'
ORDER BY ORDINAL_POSITION
我在 Netezza、DB2 和 PostgreSQL 数据库中有一些 tables,我需要协调它们,我们想出的最好方法是对所有 NUMERIC Table 所有 3 个数据库的列。
有没有人有快速简单的方法来找到所有的 COLUMNS,这些 COLUMNS 要么是 NUMERIC 要么 INTEGER 要么 BIGINT 然后 运行 一个 SUM() 对所有这些?
为了比较结果,我也可以手动进行,或者如果有人有办法将这些结果统一捕获table并自动检查 SUM 中的差异?
对于 Netezza,我收到以下查询:
SELECT 0 AS ATTNUM, 'SELECT' AS SQL
UNION
SELECT ATTNUM, 'SUM(' || ATTNAME || ') AS S_' || ATTNAME || ',' AS COLMN
FROM _V_RELATION_COLUMN RC
WHERE NAME = '<table-name>'
AND FORMAT_TYPE= 'NUMERIC'
UNION
SELECT 10000 AS ATTNUM, ' 0 AS FLAG FROM ' || '<table-name>'
ORDER BY ATTNUM
仍在研究如何跨 DB2 和 PostgreSQL 执行此操作。
对于 DB2,您可以使用此元数据,这将帮助您找出每一列的数据类型
SELECT
COLUMN_NAME || ' ' || REPLACE(REPLACE(DATA_TYPE,'DECIMAL','NUMERIC'),'CHARACTER','VARCHAR') ||
CASE
WHEN DATA_TYPE = 'TIMESTAMP' THEN ''
ELSE
' (' ||
CASE
WHEN CHARACTER_MAXIMUM_LENGTH IS NOT NULL THEN CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(30))
WHEN NUMERIC_PRECISION IS NOT NULL THEN CAST(NUMERIC_PRECISION AS VARCHAR(30)) ||
CASE
WHEN NUMERIC_SCALE = 0 THEN ''
ELSE ',' || CAST(NUMERIC_SCALE AS VARCHAR(3))
END
ELSE ''
END || ')'
END || ',' "SQLCOL",
COLUMN_NAME,
DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, ORDINAL_POSITION
FROM SYSIBM.COLUMNS
WHERE TABLE_NAME = 'insert your table name'
AND TABLE_SCHEMA = 'insert your table schema'
ORDER BY ORDINAL_POSITION