我有一个数据集看起来是这样的:
\x{e76f} id _
#^ blab #^blab#.
#^ blub #^blub#.
\x{e76f}\x{e76f}=2019年.
#^ ana #^ana#.
\x{e76f}\x{e76f}#^ mars #^mars活动#^mars 2019-3-4
我要做到以下几点:
\x{e76f} id _ status_change_date _
#^ blab #^blab#.
#^ blub #^blub活动#^blub 209-3-8#.#^blub#^blub#.
\x{e76f}\x{e76f}=2019年.
#^ ana #^ana#.
={= 2019-3-4
对于每个id,我喜欢计算状态上次更改的时间。
我已经尝试过使用groupBy,但问题是我只想按照彼此相邻的活动行和非活动行进行分组。如果在ACTIVE之间有一个非活动的,我喜欢为新的ACTIVE创建一个新的组。
有人想办法解决这个问题吗?
发布于 2019-09-30 15:37:11
以下是使用窗口函数的纯SQL解决方案。这是通过生成包含具有相同id和状态的连续记录的分区来工作的。
SELECT
id,
status,
open_date,
name,
MIN(open_date) OVER(PARTITION BY id, rn1 - rn2 ORDER BY open_date) status_change_date
FROM (
SELECT
t.*,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY open_date) rn1,
ROW_NUMBER() OVER(PARTITION BY id, status ORDER BY open_date) rn2
FROM mytable t
) x
ORDER BY id, open_date| id | status | open_date | name | status_change_date |
| --- | -------- | ---------- | ---- | ------------------ |
| 8 | active | 2019-03-02 | blab | 2019-03-02 |
| 8 | active | 2019-03-08 | blub | 2019-03-02 |
| 8 | inactive | 2019-03-09 | hans | 2019-03-09 |
| 8 | active | 2019-03-10 | ana | 2019-03-10 |
| 9 | active | 2019-03-04 | mars | 2019-03-04 |发布于 2019-09-30 15:02:16
你可以试试这个:
var movies = [
{title: 'The Godfather', rating: 9.2, release: '24 March 1972'},
{title: 'The Godfather: Part II', rating: 9.0, release: '20 December 1972'},
{title: 'The Shawshank Redemption', rating: 9.3, release: '14 October 1994'},
];
movies.sort(function(a, b) {
var dateA = new Date(a.release), dateB = new Date(b.release);
return dateA - dateB;
});此排序方法之所以有效,是因为js允许您比较日期对象上的算术,这些对象首先会自动转换为数字表示形式。
在SQL中使用MIN函数:
订单Id OrderDate OrderNumber CustomerId TotalAmount
从年份(OrderDate)=2013年的顺序中选择MIN(OrderDate)
发布于 2019-09-30 15:11:33
这就是如何取一个小组中最小的日期的答案?
let minDate = new Date('0001-01-01T00:00:00Z');
dataset.forEach(x => if( x.date > this.minDate) { this.minDate = x.date } )
console.log(this.minDate);https://stackoverflow.com/questions/58170260
复制相似问题