我有一个如下所示的数据集:
5338 300 [False False False True True True]
[[349154 5338 100]
[349155 5338 100]
[349156 5338 100]]
4970 400 [False False False False False False False False True True True]
[[349159 4970 50]
[349160 4970 50]
[349179 4970 50]
[349181 4970 50]
[349192 4970 50]
[349113 4970 50]
[349124 4970 50]
[349135 4970 50]]
5339 150 [False False False True True]
[[449124 5339 50]
[549135 5339 50]
[649146 5339 50]]
使用这些数据,我想写一个返回这些数据的代码:
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
答案 0 :(得分:1)
这很棘手,因为您必须生成零值的行。使用cross join
即可。其余的是left join
和group 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