30天登陆问题
某用户,在30天内登陆。比如:
a5612 2011-01-01
a5612 2011-01-02
a5612 2011-01-03
a5612 2011-01-07
a5612 2011-01-08
a5612 2011-01-09
a5612 2011-01-10
a5612 2011-01-11
a5612 2011-01-12
a5612 2011-01-17
a5612 2011-01-18
a5612 2011-01-19
a5612 2011-01-24
a5612 2011-01-25
a5612 2011-01-28
a5612 2011-01-30
怎么表述称一个整数sum:比如登陆第1,2,3天依次为2的0,1,2次幂;第4,5,6天没登陆,都为0;第7天登陆,记为2的6次幂。。依此类推。最终,把各个2的次幂求和sum。
有效率高些的思路吗?
------解决方案--------------------
with cte
as(select *,pw=power(2,day(date)-1),from tb)
select name,sum(pw)
from cte
group by name
这样?