count(case when.distinct 解决方法

count(case when..distinct 解决办法SQL codeselect u,count(case when a is not null then distinct a el

count(case when..distinct 解决办法

SQL code
select u,count(case when a is not null then distinct a else b end)from t1group by 1

形如这样的代码出错,有什么解决办法吗?

现有的代码为
SQL code
select u.count(distinct a)from t1where a is not nullgroup by 1union allselect u.count(b)from t1where a is nullgroup by 1

觉得这样的代码实在太长,应该可以合并

[解决办法]
select U,count(*) from (
select U,case when a is null then a else b end as Na from t1 group by 1,2) a1
group by 1