_Name | Value | Timestamp
-----|-------|-----------------
One | 1 | 2016-01-01 02:00
Two | 3 | 2016-01-01 03:00
One | 2 | 2016-01-02 02:00
Two | 4 | 2016-01-03 04:00
_
_Name | Value | EarliestTimestamp | LatestTimestamp
-----|-------|-------------------|-----------------
One | 2 | 2016-01-01 02:00 | 2016-01-02 02:00
Two | 4 | 2016-01-01 03:00 | 2016-01-03 04:00
_
最新のName
とValue
を取得するためにROW_NUMBER()
と_PARTITION BY
_を使用しようとしていますが、最古の最新のTimestamp
も必要です値:
_SELECT
t.Name,
t.Value,
t.????????? AS EarliestTimestamp,
t.Timestamp AS LatestTimestamp
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS RowNumber,
Name,
Value
Timestamp) t
WHERE t.RowNumber = 1
_
これは、ウィンドウ関数min
およびmax
を使用して実行できます。
select distinct name,
min(timestamp) over(partition by name), max(timestamp) over(partition by name)
from tablename
編集:コメントに基づく
select t.name,t.value,t1.earliest,t1.latest
from t
join (select distinct name,
min(tm) over(partition by name) earliest, max(tm) over(partition by name) latest
from t) t1 on t1.name = t.name and t1.latest = t.tm
編集:別のアプローチはfirst_value
ウィンドウ関数。サブクエリと結合の必要性を排除します。
select distinct
name,
first_value(value) over(partition by name order by timestamp desc) as latest_value,
min(tm) over(partition by name) earliest,
-- or first_value can be used
-- first_value(timestamp) over(partition by name order by timestamp)
max(tm) over(partition by name) latest
-- or first_value can be used
-- first_value(timestamp) over(partition by name order by timestamp desc)
from t
[〜#〜] min [〜#〜] および [〜#〜] max [〜#〜] 関数+ OUTER APPLY を使用できます=:
SELECT t.Name,
p.[Value],
MIN(t.[Timestamp]) as EarliestTimestamp ,
MAX(t.[Timestamp]) as LatestTimestamp
FROM Table1 t
OUTER APPLY (SELECT TOP 1 * FROM Table1 WHERE t.Name = Name ORDER BY [Timestamp] DESC) p
GROUP BY t.Name, p.[Value]
出力:
Name Value EarliestTimestamp LatestTimestamp
One 2 2016-01-01 02:00 2016-01-02 02:00
Two 4 2016-01-01 03:00 2016-01-03 04:00
次のように、MIN(Timestamp) OVER (PARTITION BY Name)
列に加えてROW_NUMBER()
を使用します。
SELECT
t.Name,
t.Value,
t.EarliestTimestamp AS EarliestTimestamp,
t.Timestamp AS LatestTimestamp
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS RowNumber,
MIN(Timestamp) OVER (PARTITION BY Name) AS EarliestTimestamp,
^^
Name,
Value
Timestamp) t
WHERE t.RowNumber = 1
あなたの質問を理解したら、次のようにrow_number()
関数を使用します。
SELECT
t.Name,
t.Value,
min(t.Timestamp) Over (Partition by name) As EarliestTimestamp,
t.Timestamp AS LatestTimestamp
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) AS RowNumber,
Name,
Value,
Timestamp) t
WHERE t.RowNumber = 1
Group By t.Name, t.Value, t.TimeStamp
シンプルに考えてください。
select
t.Name,
MAX(t.Value),
MIN(t.Timestamp),
MAX(t.Timestamp)
FROM
t
group by
t.Name
あなたの質問を正しく理解している場合、row_number
関数を2回使用する1つのオプションを次に示します。次に、それらを同じ行に配置するには、conditional aggregation
を使用できます。
これは近いはずです:
SELECT
t.Name,
t.Value,
max(case when t.minrn = 1 then t.timestamp end) AS EarliestTimestamp,
max(case when t.maxrn = 1 then t.timestamp end) AS LatestTimestamp
FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP) as minrn,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY TIMESTAMP DESC) as maxrn,
Name,
Value
Timestamp
FROM YourTable) t
WHERE t.minrn = 1 or t.maxrn = 1
GROUP BY t.Name, t.Value