Aggregate Math on TimeStamp fields - advantage-database-server

I need a timestamp difference calculated!. The purpose is to determine the number of records entered into a database expressed in records per hour. The sql is something like
SELECT (MAX(myTimeStampField)-MIN(myTimeStampField))/COUNT(*) FROM myTable
Which does not work :(

This will give you records per hour:
SELECT COUNT(*) / CONVERT( TIMESTAMPDIFF( SQL_TSI_HOUR, MIN( myTimeStampField ),
MAX( myTimeStampField )), SQL_DOUBLE )
FROM myTable
You can read more about our SQL date & time functions here.

Perhaps I'm missing the point, as it seems like a simple count(*) using between dt1 and dt2 would do the trick?
SELECT COUNT(*)
FROM tablex t
WHERE t.dtfield BETWEEN dt1 AND dt2
Reinaldo.

Related

In Google sheets, can a QUERY statement determine the oldest date in a column, add 6 months to it , then display that range?

Currently, I am having to query the entire set, jot down the oldest date, and then add a GTE (>=) and a LTE (<=) back into the query statement to limit the results to 6 months, like this:
=QUERY(RawData!A1:D, "SELECT * WHERE A = 'N502SY' and C >= date '2020-03-20' and C <= date '2020-09-20' ORDER BY C ASC")
Ideally, the query statement would determine ON ITS OWN what the oldest date is and then display all rows found with that date PLUS all rows that have a date within a 6-month period (from the oldest date).
Have done a ton of google searching to find an answer but have come to the conclusion that QUERY statements may not be as robust as I am hoping for. Here is the URL (kindly do not change the raw date):
https://docs.google.com/spreadsheets/d/17n3PYBrhlbD1RIgToohS3a1QbN5qhrQnREcMAnZdcbY/edit#gid=626607315
your shared sheet has restricted access. so here's a working formula based on rough sample data.
=QUERY(A:D, "SELECT * WHERE A = 'N502SY' and C >= date '"&text(EDATE(MAX(C:C),-6),"yyyy-mm-dd")&"' and C <= date '"&text(MAX(C:C),"yyyy-mm-dd")&"' ORDER BY C ASC")
-
You can try with FILTER and SORT too:
=SORT(FILTER(A1:D,A:A="N502SY",C:C>=EDATE(MAX(C:C),-6)),3,1)

Get the value timestamp when grouping by time in InfluxDB

I am trying to get the MAX/MIN values of an interval of time, and I would like to get the correspond timestamp of the value.
If I run: SELECT max(value) FROM data WHERE time > 1549034249000000000 and time < 1550157449000000000 GROUP BY time(10s)
I am receiving the timestamp of the range beginning instead of the max(value) timestamp.
What alternatives could there be for receiving the max(value) of an interval and his timestamp?
In SQL is possible to execute a query like: SELECT value DENSE_RANK () OVER (PARTITION BY time ORDER BY variableName DESC) AS Rank FROM tableName Is not possible to run something like that in InfluxDB?
Yout can not. When you use group by you get ever the beginning timestamp of the group by.
The alternative is not to use group by.

Using InfluxDB subquery to subtract values

I have a Influx database that is getting filled with values. These values are presented by Grafana. What I need is to get the actual values depending on the selected time interval.
Currently I have the following query for a single metric:
SELECT mean("value") FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
What I want is to subtract the lowest value from that interval, so it only counts the values from within that interval. So the graph needs to start at zero. To get the lowest value from that interval I use:
SELECT min("value") FROM "table" WHERE $timeFilter
So I thought combining those two (with a subquery) like this should work:
SELECT mean("value") - (SELECT min("value") FROM "table" WHERE $timeFilter) FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
Unfortunately this doesnt work. The query is not accepted as a subquery.
This is possible using InfluxDB's built-in functions:
SELECT cumulative_sum(difference((mean("value")))) FROM "table" WHERE $timeFilter GROUP BY time($interval) fill(null)
This takes the difference between consecutive data points (ignoring the absolute value), and cumulatively sums this over the selected time range.
I'm not entirely sure, but this query requires an additional mean() associated with the GROUP BY clause. Perhaps it's better to use max or first instead, depending on your needs.
Update
Alternatively, you can indeed use subqueries and GROUP BY to achieve this, but this way you can only get time ranges that can be represented by GROUP BY (e.g. from 14:13 to 15:16 on July 6 2010 is more difficult).
SELECT (energy_live-energy_fill) as Energy FROM
(SELECT first(value) as energy_fill from energyv3 WHERE $timeFilter GROUP BY time(1d)),
(SELECT first(value) as energy_live from energyv3 WHERE $timeFilter GROUP BY time($__interval))
fill(previous)

Delphi 7 + Zeos: how to prevent sqlite 3 converting date and integer/float to string?

Test database:
create table a ( d date not null primary key);
create table b ( d date not null primary key);
insert into a values ('2013-01-01');
insert into b values ('2014-01-01');
Using Zeos lib with Delphi 7, these queries all return TDateTimeField:
select d from a order by 1;
select d from b order by 1;
select d from a union all select d from b;
select * from (select d from a union all select d from b) s;
However, this query returns a TStringField:
select * from (select d from a union all select d from b) s order by 1;
Questions:
Why is that?
How do I prevent this from happening?
Is this a bug? How on earth ordering a result set changes the type of a column????
This is a serious problem because I cannot generate SQL queries from my program and open them in a TZReadOnlyQuery that was created in design time
Update: it does not work with integers either.
create table c ( id integer not null primary key);
create table d ( id integer not null primary key);
insert into c values(1);
insert into d values(2);
These result in TLargeIntField:
select * from c order by 1
select * from d order by 1
select * from ( select * from c union all select * from d) s
However, this results in a TStringField:
select * from ( select * from c union all select * from d) s order by 1
As stated here:http://www.sqlite.org/datatype3.html
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian
day numbers, the number of days since noon in Greenwich on November
24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER
as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
So, can you try the statement below and post the results?!
select date(*) as test from (select date(d) from a union all select date(d) from b) s order by 1;

Display users shift with from and to date in rails

I have the following table which contains date and user shift.
In the view i have to display in tabular form from_date[Shift start date] to_date[Shift end date.] and shift_id of users.It seems to be complex.I'm thinking of using "group by" but I'm not able to do it.If you guys can just give me the query i can display it.
Any help will be greatly appreciated.
I suppose you are trying to select start date and end date in one query. If so, the query could be like this:
SELECT a.*, (SELECT min(shift_date) FROM shifts
WHERE user_id = a.user_id AND shift_date > a.shift_date) end_date
FROM shifts a
You can adapt this query according to your requirements and convert it into Rails' query.

Resources