select count(case when pos=0 and count_distinct_ID>1 then 1 end) as different_cityid
,count(case when pos=1 and count_distinct_ID>1 then 1 end) as different_countryid
,count(case when pos=2 and count_distinct_ID>1 then 1 end) as different_tagid
from (select pe.pos
,count (distinct pe.ID) as count_distinct_ID
from mytable t
lateral view posexplode (array(CityID,CountryID,TagID)) pe as pos,ID
group by t.UserID
,pe.pos
) t
;
+------------------+---------------------+-----------------+
| different_cityid | different_countryid | different_tagid |
+------------------+---------------------+-----------------+
| 1 | 3 | 2 |
+------------------+---------------------+-----------------+
這裏是另一個變化是避免count(distinct ...)
select count (case when pos=0 and not is_distinct_ID then 1 end) as different_cityid
,count (case when pos=1 and not is_distinct_ID then 1 end) as different_countryid
,count (case when pos=2 and not is_distinct_ID then 1 end) as different_tagid
from (select pe.pos
,min(pe.ID)<=>max(pe.ID) as is_distinct_ID
from mytable t
lateral view posexplode (array(CityID,CountryID,TagID)) pe as pos,ID
group by t.UserID
,pe.pos
) t
;
...和另一個變化
select count (case when not is_distinct_CityID then 1 end) as different_cityid
,count (case when not is_distinct_CountryID then 1 end) as different_countryid
,count (case when not is_distinct_TagID then 1 end) as different_tagid
from (select min (CityID) <=> max (CityID) as is_distinct_CityID
,min (CountryID) <=> max (CountryID) as is_distinct_CountryID
,min (TagID) <=> max (TagID) as is_distinct_TagID
from mytable
group by UserID
) t
;
耶穌基督!謝謝@Dudu!這看起來很奇怪,我會嘗試。從未見過「側面視圖」。 – Peter
您的歡迎:-)檢查更新的答案 - 爲教育目的 –
是什麼「<=>」運營商怎麼辦?根據配置單元文檔中的解釋,我不完全清楚它的功能。 – invoketheshell