我有一个查询来选择客户数据,我希望保持客户数量的演变。在第1周,我有2个新客户,因此数量为2.在第2周,我收到3个新客户,因此客户数量为5个。
我有以下查询来执行此操作
SELECT LAST_UPDATED_WEEK, SUM( NUM_CUSTOMERS ) OVER ( ORDER BY LAST_UPDATED_WEEK ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of customers"
FROM (
SELECT DATEADD(dd,DATEDIFF(dd,0,REGISTRATION_DATE),0) AS LAST_UPDATED_WEEK,
COUNT(DISTINCT CUSTOMER_ID) AS NUM_CUSTOMERS
FROM CUSTOMERS_TABLE
GROUP BY DATEADD(dd,DATEDIFF(dd,0,REGISTRATION_DATE),0)) AS T
但是当我运行此查询时,它不按周分组我的数据。我读到了DATEPART函数,但它返回一个整数,但我需要有实际的日期。 有人能帮助我吗?
答案 0 :(得分:1)
只需将dd
和DATEADD
函数中的DATEDIFF
替换为WEEK