How can I solve this TDengine sql problem? - tdengine

I was trying to output a desired result of my data by using TDengine, sql is:
select * from taos_dn_disk_total where ts >= NOW-1d and ts <= NOW interval(10m) fill(prev);
And the result occurs:
DB error: invalid operation: functions not compatible with interval (0.000562s)
TDengine's official documentation notes that they support both interval operation and fill operation, but why the result shows 'not compatible'?

From official document
In a scenario of IoT, it is often necessary to aggregate the collected data by intervals through down sampling. TDengine provides a simple keyword interval, which makes query operations according to time windows extremely simple.
So interval can only use with aggregate function, you might want to change the SQL to something like:
select max(*) from taos_dn_disk_total where ts >= NOW-1d and ts <= NOW interval(10m) fill(prev);

Related

query and update a stream in flink stream sql

I am looking for a solution based on flink, the situation is that I have a trans stream and some rules which can be expressed as SQL, I want to update the stream after query(if matched ruleSql1 set this transEvent respCode = 01; if matched ruleSql2 then set this transEvent respCode = 02; respCode has priority).
The question is:
By flink sql I can get a result, but how to feedback the result to original stream, the output I expected is original stream with different respCode.
I have a lot of rules, how to merge the result.
Flink's operators have streams coming in, and transformed streams coming out. It's not clear exactly what you want -- but whether you want to modify each event to add a field with the response code, or something else, it's easily done. If you are using SQL, simply describe the output you want in the SELECT clause.
You can use split/select to make n copies of your stream, and then apply one of your rules (expressed as a SQL query) to each of these parallel copies. Then you can use union to merge them back together (provided they are all of the same type).
You'll find the documentation on split, select, and union in this section of the docs.
The Flink training site has a sequence of hands-on exercises that you may find helpful in learning how the pieces of the API fit together, though none that use split/select/union.

SQL Server Query SELECT Error (now trying LIMIT)

I am working on what should be a super simple query for SQL Server 2014. All I want to do is check that our systems can interface with SQL Server after updates, etc. So I need to just verify that it makes the connection correctly and finds a table within the Server.
Attempt 1:
SELECT TOP (1) *
From [X].[dbo].[Y]
WITH (NOLOCK);
But apparently 'top' is not a supported option with SQL Server 2014.
To add some more, here is the exact error I get when trying to run that: Syntax error. The token 'Top' is invalid. Please check the case of your operators (eg 'or' versus 'OR') and check that your functions use brackets after the function name eg Now(), eg Len("abc").
Attempt 2:
SELECT *
From [X].[dbo].[Y]
WITH (NOLOCK)
LIMIT (1);
That one tells me that I need to put data items between [], text between "", and functions as FunctionName(). However...I don't see where I missed any of those.
Can anybody possibly shed some light on why my query isn't going through? Any help would be appreciated.
The first attempt should work just fine:
SELECT TOP (1) *
From [dbo].[Y]
WITH (NOLOCK);
See example
If it doesn't work, you should include the error message.

SQL Server : function precedence and short curcuiting in where clause

Consider this setup:
create table #test (val varchar(10))
insert into #test values ('20100101'), ('1')
Now if I run this query
select *
from #test
where ISDATE(val) = 1
and CAST(val as datetimeoffset) > '2005-03-01 00:00:00 +00:00'
it will fail with
Conversion failed when converting date and/or time from character string
which tells me that the where conditions are not short-circuited and both functions are evaluated. OK.
However if I run
select *
from #test
where LEN(val) > 2
and CAST(val as datetimeoffset) > '2005-03-01 00:00:00 +00:00'
it doesn't fail, which tells me that where clause is short-circuited in this case.
This
select *
from #test
where ISDATE(val) = 1
and CAST(val as datetimeoffset) > '2005-03-01 00:00:00 +00:00'
and LEN(val) > 2
fails again, but if I move length check to before cast, it work. So looks like the functions are evaluated in the order they appear in query.
Can anyone explain why first query fails?
It fails because SQL is declarative so the order of your conditions is not taken into account when the plan is generated (nor is it required to do so).
The usual way to get around this is to use CASE which has strict rules about sequence and when to stop.
In your case you will probably need nested CASEs, something like this:
WHERE
(
case when ISDATE(val) = 1 then
case when CAST(val as datetimeoffset) > '2005-03-01 00:00:00 +00:00' and
LEN(val) > 2
THEN 1 ELSE 0 END
ELSE 0
END
) = 1
(note this is unlikely to be actually correct SQL as I just typed it in).
By the way, even if you get it "working" by rearranging the conditions, I'd advise you don't. Accept that SQL simply doesn't work in that way. As the data changes & stats change, SQL is upgraded, workload varies, indexes are added the query plan could change. Any attempts to "get it working" are going to be short-lived at best so go with the CASE which will continue to work once you've got it right (provided you nest CASE statements where required and don't fall into the same precedence trap in the CASE conditions!)
The mystery is answered if you examine the Execution Plan. Both the CAST() and the LEN() are applied as part of the Table Scan step, while the test for IsDate() is a separate Filter test after the Table Scan.
It appears that the SQL Engine's internal optimizations use certain filtering functions as part of the retrieval of the data, and others as separate filters, almost certainly as a form of query optimization to minimize the load from disk into main memory. However, more complex functions, such as IsDate(), which is dependent on system variables such as system date format in some cases (is '01/02/2017' Jan 2nd or Feb 1st?), need to have the data retrieved before the filter is applied.
Although I have no hard information on this, I strongly suspect that any filter more resource intensive than a certain level is delegated to the Filter steps in the query plan, and anything simple/fast enough to be checked as the data is being read in is applied during the Scan/Seek steps. Also, if a filter could be applied on the data in the index, I am certain that it will be tested before any non-index data is tested, solely to minimize disk reads, which are bad performance juju (this may not apply on the Clustered index of the table). In these cases, the short-circuiting might not be straightforward, with an IsDate() test specified on a non-index field being executed after a similar test on an indexed field, no matter where they are in the list of conditions.
That said, it appears to be true that conditions short-circuit when they are executed in the same step of the query plan. If you insert a string like '201612123' into the temp table, then add a check on Len(val) < 9 after the date comparison, it still generates an error, instead of checking both LEN() conditions at the same time in a tiny optimization.
which tells me that where conditions are not short-circuited and both functions are evaluated.
To expand on LoztInSpace's answer, your terminology suggests you are not interpreting SQL correctly, on its own terms.
The various parts of a SELECT statement are not "functions". The entire statement is atomic. You supply the query as unit, and the DBMS responds. There is no "before" and no "after". There is just the query.
Those are the rules. Your job in formulating the query is to supply one that is valid. It's a logical progression: valid question, valid answer, etc. The moment you step out of that frame, you might as well be asking, "why is the sky seven?".
One a small clarification to #LoztInSpace's answer. When he refers to the order of your statements, he's presumably talking about the phrasing of your query, which for purposes of evaluation is inconsequential. Sequential SQL statements are executed sequentially, as presented. That is guaranteed by the SQL standard.

How to do GROUP BY with COUNT() and ordering by COUNT in influxdb?

Im using influxDb and recording user visits to a dictionary pages and trying to get some queries working.
Like for example I'm trying to find out how to get a sorted set of headwords sorted by a number of visits to a particular word definition within some timeframe. So basically words sorted by a number of visits.
Im trying something like this:
SELECT COUNT(*) FROM lookup GROUP BY word ORDER BY count_value LIMIT 100
^But it doesn't work. Error message is "Server returned error: error parsing query: only ORDER BY time supported at this time".
Is what im trying to do not achievable in influxDb?
As noted by the error that was returned
Server returned error: error parsing query: only ORDER BY time supported at this time
InfluxDB only supports ORDER BY time at the moment. To achieve the result that you're looking for you'd need to do the ORDER BY client side.

Can I use HAVING instead of WHERE in SQL queries?

I always thought that I could not, but MSDN says otherwise.
When GROUP BY is not used, HAVING behaves like a WHERE clause.
I had checked and got the error:
Msg 8121:
Column '...' is invalid in the HAVING clause because
it is not contained in either an aggregate function or the GROUP BY.
So, what is it? An error in documentation, or a little-known detail?
This may be one of those things that worked in Sybase SQL Server. Now Microsoft rewrote SQL Server closer to the ANSI standard, but forgot to fix documentation.
This is the case?
You can use having without using group by but on aggregate function.
select avg(price) from tbltemp having avg(price) >= 2
In the MSDN link you provided, the example given instructs us to use where instead of having.
You may NOT write something like "select avg(price) from goods HAVING
price >= 1000" but you may write "select avg(price) from goods WHERE price >= > 1000"

Resources