Given a table such as this,
I want to create a table via SQL without doing window functions
| id | time | attribute1 | attribute1atFirstRecordedTimeperID |
|---|---|---|---|
| 1 | 0 | a | a |
| 1 | 2 | b | a |
| 2 | 0 | c | c |
| 2 | 4 | d | c |
I tried
SELECT
t1.id,
t1.time,
t1.attribute1,
attribute1fixedattime0 = t2.attribute1
FROM s t1
INNER JOIN (
SELECT attribute1 AS attribute1fixedattime0
FROM s
WHERE time = 0
) t2 ON t1.id = t2.id
Is the column alias correct?
If I fix this code, I want to make the SQL code work for the case when id=2 does not have their first attribute1 recorded at time t=0, but for t>0. Then do I do a min on the join condition or inside the JOIN?
I want to create a table via SQL without doing window functions to turn
into
To go from
| id | time | attribute1 |
|---|---|---|
| 1 | 0 | a |
| 1 | 2 | b |
| 2 | 42 | c |
| 2 | 69 | d |
to the table
| id | time | attribute1 | attribute1atFirstRecordedTimeperID |
|---|---|---|---|
| 1 | 0 | a | a |
| 1 | 2 | b | a |
| 2 | 42 | c | c |
| 2 | 69 | d | c |
if you use a row_number() window function to mark the spot at which first time is within every id, then how do you join or would you not use a join but something else?
SELECT
id,
time,
attribute1,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY time) AS rn
FROM your_table
SELECT
id,
time,
attribute1,
FIRST_VALUE(attribute1) OVER (PARTITION BY id ORDER BY time ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS attribute1atFirstRecordedTimeperID
FROM (
-- Previous query here
) AS subquery

