I have a kusto query which has columns 'a','b', 'c','d' and 'timestamp'.
It basically is as
object
| serialize rn = row_number()
| project rn, a, b, c, d, timestamp
| project-reorder rn, timestamp
| order by timestamp desc
This returns output having timestamp as the first column which I do not want. Row number should be the first column followed by timestamp.
I have tried to order the query in multiple ways but still not able to get the desired output. How can this be done?
Strange, haven't noticed until I read your post and I can verify this behavior.
Got it working as you need it by renaming the timestamp column:
object
| serialize rn = row_number()
| project rn, a, b, c, d, time1 = timestamp
| project-reorder rn, time1
| order by time1 desc
Related
I have a table:
id | updated_at
1 | 2018-10-22T21:00:00Z
2 | 2018-10-22T21:02:00Z
I'd like to find the largest delta for a given day between closest updated timestamps. For example, if there were 5 rows:
id | updated_at
1 | 2018-10-22T21:00:00Z
2 | 2018-10-22T21:02:00Z
3 | 2018-10-22T21:05:00Z
4 | 2018-10-22T21:06:00Z
5 | 2018-10-22T21:16:00Z
The largest delta is between 4 and 5 (10 minutes). Note that really when comparing, I just want to find the next closest updated_at timestamp and then give me the max of this. I feel like I'm messing up the subquery to do this.
with nearest_time(time_diff)
as
(
select datediff('minute', updated_at as u1, (select updated_at from table where updated_at > u1 limit 1) as u2)
group by updated_at::date
)
select max(select time_diff from nearest_time);
demo:db<>fiddle
SELECT
lead(updated) OVER (ORDER BY updated) - updated as diff
FROM dates
ORDER BY diff DESC NULLS LAST
LIMIT 1;
Using window function LEAD allows you to get the value of the next row: In this case you can get the next timestamp.
With that you can do a substraction, sorting the results descending and take the first value.
Use lag to get the updated_at from the previous row and then get the max difference per day.
select dt_updated_at,max(time_diff)
from (select updated_at::date as dt_updated_at
,updated_at - lag(updated_at) over(partition by updated_at::date order by updated_at) as time_diff
from tbl
) t
group by dt_updated_at
One more option using DISTINCT ON (only works on Postgres..as the question was initially tagged Postgres, keeping this answer)
select distinct on
(updated_at::date)
updated_at::date as dt_updated_at
,updated_at-lag(updated_at) over(partition by updated_at::date order by updated_at) as diff
from dates
order by updated_at::date,diff desc
nulls last
I have a table where as below, I want to get records by id which have value b if value b is not there then value b. I have a date volumn associated in the table by which I can use order by date.
I tried to post an image of screen from mobile it failed attach.
Also could not post sql which i tried as it is officially not allowed here.
My attempt to explain the question is below
Persid. Value. Date
1 a
1 a
1 a
2 b
2 a
2 a
Expected result
1 a. Latest date
2 b any date
I have tried using group by on id and value columns to get count but I am not able to understand how to use that count to filter out records.
Any help is greatly appreciated :)
You can use row_number():
select id, value, date
from (select t.*,
row_number() over (partition by persid
order by value desc, date desc
) as seqnum
from t
) t
where seqnum = 1;
This gets the latest date for any value. Note that the value desc is so "b" appears before "a". If your values are not ordered alphabetically, then you can use a case or other mechanism to set the proper sort order.
I have a record with same ID but different data in both rows
While updating the final result should be the last record of that ID present in data.
Example
ID | Name | PermanentAddrss | CurrentLocation
1 | R1 | INDIA | USA
1 | R1 | INDIA | UK
Now for ID 1 the record which will be loaded in database
1|R1|INDIA|UK
How this can be done in SQL server for multiple records?
Please understand that SQL server does not store or fetch data in order of data insertion, so to find the latest/last record you should have some way to order the records.
This is typically a timestamp column like last_modified_date. Your current table is prime candidate for a slow changing dimension type 2; and you should consider implementing it.
See explanation on Kimball's group site.
If you are really not affected by any order and just need a row for each id you can try below query.
select
ID,
Name,
PermanentAddress,
CurrentLocation
from
(select
*,
row_number() over(partition by id order by (select null)) r
from yourtable)t
where r=1
You can identify the latest ID value by:
SELECT B.ID, A.NAME, A.PERMANENTADDRS, A.CURRENTLOCATION
FROM
(SELECT ID, NAME, PERMANENTADDRS, CURRENTLOCATION, MAX(RNUM) AS LATEST_ID FROM
(SELECT ID, NAME, PERMANENTADDRS, CURRENTLOCATION, ROW_NUMBER() OVER (PARTITION BY ID) AS RNUM FROM YOUR_TABLE)
GROUP BY ID, NAME, PERMANENTADDRS, CURRENTLOCATION) A
INNER JOIN
YOUR_TABLE B
ON A.LATEST_ID = B.ID;
This will take the last populated record for a given ID value. If the logic for latest record is different, it can be appropriately incorporated in the query.
Hi guys i have a postgres table with a column for event and a column for sequence. Every event may have multiple sequences. For ex:
Event | Sequence
a | 1
a | 4
a | 5
b | 1
b | 2
Now i know that select min(sequence) group by event gives me the minimum sequence. How do i get the very next value after the min value. i hope that makes sense. Thanks in advance.
I'm Using Postgres 9.3.
You can use ROW_NUMBER() partitioning by Event and ordering by Sequence to get the second lowest sequence number per Event;
SELECT Event, Sequence
FROM (
SELECT Event, Sequence,
ROW_NUMBER() OVER (PARTITION BY Event ORDER BY Sequence) rn
FROM Table1
) z
WHERE rn = 2;
An SQLfiddle to test with.
EDIT A bit more complicated, but if you need a query that doesn't rely on ROW_NUMBER(), use a subquery with a self-join to exclude rows with minimum sequence for each event:
SELECT outer_query.Event, MIN(outer_query.Sequence) AS SecondMinSeq
FROM Table1 as outer_query
INNER JOIN (
SELECT Table1.Event, MIN(Sequence) AS MinSeq
FROM Table1
GROUP BY Table1.Event
) AS min_sequences
ON outer_query.Event = min_sequences.Event AND outer_query.Sequence <> min_sequences.MinSeq
GROUP BY outer_query.Event
SQL Fiddle: http://sqlfiddle.com/#!15/4438b/7
Select A, min(b) from TableX
group by a
This works but I want one more piece of information. The output will be one row for each A with A and the min(b) for that A.
But I also want C from that row.
I cannot figure out how to do it!
MS SQL Server 2012
"C" is the sysident of the row.
So table has
Sysident ID Date
1 100 2014-01-01
2 100 2014-01-02
3 200 2014-02-01
4 200 201-002-05
etc
I want output of
Sysident id Date
1 100 2014-01-01
3 200 2014-02-01
I can get the ID and min date with a simple Select ID, Min(date) group by ID but don't know how to get the Sysident for each of the rows.
When I write/edit this, my sample table looks like a table but when it displays it is all run together. I have searched HELP for formatting so it will look like a table but cannot find anything.
The question is very clear (to me). For every unique A, I want the sysident of the row with the oldest date and what that date is.
If you want the first date, you can use min:
select id
, min(sysident)
, min(date)
from YourTable
group by
id
If you want a specific version of sysident, say the first ordered by the date column, you can use SQL Server's row_number():
select *
from (
select row_number() over (
partition by id
order by [date]) as rn
, min([date]) over (partition by id) as min_date
, id
, sysident
from YourTable
) as SubQueryAlias
where rn = 1 -- Only oldest row per value of id
For more answers, check out the greatest-n-per-group tag.