针对正在运行的用户总数的SQL查询

时间:2016-04-15 04:54:10

标签: sql amazon-redshift

此查询:

SELECT received_at as sign_up_date,
   COUNT(DISTINCT email) AS "count"
FROM seller_v2.users
  GROUP BY 1
  ORDER BY 1;

创建以下输出:

sign_up_date                count
2016-02-18T17:38:51.000Z      1
2016-02-18T21:47:48.000Z      1 

每个日期等于用户的注册日期。如何创建一个包含运行总用户数的新列,以便我可以创建时间序列图表?

1 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server(我相信Oracle和其他一些RDBMS的语法相同),您可以使用ROW_NUMBER()来显示行号,在您的情况下与行号相同:

SELECT received_at as sign_up_date,
   ROW_NUMBER() OVER (ORDER BY received_at ASC) AS running_total
FROM seller_v2.users;