i'd like to find a way to implement a clickhouse (view|table) which can handling where conditions like
select * from sometable where a=1 and b=2 and c='whatever' and e=4+1
can returns
+-+-+--------+-+
|a|b|c |d|
+-+-+--------+-+
|1|2|whatever|5|
+-+-+--------+-+
Related
so the requirement is simple, my query is:
select to_tsquery('XXX');
What should be my string 'XXX' so that:
to_tsquery('XXX') returns pg_sleep(2)
Is there any way to do it?
PS: I am trying it for good motive.
I don't think there is a way for to_tsquery('XXX') to return pg_sleep(2). But if you just want to do something similar to select pg_sleep(2); you can do this:
select to_tsquery('XXX'||pg_sleep(2)); -- Simple sleep
select to_tsquery('XXX'||(select case when (<your_condition_here>) then pg_sleep(10) else pg_sleep(0) end)); -- Conditional sleep
I am trying to loop through a set of geometries and seeing if they intersect with another geometry, so something like this:
select unnest(
ARRAY[
ST_Intersects(box,ST_GeomFromText('POINT(4 4)')),
ST_Intersects(box,ST_GeomFromText('POINT(4.0001 4.0001)')),
ST_Intersects(box,ST_GeomFromText('POINT(4.4 4.4)')),
ST_Intersects(box,ST_GeomFromText('POINT(4.4002 4.4002)')),
ST_Intersects(box,ST_GeomFromText('POINT(5 5)'))
]
) from (select ST_MakeBox2D(ST_Point(3,2),ST_Point(5,4)) as box) as sq
How can I do this without having to call ST_Intersects on each point individually?
If you mean that you don't want to write the st_intersects on each one of them you can turn them around:
select st_intersects(st_MakeBox2D(ST_Point(3,2),ST_Point(5,4)), point) from unnest(
ARRAY[
ST_GeomFromText('POINT(4 4)'),
ST_GeomFromText('POINT(4.0001 4.0001)'),
ST_GeomFromText('POINT(4.4 4.4)'),
ST_GeomFromText('POINT(4.4002 4.4002)'),
ST_GeomFromText('POINT(5 5)')
]) point;
This way you don't repeat the intersecting, just unnest the array and check intersection to the box. Of course internally the check is always run for each of them.
I am trying to remove entries from an array field that are found in a query.
tablename.listfield::integer[] has a full list
I am trying to remove a list of values from that field, which are gathered within the update query.
the ARRAY_REMOVE method only accepts single values, and the intarray module which has int[] - int[] doesn't seem to be an option.
the ARRAY[] && ARRAY[] can boolean return if there is overlap, but that doesn't help me
basically what I need is a real working version of this concept, which I know does not work.
UDPATE tablename SET listfield = ARRAY_REMOVE( listfield, ( select id from othertable ) )
is it possible to get this done with maybe a tricky CTE setup or something?
thanks!
I'm not sure why you say intarray doesn't seem to be an option, because it works just fine:
... SET listfield = listfield - ( SELECT array_agg(id) FROM othertable )
But if you want to do this without installing the extension, you can UNNEST the array and use the EXCEPT construct:
... SET listfield = ARRAY(SELECT UNNEST(listfield) EXCEPT SELECT id FROM othertable)
I have a table valued function declared so that I can return several values in one go. The values are calculated using DECLARE and Maths and Date functions.
The function is structured such that it only takes a 'logged date', and a priority for issues in a support system. I honestly thought that I'd be able to select as follows:
SELECT SupportCall.*, dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority).* FROM SupportCall
I've actually ended up with:
SELECT SupportCall.*,
SLADays = (select SLADays from dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority)),
SLARecieved = (select SLAReceived from dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority)),
SLATarget = (select SLATarget from dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority)),
SLAHoursRemaining = (select SLAHoursRemaining from dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority))
From SupportCall
I can't see a possible join for an Apply (which I don't fully understand anyway).
Does anybody know whether the function calls with the same parameters will be executed once? If I'm not going to end up with lots of subqueries and function calls when the query runs, then I don't care, the code is actually quite tidy if not concise.
If there is a massive overhead, does anybody know how to select all columns from a table function of this kind (i.e. no keys, just several calculations on the same input data).
Thanks.
Mark
Don't do that! Inline queries are NEVER faster than JOINS or APPLY. Rewrite your query and check the IO. You can rewrite it something like:
SELECT SupportCall.*,
SLADays = gs.SLADays,
SLAReceived = gs.SLAReceived,
...
From SupportCall sc
CROSS APPLY dbo.GetSLAStatus(SupportCall.createDate, SupportCall.priority) gs
Can't you just do this:
SELECT C.*,
F.SLADays,
F.SLAReceived,
F.SLATarget,
F.SLAHoursRemaining
From
SupportCall C
cross apply dbo.GetSLAStatus(C.createDate, C.priority) F
I hope you're function is an inline function (e.g has a single statement that starts returns table return (...) and does not have a defined result table)
I am dynamically creating queries in SQL Server that are parameterized. When it comes to the CONTAINS predicate I have run into a question that I couldn’t find an answer to.
If the CONATINS will have logical operators (OR, AND, AND NOT) do I have to enumerate each of the values or do I use one parameter for all?
Here is a static example:
SELECT * FROM SomeTable WHERE CONTAINS(ColumName, ‘Cat and Dog’);
Is this the correct parameterized example?
SELECT * FROM SomeTable WHERE CONTAINS(ColumName, ‘#Q1 and #Q2’);
#Q1 = “Cat”
#Q2 = “Dog”
Or is this the correct approach?
SELECT * FROM SomeTable WHERE CONTAINS(ColumName, ‘#Q1’);
#Q1 = “Cat and Dog”
Or is it something entirely different?
At the time I asked the question I didn't have a database with full text enabled that I could test against. I was just writing the code and unit tests. Since then I managed to find a DB that I could test with.
The second example was the closest. The only problem was the the parameter was quoted so it would have searched on the literal "#Q1".
SELECT * FROM SomeTable WHERE CONTAINS(ColumName, #Q1);
#Q1 = “Cat and Dog”
If the query needs the a generation term, proximity term or wieghted term then those need to be in the parameter too.
#Q1 = 'FORMSOF(INFLECTIONAL, Cat) and FORMSOF(INFLECTIONAL, Dog)'
#Q1 = 'FORMSOF(THESAURUS, Cat) and FORMSOF(THESAURUS, "Dog")'
#Q1 = 'Cat NEAR Dog'