我有以下数据:
ID| TimeStamp| Data
-------------------
1| t+1 |A
2| t+2 |B
3| t+3 |A
4| t+4 |A
5| t+5 |D
我想基于数据获取所有唯一的行。如果彼此之间有一些值,我想获取最新的值(时间戳)。
我想要的结果:
ID| TimeStamp| Data
-------------------
1| t+1 |A
2| t+2 |B
4| t+4 |A
5| t+5 |D
ID为3的行应进行过滤(t + 4> t + 3),但ID为1的行应保留(因为之间存在数据B)。
在sql / linq中查询如何?
答案 0 :(得分:1)
您可以使用lead()
进行此操作:
select id, timestamp, data
from (select t.*, lead(data) over (order by timestamp) as next_data
from t
) t
where next_data <> data or next_data is null;
答案 1 :(得分:0)
SELECT *
FROM your_table YT1
WHERE NOT EXISTS (
SELECT *
FROM your_table YT2 yt2.data = yt1.data
AND yt1.id = yt2.id + 1
)
答案 2 :(得分:0)
这是Linq的解决方案:
class Row
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public string Data { get; set; }
}
var testData = new List<Row>
{
new Row { Id = 1, Timestamp = DateTime.Now.AddHours(-5), Data = "A" },
new Row { Id = 2, Timestamp = DateTime.Now.AddHours(-4), Data = "B" },
new Row { Id = 3, Timestamp = DateTime.Now.AddHours(-3), Data = "A" },
new Row { Id = 4, Timestamp = DateTime.Now.AddHours(-1), Data = "A" },
new Row { Id = 5, Timestamp = DateTime.Now.AddHours(-2), Data = "A" },
new Row { Id = 6, Timestamp = DateTime.Now.AddHours(-0), Data = "D" },
};
var orderedData = testData.OrderBy(row => row.Timestamp); //The list needs to be in correct order for the where statement to work.
var filteredResult =
orderedData.Where((row, i) =>
i + 1 == testData.Count || row.Data != testData[i + 1].Data); //Current row is last row OR Data of current row is different from next row.
foreach (var row in filteredResult)
{
Console.WriteLine($"ID: {row.Id}, Timestamp: {row.Timestamp}, Data: {row.Data}");
}