简体   繁体   English

如何查看前3个月和未来3个月的数据

[英]How to check data for previous 3 month and next 3 months

I am working with telecom data. 我正在处理电信数据。 So basically I have mobile numbers and I need to have a flag (Yes/ No) that has following conditions. 所以基本上我有手机号码,我需要有一个有以下条件的标志(是/否)。

Flag 'Yes' if, 标记'是'如果,

  1. Invoice month must be at-least for 3 months 发票月份必须至少为3个月
  2. Total_Minutes_Used=0 Total_Minutes_Used = 0
  3. Total_Data_Usage_MB=0 Total_Data_Usage_MB = 0
  4. Case if Total_Minutes_Used=0 OR Total_Data_Usage_MB=0 for next 3 months (to check for +- 3 months) else 'No' 如果Total_Minutes_Used = 0或者Total_Data_Usage_MB = 0表示接下来的3个月(检查+ - 3个月),否则为“否”

Sample data 样本数据

Mobile Number  Invoice_Month Total_Minutes_Used Total_Data_Usage_MB
------------- -------------- ------------------ -------------------
9112222210          01-2019     0                 0
9112222210          02-2019     0                 0
9112222210          03-2019     0                 45
9112222210          04-2019     0                 0
9112222211          01-2019     0                 0
9112222211          02-2019     0                 0
9112222211          03-2019     0                 0
9112222211          04-2019     0                 0
9112222212          01-2019     0                 0
9112222212          02-2019     0                 0
9112222212          03-2019     0                 0
9112222212          04-2019     0                 50

Expected Output 预期产出

Mobile Number  Invoice_Month Total_Minutes_Used Total_Data_Usage_MB       Flag
------------- -------------- ------------------ -------------------       ----
9112222210          01-2019     0                 0      N
9112222210          02-2019     0                 0      N
9112222210          03-2019     0                 45     N
9112222210          04-2019     0                 0      N
9112222211          01-2019     0                 0      N
9112222211          02-2019     0                 0      N
9112222211          03-2019     0                 0      Y
9112222211          04-2019     0                 0      Y
9112222212          01-2019     0                 0      N
9112222212          02-2019     0                 0      N
9112222212          03-2019     0                 0      N
9112222212          04-2019     0                 50     N

I tried it with LEAD and LAG but it is not working as expected. 我用LEAD和LAG尝试了它,但它没有按预期工作。

You could try this 你可以试试这个

select *, 
case when Invoice_Month <= DATEADD(mm, -3, GETDATE()) AND
          Total_Minutes_Used=0 AND
          Total_Data_Usage_MB=0 AND
          Total_Minutes_Used=0 OR Total_Data_Usage_MB=0 THEN "YES"
          ELSE "NO"
          END AS FLAG 
FROM TABLE_NAME;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM