Simple subquery in Esper - complex-event-processing

I want to be able to nest queries expressed in Esper's EPL. Let's assume I want to detect this pattern: A -> (B -> C). (A, B and C are types of events, -> is EPL's sequence operator.)
Here is the query representing B -> C:
select * from pattern [every (b=B -> c=C)]
I would then like to do the following:
select * from pattern [every (a=A -> bc=
(select * from pattern [every (b=B -> c=C)])
)]
Actually, it would be best if something like this would be possible:
select * from pattern [every (b=B -> c=C)]) as bc
select * from pattern [every (a=A -> bc)]
This way, the first query is just bound to an identifier bc, which can then be used in the second query. That would be awesome!
Could someone please tell me if similar syntax exists in EPL? I appreciate any hint!

The query could simply be...
every (a=A -> (every (b=B -> c=C))]
Or insert "bc" into another stream and use that...
insert into BCStream select * from pattern[every b=B -> c=C]; // also note lack of parens
and
select * from pattern[a=A -> BCStream]

Related

Is it possible to get a field of a JSON object that is a field of a JSON object without casting twice?

Is the second way the only way to get the second result or is something else possible?
1.
select '{"a": [{"b":1}]}'::jsonb -> 'a' ->> 0;
---
{"b": 1}
2.
select ('{"a": [{"b":1}]}'::jsonb -> 'a' ->> 0)::jsonb -> 'b';
---
1
3.
select '{"a": [{"b":1}]}'::jsonb -> 'a' ->> 0 -> 'b';
---
No operator matches the given name and argument types...
casting (back to json) is probably only needed in 2 since you're casting the first element of a to text via ->>. try this instead:
select '{"a": [{"b":1}]}'::jsonb -> 'a' -> 0 -> 'b';
It should work and return 1 (as a json object) which you then might have to cast to int or another appropriate type to reuse in any computation.
alternatively, this should also work, and is shorter as well:
select '{"a": [{"b":1}]}'::jsonb #> '{a,0,b}';
(disclosure: i'm not at a computer with postgresql. i did not test the above queries)

OrientDB query all out edges

I want to do a query in OrientDB but i don't know how it works.
I'm using the Graph API and my DB looks like this:
class(V): #route (Name, Start, Length, Goal)
class(V): #direction (Goal, length)
class(E): #has_A
class(E): #start_Direction
class(E): #has_Follower
The Edges are connected to the Vertices like this:
route -> has_A -> direction
route -> start_Direction -> direction
direction -> has_Follower -> direction
now i need to find all followers by the name of the route,
what works so far is when i enter a rid (of the class startDirection) directly to the query for example:
select * from (traverse outE('has_Follower'),has_Follower.in from #??:?) where #class='direction'
But now i need to get to the rid over the Name of the route (which is the problem), what i tried so far is:
select * from (traverse outE('has_Follower'),has_Follower.in from (select #rid from start_Direction where start_Direction.out = (select #rid from route where Name = 'nameOfroute'))) where #class='direction'
But this gives me an empty query, although when i type in the rid from start_Direction of which i know has a has_Follower it works.
Hope this is understandable, thanks for any help in advance.
One problem in your query is probably start_Direction.out = (select..., you have to use an IN instead of an =, eg start_Direction.out IN (select....
In general, I think the easiest way to do what you need is as follows:
MATCH
{class:route, as:route} -start_Direction-> {} -has_Follower-> {as:direction, while:(true)}
RETURN route.#rid, route.Name, route.Start, route.Length, route.Goal, direction.Goal, direction.length
Or just RETURN $elements if you want single records per row

What's the opposite of the intersect function in OrientDB?

I'm currently looking for the opposite of the function intersect
Let me explain with some queries :
the first one return a list of #rid
The seconde one retun a list of #rid
The third one return a list of #rid which are in the first and in the second one
-> I'm looking for the query that return a list of #rid which are in the first but not in the second
SELECT in("Regroupe").in("Obligatoire").out("Pertinent") FROM 89:50
-> #69:110 #62:19 #60:1 #59:38 #62:114
SELECT out("Pertinent") FROM 89:50
-> #69:110 #62:19 #60:1
SELECT intersect(in("Regroupe").in("Obligatoire").out("Pertinent"), out("Pertinent")) FROM #89:50
-> #62:19 #60:1 #69:110
I'm looking for this query :
SELECT except/difference(in("Regroupe").in("Obligatoire").out("Pertinent"), out("Pertinent")) FROM #89:50
-> #59:38 #62:114
Ok i find what was missing !
I have to compare on #rid :
SELECT difference(in("Regroupe").in("Obligatoire").out("Pertinent").#rid, out("Pertinent").#rid) FROM #89:50

Combine SUM and CAST - not working?

PostgreSQL Unicode 9.01 doesn't like:
SELECT table1.fielda,
SUM (CAST (table2.fielda AS INT)) AS header.specific
FROM *etc*
What is wrong with SUM-CAST?
Error Message:
Incorrect column expression: 'SUM (CAST
(specifics_nfl_3pl_work_order_item.delivery_quantity AS INT))
Query:
SELECT specifics_nfl_3pl_work_order.work_order_number,
specifics_nfl_3pl_work_order.goods_issue_date,
specifics_nfl_3pl_work_order.order_status_id,
SUM (CAST (specifics_nfl_3pl_work_order_item.delivery_quantity AS INT)) AS units
FROM public.specifics_nfl_3pl_work_order specifics_nfl_3pl_work_order,
public.specifics_nfl_3pl_work_order_item specifics_nfl_3pl_work_order_item,
public.specifics_nfl_order_status specifics_nfl_order_status
WHERE specifics_nfl_3pl_work_order.order_status_id In (3,17,14)
AND specifics_nfl_3pl_work_order_item.specifics_nfl_work_order_id=
specifics_nfl_3pl_work_order.id
AND ((specifics_nfl_3pl_work_order.sold_to_id<>'0000000000')
AND (specifics_nfl_3pl_work_order.goods_issue_date>={d '2013-08-01'}))
It would be really great if you can help.
If I were you, then I would do these steps:
give your table short aliases
format the query
use proper ANSI joins:
remove spaces between function name and (
select
o.work_order_number,
o.goods_issue_date,
o.order_status_id,
sum(cast(oi.delivery_quantity as int)) as units
from public.specifics_nfl_3pl_work_order as o
inner join public.specifics_nfl_3pl_work_order_item as oi on
oi.specifics_nfl_work_order_id = o.id
-- inner join public.specifics_nfl_order_status os -- seems redundant
where
o.order_status_id In (3,17,14) and
o.sold_to_id <> '0000000000' and
o.goods_issue_date >= {d '2013-08-01'}
Actually I really think you need group by clause here:
select
o.work_order_number,
o.goods_issue_date,
o.order_status_id,
sum(cast(oi.delivery_quantity as int)) as units
from public.specifics_nfl_3pl_work_order as o
inner join public.specifics_nfl_3pl_work_order_item as oi on
oi.specifics_nfl_work_order_id = o.id
where
o.order_status_id In (3,17,14) and
o.sold_to_id <> '0000000000' and
o.goods_issue_date >= {d '2013-08-01'}
group by
o.work_order_number,
o.goods_issue_date,
o.order_status_id
if it still doesn't work - try to comment sum and see is it working?
But you have a table2 or only table1?
Try:
SELECT table1.fielda,
SUM (CAST (table1.fielda AS INT)) AS "header.specific"
FROM etc
In addition to what #Roman already cleared up, there are more problems here:
SELECT o.work_order_number
,o.goods_issue_date
,o.order_status_id
,SUM(CAST(oi.delivery_quantity AS INT)) AS units -- suspicious
FROM public.specifics_nfl_3pl_work_order o,
JOIN public.specifics_nfl_3pl_work_order_item oi
ON oi.specifics_nfl_work_order_id = o.id
CROSS JOIN public.specifics_nfl_order_status os -- probably wrong
WHERE o.order_status_id IN (3,17,14)
AND o.sold_to_id <> '0000000000' -- suspicious
AND o.goods_issue_date> = {d '2013-08-01'} -- nonsense
GROUP BY 1, 2, 3
o.goods_issue_date> = {d '2013-08-01'} is syntactical nonsense. Maybe you mean:
o.goods_issue_date> = '2013-08-01'
You have the table specifics_nfl_order_status in your FROM list, but without any expression connecting it to the rest. This effectively results in a CROSS JOIN, which results in a Cartesian product and is almost certainly wrong in a very expensive way: every row is combined with every row of the rest:
CROSS JOIN public.specifics_nfl_order_status os
Either remove the table (since you don't use it) or add a WHERE or ON clause to connect it to the rest. Note, that it is not just redundant, it has a dramatic effect on the result as it is.
This WHERE clause is suspicious:
AND o.sold_to_id <> '0000000000'
Seems like you are storing numbers as strings or otherwise confusing the two.
Also, CAST (oi.delivery_quantity AS INT) should not be needed to begin with. The column should be of data type integer or some other appropriate numeric type to begin with. Be sure to use proper data types.
The default setting of search_path includes public, and you may not need to schema-qualify tables. Instead of public.specifics_nfl_3pl_work_order, it may suffice to use:
specifics_nfl_3pl_work_order
GROUP BY 1, 2, 3 is using positional parameters, just a notational shortcut for:
GROUP BY o.work_order_number, o.goods_issue_date, o.order_status_id
Details in the manual.
According to comments you are using MS Query to create the query. This is not the best of ideas. Produces the kind of inferior code you presented us with. You may want to get rid of that while you are working with PostgreSQL.

Table scoping difficulty: Joining on a sub query with a where clause from a table outside the subquery

I am trying to adapt a stored procedure into a view and I've run into what appears to be a table scoping issue in SQL Server 2008 R2.
In the sproc, there was a parameter that specified the companyId. Now we want to retrieve the same data but not limit it to a specific companyId but rather to group by that companyId.
However, my attempt broadening it has failed.
Select Distinct [T].[TruckID], [T].[VIN], [T].[Make], [T].[Model],
[T].[ModelYear], [T].[RFIDNo], [SQCT].[VendorCode], [SQCT].[UserLabel],
[T].[IsActive], [T].[IsFleetTruck], [SQDA].[DriverAssociations],
dbo.GetCurrentPlate([T].[TruckID]) [Plate],
[TCT].[CompanyId] -- // <- Now we're including it
From [dbo].[Truck] as [T]
Inner Join [dbo].[TruckerCompanyTruck] as [TCT]
On [T].[TruckID] = [TCT].[TruckId]
Left Outer Join (
Select [CT].[CompanyTruckId], [CT].[CompanyId],
[CT].[TruckId], [CT].[VendorCode], [CT].[UserLabel],
[CT].[CreateDate], [CT].[CreateBy], [CT].[UpdateDate],
[CT].[UpdateBy], [CT].[Version]
From [dbo].[CompanyTruck] as [CT]
) as [SQCT]
On [TCT].[CompanyId] = [SQCT].[CompanyId]
and [TCT].[TruckId] = [SQCT].[TruckId]
Left Join (
Select Distinct [TCTC].[TruckId], Count(*) [DriverAssociations]
From [TruckerCompanyTruck] [TCTC]
Inner Join [Trucker] [D]
On [TCTC].[TruckerId] = [D].[TruckerId]
Inner Join [TruckerContract] [TC]
On [TCTC].[TruckerId] = [TC].[TruckerId]
Where [TCTC].[CompanyId] = [TCT].[CompanyId] -- // Error!
and [TC].[CompanyId] = [TCT].[CompanyId] -- // Error!
Group By [TCTC].[TruckId]
) as [SQDA]
On [SQDA].[TruckId] = [T].[TruckId]
The "Error!" lines throw "The multi-part identifier "TCT.CompanyId" could not be bound."
The ultimate goal is that running this query will produce multiple rows for a truck that has multiple company associations and return the correct driver count.
In the end, this query of the view...
Select *
From [FlattenedTruck]
Where [CompanyId] = 28
Order By [VIN]
...should produce the same output as this.
Exec [GetTrucksForGridWithAssociationCounts] #companyId = 28
But the differ. The sproc returns 1 driver association for companyId 28 and the view returns 1246 driver associations which is all of the associations for that truck regardless of the company. (It's also different because the sproc does not return the companyId because it's passed in as a parameter.)
TruckID -> VIN -> Make -> Model -> ModelYear -> RFIDNo -> VendorCode -> UserLabel -> IsActive -> IsFleetTruck -> DriverAssociations -> Plate -> CompanyId
26 -> NULL -> NULL -> NULL -> NULL -> NULL -> NULL -> NULL -> 1 -> 0 -> 1246 -> US-WA-9D68812 28
26 -> NULL -> NULL -> NULL -> NULL -> NULL -> NULL -> NULL -> 1 -> 0 -> 1 -> US-WA-9D68812
Can you try OUTER APPLY instead of the LEFT JOIN to the SQDA subquery? You'll want to add the condition of [TCTC].[TruckId] = [T].[TruckId] to the WHERE clause within the subquery, as the APPLY doesn't take an ON condition.
When joining with subqueries, it is important to remember that joins inside the subquery are not aware of tables joined outside the subquery.
The error is being thrown because you are attempting to reference table TCT inside the parentheses of the SQDA subquery, but the join aliased to TCT is outside the parentheses. You are joining that same table inside the parentheses as TCTC so it is a simple matter of changing any use of TCT inside the subquery to TCTC, or moving it outside as appropriate.
So your WHERE clause would become WHERE [TC].[CompanyId] = [TCTC].[CompanyId] inside the subquery, and you would move the other clause outside to join to the subquery since WHERE [TCTC].[CompanyId] = [TCTC].[CompanyId] is pointless,
That will make the query work; without knowing more about the underlying data model I can't guarantee it will actually return what you intend it to return. But that should be sufficient to get you past the error you are currently getting.