我对解决这个问题有疑问。我的表具有型号名称,设备类型,计数设备类型和NEW_device_type。我想将每个模型的当前device_type列更新为只有一个device_type(请参见表中的示例)
**MODEL DEVICE_TYPE COUNT_DEVICE_TYPE NEW_DEVICE_TYPE**
SAMUNG GALAXY S5 SMARTPHONE 100 SMARTPHONE
SAMUNG GALAXY S5 PORTABLE PDA 30 SMARTPHONE
SAMUNG GALAXY S5 HANDHELD 10 SMARTPHONE
我已经尝试过此代码,但是无法捕获新值:
select
model
,device_type
,case when count(model)<40 then
(select distinct device_type from tmp_BI_dim_device_ref a group by model, device_type having count(model)>10 ) else device_type end as new_device_type
from tmp_BI_device_table
group by 1,2
我收到此错误:
ERROR: 12 : More than one tuple returned by a subselect used as an expression
如果要使用最高数量的设备类型,则可以使用first_value()
:
select model, device_type, count(*),
first_value(device_type) over (partition by model order by count(*) desc) as imputed_device_type
from tmp_BI_device_table
group by 1, 2