【问题标题】:Teradata: Return the count of different combinations of columnsTeradata:返回不同列组合的计数
【发布时间】:2017-10-25 10:58:58
【问题描述】:

我有一个如下所示的数据集:

SocialSecurityNumber | ProgramEnrollmentStatus | Contact_Preference | 
---------------------------------------------------------------------
920421                   Enrolled                Daytime
870725                   Not Enrolled            Night
630126                   Undecided               Night
630126                   Undecided               Night
920421                   Enrolled                Daytime
910510                   Undecided               Anytime
921115                   Enrolled                Night
921115                   Enrolled                Night
910510                   Undecided               Anytime
950202                   Enrolled                Daytime

使用这些数据,我想编写一个返回这些数据的代码:

Contact_Preference | ProgramEnrollmentStatus | Count_Unique_SSN
Night                Enrolled                       1
Night                Undecided                      1
Night                Not Enrolled                   1
Anytime              Enrolled                       0
Anytime              Undecided                      1
Anytime              Not Enrolled                   0
Daytime              Enrolled                       2
Daytime              Undecided                      0
Daytime              Not Enrolled                   0

【问题讨论】:

  • 是什么阻止了您编写该代码?您能否发布您当前的查询以及为什么它不起作用。

标签: sql count teradata


【解决方案1】:

这很棘手,因为您必须生成具有零值的行。为此使用cross join。剩下的就是left joingroup by

select cp.contact_preference, pes.ProgramEnrollmentStatus,
       count(distinct SocialSecurityNumber)
from (select distinct contact_preference from t) cp cross join
     (select distinct ProgramEnrollmentStatus from t) pes left join
     t
     on t.contact_preference = cp.contact_preference and
        t.ProgramEnrollmentStatus = pes.ProgramEnrollmentStatus
group by cp.contact_preference, pes.ProgramEnrollmentStatus

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 2017-03-11
    • 2021-11-15
    • 1970-01-01
    • 2016-02-11
    • 2013-04-15
    相关资源
    最近更新 更多