Teradata SQL 条件聚合

发布于 2025-01-17 12:40:20 字数 860 浏览 1 评论 0原文

我有下表,其中包含一些客户一个月的数据,我需要根据每列值的条件聚合该表,

条件

  • 如果客户的稳定性有> = 2 “非常不稳定”状态则在 Value_1 中返回 1 else 0
  • 如果客户在整个月内至少有一条值为 1 的记录,则返回 1 else
|Cust_ID|Date    |Stability     |Value_1|
|-------+--------+--------------+-------|
|123    |3/1/2022|Unstable      |1      |
|123    |3/2/2022|Very Unstable |0      |
|123    |3/3/2022|Stable        |1      |
|123    |3/4/2022|Ver Stable    |NULL   |
|123    |3/5/2022|Unstable      |NULL   |
|123    |3/6/2022|Very Unstable |0      |
|123    |3/7/2022|Unstable      |0      |
|123    |3/8/2022|Very Unstable |0      |
|…      |…       |…             |…      |
|123    |3/31/2022|Very Unstable|0      |

0结果表如下:

|Cust_ID|Stability|Value_1|
|-------+---------+-------|
|123    |1        |1      |

I have the below table with data for one month for some customers, and I need to aggregate this table based on conditions for the values of every column,

The conditions

  • If the customer's stability have >= 2 of "Very Unstable" status then return 1 else 0
  • In Value_1 if the customer has at least one record with the value: 1 along the entire month then return 1 else 0
|Cust_ID|Date    |Stability     |Value_1|
|-------+--------+--------------+-------|
|123    |3/1/2022|Unstable      |1      |
|123    |3/2/2022|Very Unstable |0      |
|123    |3/3/2022|Stable        |1      |
|123    |3/4/2022|Ver Stable    |NULL   |
|123    |3/5/2022|Unstable      |NULL   |
|123    |3/6/2022|Very Unstable |0      |
|123    |3/7/2022|Unstable      |0      |
|123    |3/8/2022|Very Unstable |0      |
|…      |…       |…             |…      |
|123    |3/31/2022|Very Unstable|0      |

to be the result table like that:

|Cust_ID|Stability|Value_1|
|-------+---------+-------|
|123    |1        |1      |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2025-01-24 12:40:20

这似乎与您的描述相符:

SELECT Cust_ID
   -- If the customer's stability have >= 2 of "Very Unstable" status 
   -- then return 1 else 0
  ,CASE
     WHEN COUNT(CASE WHEN Stability = 'Very Unstable' THEN 1 END) >= 2 
     THEN 1 
     ELSE 0
   END AS STABILITY_COUNT
  -- In Value_1 if the customer has at least one record with
  -- the value: 1 along the entire month then return 1 else 0
  ,MAX(Value_1) Value_1_m
FROM ST_TABLE
GROUP BY 1

This seems to match your description:

SELECT Cust_ID
   -- If the customer's stability have >= 2 of "Very Unstable" status 
   -- then return 1 else 0
  ,CASE
     WHEN COUNT(CASE WHEN Stability = 'Very Unstable' THEN 1 END) >= 2 
     THEN 1 
     ELSE 0
   END AS STABILITY_COUNT
  -- In Value_1 if the customer has at least one record with
  -- the value: 1 along the entire month then return 1 else 0
  ,MAX(Value_1) Value_1_m
FROM ST_TABLE
GROUP BY 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文