SQLite用两个条件对行编号

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

我想使用sqlite通过两个条件对csv文件中的行进行编号:重复的ID号和创建的日期。 (我正在分析由DemandTools生成的重复帐户数据。)为了帮助解释,这是我要执行的操作的简化版本:

想要打开这个:

Dupe Id #  |   Account Name   |   Created Date  |
1882       | A1, Inc.         |  03/15/2015     |
1567       | Joe's Plumbing   |  08/01/2019     |
1567       | Joes plubming    |  02/07/2020     |
1882       | A1 Corporation   |  06/20/2019     |
1882       | A1 Incorporated  |  05/16/2016     |

进入此:

Dupe Id #  |   Account Name   |   Created Date  |   Dupe #   |
1567       | Joe's Plumbing   |  08/01/2019     |   0        |
1567       | Joes plubming    |  02/07/2020     |   1        |
1882       | A1, Inc.         |  03/15/2015     |   0        |
1882       | A1 Incorporated  |  05/16/2016     |   1        |
1882       | A1 Corporation   |  06/20/2019     |   2        |

我希望原始帐户的值为0,第一个被骗者为1,第二个被骗者为2,等等。

我有一种方法可以在Excel中使用多列排序和类似countCount(= $ IF(A $ 2:A2,A2)-1)这样的countif函数,但是在excel中工作10万行和10列的方法却并非如此处理700k行和24列。

我的SQLite知识目前处于初级水平。我了解基本知识,但不确定如何从这样的问题开始。我知道如何在SQLite中按单个列进行排序,但是我不知道如何处理问题的countif部分(也许SQLite有更好的方法?)。

非常感谢您的帮助。 。 。 。

谢谢

sqlite row-number
1个回答
1
投票

首先,您必须将日期格式更改为YYYY-MM-DD,因为这是SQLite中日期的唯一有效格式,并且具有可比性。使用ROW_NUMBER()窗口功能:

select *,
  row_number() over (partition by Id order by CreatedDate) - 1 Dupe
from tablename 
order by id, Dupe

请参见demo。结果:

| Id   | AccountName     | CreatedDate | Dupe |
| ---- | --------------- | ----------- | ---- |
| 1567 | Joe's Plumbing  | 2019-08-01  | 0    |
| 1567 | Joes plubming   | 2020-02-07  | 1    |
| 1882 | A1, Inc.        | 2015-03-15  | 0    |
| 1882 | A1 Incorporated | 2016-05-16  | 1    |
| 1882 | A1 Corporation  | 2019-06-20  | 2    |
© www.soinside.com 2019 - 2024. All rights reserved.