PostgreSQL查询以按产品和细分分组的净新客户数

问题描述 投票:0回答:1

我有一张帐单数据表,看起来像这样:

+-----------------------+------------+------------+-------------+---------+
|      segment          |  Product   |  custname  | prod_yr_mth | revenue |
+-----------------------+------------+------------+-------------+---------+
| PUBLIC SECTOR         | product 1  | customer a |      201806 | 318.34  |
| LARGE ENTERPRISE      | product 2  | customer b |      201902 | 8.96    |
| SMALL MEDIUM BUSINESS | product 3  | customer c |      201907 | -10     |
| SMALL MEDIUM BUSINESS | product 4  | customer d |      201804 | 0       |
| SMALL MEDIUM BUSINESS | product 5  | customer e |      201809 | 9.33    |
| LARGE ENTERPRISE      | product 1  | customer f |      201901 | 155.75  |
| PUBLIC SECTOR         | product 6  | customer g |      201905 | 24.32   |
| SMALL MEDIUM BUSINESS | product 2  | customer h |      201812 | 0.25    |
| SMALL MEDIUM BUSINESS | product 2  | customer i |      201801 | 5.46    |
| LARGE ENTERPRISE      | product 7  | customer j |      201805 | 4.5     |
| LARGE ENTERPRISE      | product 1  | customer k |      201812 | 0       |
| SMALL MEDIUM BUSINESS | product 8  | customer l |      201809 | 2.99    |
| LARGE ENTERPRISE      | product 2  | customer m |      201812 | 0.71    |
| LARGE ENTERPRISE      | product 1  | customer n |      201902 | 0       |
| PUBLIC SECTOR         | product 2  | customer o |      201803 | 1.08    |
| SMALL MEDIUM BUSINESS | product 9  | customer p |      201802 | 10.27   |
| LARGE ENTERPRISE      | product 10 | customer a |      201905 | 52.99   |
| PUBLIC SECTOR         | product 1  | customer b |      201810 | 7       |
| SMALL MEDIUM BUSINESS | product 3  | customer c |      201906 | 40      |
+-----------------------+------------+------------+-------------+---------+

我想获得按业务部门分组的每种产品的净新客户数。

因此,本月prod_yr_mth列中的最大日期为201908。如果该客户购买该产品的最早价值是201908,我将认为该客户是“新用户”。

最终结果应如下所示:

+-----------------------+------------+-------------------+
|      seg_lvl5_2       |  Product   | Net_New_Customers |
+-----------------------+------------+-------------------+
| LARGE ENTERPRISE      | product 1  |                 0 |
| LARGE ENTERPRISE      | product 10 |                 5 |
| LARGE ENTERPRISE      | product 2  |                 6 |
| LARGE ENTERPRISE      | product 6  |                 1 |
| LARGE ENTERPRISE      | product 7  |                 2 |
| PUBLIC SECTOR         | product 1  |                 3 |
| PUBLIC SECTOR         | product 2  |                 1 |
| PUBLIC SECTOR         | product 5  |                 1 |
| PUBLIC SECTOR         | product 6  |                 1 |
| SMALL MEDIUM BUSINESS | product 1  |                 1 |
| SMALL MEDIUM BUSINESS | product 2  |                 0 |
| SMALL MEDIUM BUSINESS | product 3  |                 9 |
| SMALL MEDIUM BUSINESS | product 4  |                 8 |
| SMALL MEDIUM BUSINESS | product 5  |                 7 |
| SMALL MEDIUM BUSINESS | product 6  |                 3 |
| SMALL MEDIUM BUSINESS | product 8  |                 4 |
| SMALL MEDIUM BUSINESS | product 9  |                 5 |
| SMALL MEDIUM BUSINESS | product 10 |                 2 |
+-----------------------+------------+-------------------+

谢谢!

我有一张帐单数据表,看起来像这样:+ ----------------------- + ----------- -+ ------------ + ------------- + --------- + |段|产品展示客户名称| prod_yr_mth | ...

sql database postgresql analytics
1个回答
0
投票

如果我的理解正确,您希望将客户/产品的最早日期与表中的最新日期进行比较。如果是这样:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.