我的结果看起来像这样:
date | count
----------+------
12/1/17 | 13
12/2/17 | 15
12/3/17 | 2
我想添加一个列来为日期值赋值,具体取决于它在结果中的位置,如下所示:
date | count | day
----------+--------+------
12/1/17 | 13 | 1
12/2/17 | 15 | 2
12/3/17 | 2 | 3
如何做到这一点?
如果有帮助,这是我的问题:
select
a.date
count(a.user_id)
from
tableOne a,
tableTwo b
where
a.user_id = b.user_id
group by
a.date
order by
a.date asc;
答案 0 :(得分:1)
试试这个:
select
a.date,
count(a.user_id) Counts,
row_number() over (order by CASE WHEN counts=0 THEN '01/01/01' ELSE a.date END) as days
from
tableOne a,
tableTwo b
where
a.user_id = b.user_id
group by
a.date
order by
a.date asc;
<强>结果:强>
date counts days
2017-12-04 0 1
2017-12-01 13 2
2017-12-02 15 3
2017-12-03 2 4
这可能有所帮助。