Is there a way to sort influxDb by a field value other than time? - grafana

I'm trying to sort results from influxdb query but I can't get it to sort by anything other than time.
this works
SELECT last("value") FROM "table" WHERE $timeFilter GROUP BY "tag" ORDER BY "time" DESC
but this does not work
SELECT last("value") FROM "table" WHERE $timeFilter GROUP BY "tag" ORDER BY "value2" DESC
Anyone have any ideas?
Thanks!

Since sorting is only possible for the timestamp and tags, you might think about using a tag instead of a field for value2. But beware that this is only reasonable if the content of the newly created tag is not too variable. Otherwise you might kill influxDB with too many timeseries.
Details to be found here: https://docs.influxdata.com/influxdb/v1.7/concepts/schema_and_data_layout/
A different option could be to SELECT all data and to sort "manually" – in your code. But if that is reasonable depends on the usecase and the amount of data you get.

Ordering by something else than 'time' is unfortunately not allowed at this time. Maybe in the future...
https://github.com/influxdata/influxdb/issues/3954

Related

What will be the best way to get only changed objects since specific date-time in priority-web-sdk ERP

I'm new to priority-web-sdk ERP.
I'm trying to get all the elements that were changed since a specific date-time.
I've tried to use the example from Priority postman collection:
https://www.eshbelsaas.com/ui/odata/Priority/tabmob.ini/usdemo/ORDERS?$since=2020-06-01T01:15+02:00&$expand=ORDERITEMS_SUBFORM
Unfortunately, the response contains all the records, and doesn't change if I change the date.
Help,
:)
You can try using Odata 'ge' operator (greater/equal) on the field you want to filter by.
For example, to filter by CURDATE field:
https://www.eshbelsaas.com/ui/odata/Priority/tabmob.ini/usdemo/ORDERS?$filter=CURDATE ge 2021-06-24T00:00:00Z
to filter by STATUSDATE field:
https://www.eshbelsaas.com/ui/odata/Priority/tabmob.ini/usdemo/ORDERS?$filter=STATUSDATE ge 2021-06-24T00:00:00Z

Get entire record with max field for each group

There are a lot of answers about this problem, but none of them retrieves the entire record, but only the ID... and I need the whole record.
So, I have a table status_changes that is composed of 4 columns:
issue_id : the issue the change refers to
id: the id of the change, just a SERIAL
status_from and status_to that are infact the status that the issue had before, and the status that the issue got then
when that is a timestamp of when this happened
Nothing too crazy, but now, I would like to have the "most recent status_change" for each issue.
I tried something like:
select id
from change
group by issue_id
having when = max(when)
But this has obviously 2 big problems:
select contains fields that are not in the group by
2 having can't contains aggregate function in this way
I thought of "ordering every group by when and using something like top(1), but I can't figure out how to do it...
Use PostgreSQL's DISTINCT ON:
SELECT DISTINCT ON (issue_id)
id, issue_id, status_from, statue_to, when
FROM change
ORDER BY issue_id, when DESC;
This will return the first result (the one with the greatest when) for each issue.

sphinxAPI more than one sort method implementation

while fetching records from sphinxApi , i have used the following sort mode. SetSortMode (SPH_SORT_EXTENDED, " field DESC"). Records need to be display randomly. so i have added the below sort method also. SetSortMode(SPH_SORT_EXTENDED, "#random");But records display randomly. But records not display based on first sort method. How to implement two sort methods in single query? Please suggest. Advance thanks
SetSortMode(SPH_SORT_EXTENDED, "field DESC, #random");
(Although I dont know for sure if #random can be combined with others. Extended does support multiple sort orders otherwise)

FOR LAST - Query, giving wrong result

I'm looking to use the following query to find the last tender id.
FOR EACH tender-table NO-LOCK WHERE tender-table.kco = 1 BY tender-table.id:
DISPLAY tender-table.id.
END.
This query looks at all the tender id's and brings back all the results of all the id's in ascending order. The results i get are
1,035
1.036
......
1,060
1,061
1,062
1,063
1,064
1,065
1,066
FOR LAST tender-table NO-LOCK WHERE tender-table.kco = 1 BY tender-table.id:
DISPLAY tender-table.id.
END.
However when i use this query to find the last id, i get the result,
1,061
When I should be seeing the result 1,066. Can anyone suggest why this is happening?
FOR LAST is a very deceptive statement. (So is FOR FIRST.) It does not behave in an intuitive manner. The sort order is NOT specified by the BY statement. You will get the LAST record according to the index which is used and no sorting will take place. When the BY refers to an unindexed field (or one which does not sort in the order of the index actually used) or when the WHERE clause does not obviously map to an index in the order that you are hoping for you will have mysterious records chosen.
Personally, I strongly suggest that you forget about using FOR FIRST & FOR LAST. A better option, which always sorts as expected, would be:
FOR EACH tableName WHERE someCriteria BREAK BY sortOrder:
LEAVE.
END.
DISPLAY whatEver.
(Add "DESCENDING" to flip from FIRST to LAST...)
Just in case anyone needs convincing -- try this with the "sports" database:
for first customer no-lock by discount:
display name discount.
end.
Sorry I have managed to figure it out that the 1,066 values didn't have tender-table.kco = 1. this solves the problem. thanks your time.

Postgres default sort by id - worldship

I need to setup worldship to pull from one of our postgres databases. I need to have it so that the packages are sorted by id. I have no way (that i am aware of) of having worldship send the order by clause so I need to have the default for the records returned to be returned by id.
On a second note I have no idea how postgres default sorts it looks like it by the last time the record was changed so if i write a two records id 1,2 then change record 2 when I run the query it returns them with record 2 being first.
Rows are returned in an unspecified order, per sql specs, unless you add an order by clause. In Postgres, that means you'll get rows in, basically, the order that live rows read on the disk.
If you want a consistent order without needing to add an order by clause, create a view as suggested in Jack's comment.
There is no such thing as a "default sort". Rows in a table are not sorted.
You could fake this with a view (as suggested by Jack Maney) there is no way you can influence the order of the rows that are returned.
But if you do that, be aware that adding an additional ORDER BY to a SELECT based on that view will sort the data twice.
Another option might be to run the CLUSTER command on that table to physically order the rows on the disk according to the column you want. But this sill does not guarantee that the rows are returned in that order. Not even with a plain SELECT * FROM your_table (but chances are reasonably high for that).
You will need to re-run this statement on a regular basis because the order created by the CLUSTER command is not automatically maintained.
For what it's worth, which probably isn't much, from my testing, it appears that PostgreSQL's "default" ordering is based on the time the records were last updated. The most recently updated records will appear last. Note that I couldn't find any documentation to support this. It's just what I've found from my own testing.
You could eventually use a sorted index, which should guarantee you order of retrieved rows in case the query plan hits the index, or if you force it, but this approach will be more than circuitous :). ORDER BY clause is the way to go as mentioned already.