1

我的一组结果如下所示:

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;
4

1 回答 1

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

这可能会有所帮助。

于 2018-01-12T05:17:56.887 回答