如何获取 Teradata sql 中一行的最大列值?

How to get maximum column values across a row in Teradata sql?

我有一个名为 cnst_chrctrstc_abc 的 table,有 10 列 (equ_gender1 - bb_population_flag),每行包含数值 (count)。 我想从这 10 个数字列的每一行中获取最多 5 个值。

我的查询类似于以下内容..

SEL 
FROM 
(
SEL 
SUM(CASE WHEN COALESCE(act.equ_gender1,'') = COALESCE(inact.equ_gender1,'') THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
SUM(CASE WHEN COALESCE(act.exp_ex_bmyr1,'') = COALESCE(inact.exp_ex_bmyr1,'') THEN 0 ELSE 1 END ) AS exp_ex_bmyr1_chg_cnt,
SUM(CASE WHEN COALESCE(act.equ_age1,'') = COALESCE(inact.equ_age1,'') THEN 0 ELSE 1 END ) AS equ_age1_chg_cnt,
SUM(CASE WHEN COALESCE(act.maritalstatus1,'') = COALESCE(inact.maritalstatus1,'') THEN 0 ELSE 1 END ) AS maritalstatus1_chg_cnt,
SUM(CASE WHEN COALESCE(act.person_type1,'') = COALESCE(inact.person_type1,'') THEN 0 ELSE 1 END ) AS person_type1_chg_cnt,
SUM(CASE WHEN COALESCE(act.homeowner,'') = COALESCE(inact.homeowner,'') THEN 0 ELSE 1 END ) AS homeowner_chg_cnt,
SUM(CASE WHEN COALESCE(act.dwelling_size,'') = COALESCE(inact.dwelling_size,'') THEN 0 ELSE 1 END ) AS dwelling_size_chg_cnt,
SUM(CASE WHEN COALESCE(act.lengthofresidence,'') = COALESCE(inact.lengthofresidence,'') THEN 0 ELSE 1 END ) AS lengthofresidence_chg_cnt,
SUM(CASE WHEN COALESCE(act.childrenage0_18,'') = COALESCE(inact.childrenage0_18,'') THEN 0 ELSE 1 END ) AS childrenage0_18_chg_cnt,
SUM(CASE WHEN COALESCE(act.bb_population_flag,'') = COALESCE(inact.bb_population_flag,'') THEN 0 ELSE 1 END ) AS bb_population_flag


FROM
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_abc WHERE load_id=1024 AND cnst_chrctrstc_end_dt='9999-12-31' (DATE))act
LEFT JOIN
(SEL * FROM arc_mdm_Tbls.cnst_chrctrstc_abc WHERE load_id=1024 AND cnst_chrctrstc_end_dt<'9999-12-31' (DATE) 
QUALIFY ROW_NUMBER() OVER (PARTITION BY cnst_mstr_id ORDER BY cnst_chrctrstc_strt_ts DESC)=1
)inact
ON act.cnst_mstr_id = inact.cnst_mstr_id
)X

我知道 SEL GREATEST 会产生每行的最大值。但我想要 5 个最高值并为它们分配排名。

对于某些行,前五列可能包含前 5 个值,而对于某些后五列,即 bb_population_flag 的房主可能包含前 5 个值。

因此,如果 cnst_chrctrstc_abc 中的列和值类似于以下内容

cdi_batch_id | a | b | c | d | e | f | g | h | i |j
1024         |116|105|102|100|117|119|108|104|101|121

所以 select 查询应该 return 列 j,f,e,a,g 具有前 5 个值。 然后我会相应地给他们分配一个等级。

是否应该使用 unpivot 或其他方法来完成? 提前致谢。

是的,您需要对结果进行逆透视。

在 TD14.10 之前,您将需要这些列名称的列表,可以是 table

create table ColumnList (col varchar(128));
Insert into ColumnList('equ_gender1'       );
Insert into ColumnList('exp_ex_bmyr1'      );
Insert into ColumnList('equ_age1'          );
Insert into ColumnList('maritalstatus1'    );
Insert into ColumnList('person_type1'      );
Insert into ColumnList('homeowner'         );
Insert into ColumnList('dwelling_size'     );
Insert into ColumnList('lengthofresidence' );
Insert into ColumnList('childrenage0_18'   );
Insert into ColumnList('bb_population_flag');

或者使用笨重的

with ColumnList as 
 (
   select * from (select 'equ_gender1'        as Col) as dt union all
   select * from (select 'exp_ex_bmyr1'       as Col) as dt union all
   select * from (select 'equ_age1'           as Col) as dt union all
   select * from (select 'maritalstatus1'     as Col) as dt union all
   select * from (select 'person_type1'       as Col) as dt union all
   select * from (select 'homeowner'          as Col) as dt union all
   select * from (select 'dwelling_size'      as Col) as dt union all
   select * from (select 'lengthofresidence'  as Col) as dt union all
   select * from (select 'childrenage0_18'    as Col) as dt union all
   select * from (select 'bb_population_flag' as Col) as dt
 )

然后你通过 CROSS JOIN 来反转:

select
   col,
   case col 
      when 'equ_gender1'        then equ_gender1       
      when 'exp_ex_bmyr1'       then exp_ex_bmyr1      
      when 'equ_age1'           then equ_age1          
      when 'maritalstatus1'     then maritalstatus1    
      when 'person_type1'       then person_type1      
      when 'homeowner'          then homeowner         
      when 'dwelling_size'      then dwelling_size     
      when 'lengthofresidence'  then lengthofresidence 
      when 'childrenage0_18'    then childrenage0_18   
      when 'bb_population_flag' then bb_population_flag
   end as Counts,
   rank() over (order by Counts desc) as rnk
FROM
 (
   your current select
 ) as dt
cross join ColumnList
qualify rnk <= 5

在 TD14.10 中,您可以使用 TD_UNPIVOT 函数:

SELECT Col, rank() over (order by Counts desc) as rnk 
from TD_UNPIVOT(
        ON (
             your current select
           )
        USING
           VALUE_COLUMNS('Counts')
           UNPIVOT_COLUMN('Col')
           COLUMN_LIST('equ_gender1'
                      ,'exp_ex_bmyr1'
                      ,'equ_age1'          
                      ,'maritalstatus1'
                      ,'person_type1'
                      ,'homeowner'
                      ,'dwelling_size'
                      ,'lengthofresidence'
                      ,'childrenage0_18'
                      ,'bb_population_flag')

        ) dt
qualify rnk <= 5;

编辑:

此外,您可以将 LEFT JOIN 替换为单个 OLAP 函数。根据每个 cnst_mstr_id 的行数,这可能更有效,因为无论如何您都需要 ROW_NUMBER

SEL 
SUM(CASE WHEN COALESCE(equ_gender1,'') = COALESCE(last_equ_gender1,'') THEN 0 ELSE 1 END ) AS equ_gender1_chg_cnt,
...
FROM
 ( SELECT 
      min(equ_gender1) OVER (PARTITION BY cnst_mstr_id ORDER BY cnst_chrctrstc_strt_ts DESC rows between 1 following and 1 following) as equ_gender1,
      ...
   FROM arc_mdm_Tbls.cnst_chrctrstc_abc 
   WHERE load_id=1024
   qualify cnst_chrctrstc_end_dt= date '9999-12-31'
 )act