Teradata SQL排名前n个子组

时间:2018-06-27 23:34:20

标签: sql teradata

我正在使用TD v15。我有一个表格,如下所示-每行都是一条记录,我想按以下方式执行Count:

在问题栏中:我有4个“ A”,5个“ B”,3个“ C”和2个“ D”。从它们中选择前2个,即A和B。将其余问题分组为“ OtherQ”-将其放在“结果问题”列中。

在“更改”列中,我有2个“ AA”,3个“ AB”,2个“ AC”,2个“ AD”,4个“ AE”和2个“ AG”,选择前2个,即AE和AB,组其余的更改为“其他”-将它们放在“结果更改”列中。

然后,计数...

Question    Result Change  
    A         Pass   AG          
    A         Pass   AE           
    A         Pass   AA           
    A         Pass   AB       
    B         Pass   AC      
    B         Pass   AG        
    B         Pass   AB         
    B         Pass   AE       
    B         Pass   AD
    B         Pass   AA
    C         Pass   AB
    C         Pass   AC
    C         Pass   AD
    D         Pass   AE
    D         Pass   AE
    A         Fail   Null
    A         Fail   Null
    C         Fail   Null
    E         Fail   Null
    B         Fail   Null

这是理想的结果,它依靠前2个问题(A&B)和OtherQ以及前2个变更(AE&AB)和其他变更,并且它还计算A&B和OtherQ的通过与失败。

Count的总和为20,这应与上表中的20个单独的行匹配。

 Question    Result Change    Count
    A         Pass   AE         1
    A         Pass   AB         1
    A         Pass   Other      2
    B         Pass   AE         1
    B         Pass   AB         1
    B         Pass   Other      4   
  OtherQ      Pass   AE         2
  OtherQ      Pass   AB         1
  OtherQ      Pass   Other      2
    A         Fail   Null       2
    B         Fail   Null       1
  OtherQ      Fail   Null       2

可以请您帮忙吗?它的数据表非常大,需要代码高效。许多人感谢您的时间和事先的帮助。

1 个答案:

答案 0 :(得分:1)

我建议使用聚合和子查询:

select coalesce(tq.question, 'Other') as question
       (case when t.change is null then null
             else coalesce(tch.change, 'Other') 
        end) as change,
       count(*)
from t left join
     (select question, count(*) as cnt,
             row_number() over (order by count(*) desc) as seqnum
      from t
      group by question
     ) tq
     on tq.question = t.question and tq.seqnum <= 2 left join
     (select change, count(*) as cnt,
             row_number() over (order by count(*) desc) as seqnum
      from t
      group by change
     ) tch
     on tch.change = t.change and tch.seqnum <= 2
group by coalesce(tq.question, 'Other'),
         coalesce(tch.change, 'Other');