简体   繁体   English

Teradata 每天获取 MTD 唯一用户(例如,在 3 月 12 日,我需要有第 1 到第 12 个唯一用户。过去一年的每个日期也是如此

[英]Teradata Get MTD Unique Users for each day (ex. on 12th Mar I need to have 1st to 12th unique users. similarly for each date for last one year

I need to get MTD Unique users count each day for last 1 year.我需要让 MTD 唯一用户数在过去 1 年内每天计数。 Ex.前任。 if my data is like如果我的数据就像

dt  customername
1   a
1   b
2   a
2   c
3   b
3   a
4   c
4   d
4   e

expected output- on 1st only 2 unique user.预期输出 - 第一个只有 2 个唯一用户。 on 2nd (1&2nd) 3 users.在第 2 个(第 1 个和第 2 个)3 个用户上。 on 3rd (1st to 3rd) 3 users.第 3 位(第 1 至第 3 位)3 个用户。 on 4th (1st to 4th) 5 users. 4 日(第 1 至 4 日)5 个用户。 I need this for each month for each date for last 1 year我在过去 1 年的每个日期的每个月都需要这个

dt uniquecustcount
1  2
2  3
3  3
4  5

my data is in below format, bu core logic is above one for MTD.我的数据采用以下格式,但 MTD 的核心逻辑高于 1。 Appreciate any help please请感谢任何帮助

CREATE MULTISET TABLE GK_DAILY_USERS ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      EVENT_DATE DATE FORMAT 'yyyy-mm-dd',
      Subs_Id INTEGER,
      PAYMENT_METHOD_CD VARCHAR(8) CHARACTER SET UNICODE NOT CASESPECIFIC,
      Data_User_Flag VARCHAR(1) CHARACTER SET UNICODE NOT CASESPECIFIC,
      BUS_UNIT VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC,
      Event_Count INTEGER)
PRIMARY INDEX ( EVENT_DATE ,Subs_Id );

Insert into GK_DAILY_USERS values (?,?,?,?,?,?);

I am trying below sql.我正在尝试下面的sql。 I know this sql doesnt make sense and hence looking for solution.我知道这个 sql 没有意义,因此正在寻找解决方案。

SELECT EVENT_DATE, MAX(R2) FROM (
SELECT EVENT_DATE 
,COUNT(A.SUBS_ID)OVER(ORDER BY ROWNUM,A.SUBS_ID) AS R2
FROM (
SELECT A.EVENT_DATE,A.SUBS_ID
,Row_Number() Over (Order by EVENT_DATE, SUBS_ID) rownum
,COUNT(A.SUBS_ID)OVER(PARTITION BY A.SUBS_ID ORDER BY A.EVENT_DATE) AS RUN_TOTAL3
FROM  GK_DAILY_USERS A 
WHERE Data_User_Flag='Y' 
AND subs_id in (566875703,289813839, 151153086,279262050,18378517)
ORDER BY EVENT_DATE, SUBS_ID
) A 
WHERE RUN_TOTAL3 = 1
) A GROUP BY EVENT_DATE ORDER BY 1
;
SELECT EVENT_DATE 
   -- new customers per date
  ,Sum(SUBS_ID) AS daily_new_cust
  -- running total of new customers per date
  ,Sum(Sum(SUBS_ID))
   Over(ORDER BY EVENT_DATE
        ROWS Unbounded Preceding) AS unique_cust
FROM
 (
   SELECT *
   FROM  GK_DAILY_USERS
   WHERE Data_User_Flag='Y' 
   AND subs_id IN (566875703,289813839, 151153086,279262050,18378517)
   QUALIFY
      -- return a single row with the earliest date for each subs_id
      Row_Number()
      Over (PARTITION BY A.SUBS_ID
            ORDER BY EVENT_DATE) = 1
 ) AS dt
GROUP BY EVENT_DATE 
ORDER BY 1
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite获得第12条记录-最有效? - SQLite get the 12th record - most efficient? 试图找到去年的这一天。 例如:获取10月的第一个星期五,而不是10月的第4个 - Trying to find this day last year. Ex: Get the 1st Friday of October, not the October the 4th 如何在表格中找到最近的12个日期? - How do I find the 12th most recent date in a table? 如何将 SQL 日期格式更改为:2021 年 6 月 12 日。(例如) - How can i change SQL date format to: 12th Jun 2021. (as example) 如何在mysql中获取每个月的第1天和第15天(1到12之间)? - How to obtain the 1st and the 15th day of the month for each month (between 1 and 12) in mysql? 如何在SQL select语句中采用第12个值? - How do i take the 12th value in a SQL select statement? 第13行动态后和第12行静态值中包含公式 - After 13th row Dynamic and in 12th row static values with a formula 每年每个月透视独特用户 - Pivoting unique users for each month, each year 在第13行动态之后和第12行静态值中的SQL查询帮助,并带有公式帮助。 - SQL Query Help after 13th row Dynamic and in 12th row static values with a formula help pls.. posting DDL and DML 我怎样才能得到从当前日期到最后一个月的 1 日的额外天数的最后 12 个月 - How can I get the last 12 months from the current date PLUS extra days till 1st of the last month retrieved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM