我的数据如下:
| Location | ticketPeriod | UserID | ticketUses | Demographic | Date |
|----------|--------------|--------|------------|-------------|------------|
| London | Day | 1 | 1 | Adult | 21/03/2019 |
| London | Day | 1 | 2 | Adult | 06/02/2019 |
| Glasgow | Day | 1 | 1 | Adult | 04/04/2019 |
| Glasgow | Day | 12 | 3 | Child | 03/04/2019 |
| Glasgow | Day | 3 | 1 | Child | 04/01/2019 |
| Glasgow | Week | 2 | 3 | Child | 27/01/2019 |
| Glasgow | Month | 3 | 3 | Concession | 04/04/2019 |
| London | Annual | 5 | 2 | Concession | 15/02/2019 |
| London | Quarterly | 5 | 1 | Adult | 13/02/2019 |
| Cardiff | Annual | 5 | 1 | Adult | 24/01/2019 |
| Cardiff | Quarterly | 1 | 1 | Child | 11/03/2019 |
| London | Week | 3 | 2 | Child | 04/04/2019 |
| Cardiff | Week | 12 | 4 | Adult | 04/04/2019 |
| London | Annual | 3 | 9 | Adult | 04/04/2019 |
我想做的是对数据进行分组,以获取最少的日期并计算票证的使用情况,但要保留日期字段,如下所示;
| Location | ticketPeriod | UserID | ticketUses | Demographic | Date | MinDate | countTicketUses |
|----------|--------------|--------|------------|-------------|------------|------------|-----------------|
| Cardiff | Annual | 5 | 1 | Adult | 24/01/2019 | 24/01/2019 | 1 |
| Cardiff | Quarterly | 1 | 1 | Child | 11/03/2019 | 11/03/2019 | 1 |
| Cardiff | Week | 12 | 4 | Adult | 04/04/2019 | 04/04/2019 | 4 |
| Glasgow | Day | 3 | 1 | Child | 04/04/2019 | 04/01/2019 | 5 |
| Glasgow | Day | 3 | 3 | Child | 03/04/2019 | 04/01/2019 | 5 |
| Glasgow | Day | 3 | 1 | Child | 04/01/2019 | 04/01/2019 | 5 |
| Glasgow | Week | 2 | 3 | Child | 27/01/2019 | 27/01/2019 | 6 |
| Glasgow | Week | 3 | 3 | Child | 04/04/2019 | 27/01/2019 | 6 |
| London | Day | 1 | 1 | Adult | 21/03/2019 | 06/02/2019 | 17 |
| London | Day | 1 | 2 | Adult | 06/02/2019 | 06/02/2019 | 17 |
| London | Day | 5 | 2 | Adult | 15/02/2019 | 06/02/2019 | 17 |
| London | day | 5 | 1 | Adult | 13/02/2019 | 06/02/2019 | 17 |
| London | day | 3 | 2 | Adult | 04/04/2019 | 06/02/2019 | 17 |
| London | day | 3 | 9 | Adult | 04/04/2019 | 06/02/2019 | 17 |
基本上等于group by,在R中进行突变。
答案 0 :(得分:3)
您可以使用窗口功能来做到这一点:
select Location,
ticketPeriod,
UserID,
ticketUses,
Demographic,
Date,
min(date) over (partition by ticketPeriod, Demographic) as MinDate,
sum(ticketUses) over (partition by ticketPeriod, Demographic) as countTicketUses
from the_table;