[英]Insert table statistics in another table
I have the following table in a database (teradata/hive): 我在数据库(teradata / hive)中具有下表:
CREATE MULTISET TABLE DP_S.temp_a ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
id INTEGER,
val VARCHAR(200) CHARACTER SET LATIN CASESPECIFIC
)
NO PRIMARY INDEX;
I want to generate the following stats and insert them row by row in a stats table: 我想生成以下统计信息并将它们逐行插入到统计信息表中:
SEL 'temp_a' AS testing_table,
CAST(COUNT(*) AS NUMBER(30)) AS noofrows
, MIN(id) AS min_id
, MAX(id) AS max_id
, SUM(CASE WHEN id IS NULL THEN 1 ELSE 0 END) AS null_check_id
,SUM(CAST(id AS DECIMAL(30,8))) AS sumnum_id
, MIN(val) AS min_val
, MAX(val) AS max_val
,SUM(CAST(LENGTH(TRIM(val)) AS DECIMAL(30,8))) AS sumlen_val
FROM DP_S.temp_a;
DDL for stats table: 统计资料表的DDL:
CREATE MULTISET TABLE DP_S.SANITY_STATS ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
stats_date DATE DEFAULT CURRENT_DATE FORMAT 'YYYY-MM-DD',
TBL_name VARCHAR(200) CHARACTER SET LATIN CASESPECIFIC,
COL_name VARCHAR(200) CHARACTER SET LATIN CASESPECIFIC,
stats_type VARCHAR(200) CHARACTER SET LATIN CASESPECIFIC,
stats_value BIGINT
)
NO PRIMARY INDEX;
eg if temp_a is the following table 例如,如果temp_a是下表
id | val
1 | 'abc'
2 | 'def'
the stats table should contain the following information: stats表应包含以下信息:
Date | Table | Col | StatType | Value
2014-12-24 | temp_a | all | noofrows | 2
2014-12-24 | temp_a | id | min | 1
2014-12-24 | temp_a | id | max | 3
2014-12-24 | temp_a | id | sumnum | 3
2014-12-24 | temp_a | id | nullchk | 0
2014-12-24 | temp_a | val | min | 'abc'
2014-12-24 | temp_a | val | max | 'def'
2014-12-24 | temp_a | val | sumlen | 6
Is this possible to achieve in a single query? 是否可以在单个查询中实现? (I know i can run multiple inserts while selecting one stat from the table, but i guess that would be slow and cumbersome)
(我知道我可以从表中选择一个统计信息时运行多个插入,但是我想那会很慢而且很麻烦)
I suppose that should work. 我认为应该可以。 Sorry if misspelled something, don't have DB to check.
抱歉,如果拼写错误,没有数据库可以检查。
select
b.testing_table
, case b.fld
when 1 then a.noofrows
when 2 then a.min_id
when 3 then a.max_id
when 4 then a.null_check_id
when 5 then a.sumnum_id
when 6 then a.min_val
when 7 then a.max_val
when 8 then a.sumlen_val
from
(
select
CAST(COUNT(*) AS NUMBER(30)) AS noofrows
, MIN(id) AS min_id
, MAX(id) AS max_id
, SUM(CASE WHEN id IS NULL THEN 1 ELSE 0 END) AS null_check_id
, SUM(CAST(id AS DECIMAL(30,8))) AS sumnum_id
, MIN(val) AS min_val
, MAX(val) AS max_val
, SUM(CAST(LENGTH(TRIM(val)) AS DECIMAL(30,8))) AS sumlen_val
FROM DP_S.temp_a
) as a
full outer join
(
select 'temp_a' AS testing_table, 1 as fld
union all select 'temp_a' AS testing_table, 2 as fld
union all select 'temp_a' AS testing_table, 3 as fld
union all select 'temp_a' AS testing_table, 4 as fld
union all select 'temp_a' AS testing_table, 5 as fld
union all select 'temp_a' AS testing_table, 6 as fld
union all select 'temp_a' AS testing_table, 7 as fld
union all select 'temp_a' AS testing_table, 8 as fld
) as b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.