Getting ordered subset of records - sql

I have table which basically describes a bus route. The table includes all the stops of a particular route.
This SQLFiddle shows an example. The data shows the flow in one direction, but a route can also be in the other direction.
I need to get a subset of this route (or all of it) depending on the start and end stations. So for example, I might have these:
A -> M (subset would be B -> L)
A -> K (subset would be B -> J)
C -> H (subset would be D -> G)
I -> M (subset would be J -> L)
However, if the direction is from, for example, H -> B, the subset would need to be G -> C.
The sequence column is for this purpose.
Is there a clean and easy way to do this using just SQL?

Assuming I understand your question, it seems simple enough.
To get a subset just use a between...and operator on the sequence column:
SELECT stop_from, stop_to
FROM routes
WHERE sequence BETWEEN 2 AND 11
ORDER BY sequence
To get the subset in the opposite direction, just order by desc, and select the stop_to as stop_from, and stop_from as stop_to:
SELECT stop_to As stop_from, stop_from As stop_to
FROM routes
WHERE sequence BETWEEN 3 AND 7
ORDER BY sequence DESC
See fiddle here

Related

Optaplanner - Can we specify the sequence order for visits or add dependency/relation constraints to the visit

I have four customer visits A, B, C, D after calling the optaplanner solve method i got the optimal solution like as below
B -> C -> A -> D
But in my use case we need to arrive VISIT_C after completion of VISIT_B and VISIT_A

Can I rewrite this Cypher query to be compatible with Redis Graph?

My use-case is that I have some agents in organisation structure. I want select for some agent (can by me) to see sum (amount of money) of all contracts that that agents subordinates (and subordinates of their subordinates and so on...) created with clients grouped by contract category.
Problem is that Redis Graph do not currently support all predicate. But I need to filter relations between agents because we have multiple "modules" with different organisation structures and I need report just from one module at the time.
My current Cypher query is:
MATCH path = (:agent {id: 482})<-[:supervised *]-(b:agent)
WHERE all(rel IN relationships(path) WHERE
rel.module_id = 1
AND rel.valid_from < '2020-05-29'
AND '2020-05-29' < rel.valid_to)
WITH b as mediators
MATCH (mediators)-[:mediated]->(c:contract)
RETURN
c.category as category,
count(c) as contract_count,
sum(c.sum) as sum
ORDER BY sum DESC, category
This query works in Neo4j.
I don't event know if this query is correctly written for the type of result that I want.
My boss would really like to use Redis Graph instead Neo4j because of performance reasons but I can't find any way to rewrite this query to be functional in the Redis graph. Is it even possible?
Edit 1: I was told that we will be using graph just for currently valid data and just for one module so I no longer need functional all predicate but I am still interested in answer.
The ALL function isn't supported at the moment, we do intend to add it in the near future, an awkward way of achieving the same effect as the ALL function would be a combination of UNWIND and count
MATCH path = (:agent {id: 482})<-[:supervised *]-(b:agent)
WITH b AS b, relationships(path) AS edges, size(relationships(path)) AS edge_count
UNWIND edges AS r
WITH b AS b, edge_count AS edge_count, r AS r
WHERE r.module_id = 1 AND r.valid_from < '2020-05-29' AND '2020-05-29' < r.valid_to
WITH b AS b, edge_count AS edge_count, count(r) AS filter_edge_count
WHERE edge_count = filter_edge_count
....

Infer order of insertion into BST

It seems several possible orders of insertion would create the BST in the image below.
E.g: F C S B E R D
What conditions regarding the order would ensure the same result please? I'm thinking as long as the entries for a given level are entered before those on the next level?

SiddhiQL Pattern and Window Query

I am trying to write a simple siddhi query which detects a pattern
eg: "Ice" "cream" "x" "y" "apple" "water"
where events Ice & cream both should be together and apple water should be together and x y are any random values in the window.length(6)
problem is the following query is not restricting the window.length(6) how can i achieve this?
from every (( s1=windowedStream[s1.val=='ice']-> s2= windowedStream[s2.val=='cream'] )
-> ( a1=windowedStream[a1.val=='apple'] -> a2 = windowedStream[a2.val =='water'] ))
select s1.meta_timestamp, s1.val
insert into filteredStream
As per the existent notations, Siddhi allows you to restrict a pattern based on a time window only. Please refer the following.
https://docs.wso2.com/display/CEP420/SiddhiQL+Guide+3.1#SiddhiQLGuide3.1-Pattern
As a workaround to restricting the patterns based on a length window, you may introduce a 3rd attribute called index to the "windowedStream", where index reflects the order of event arrival (i.e. index of the 1st event is 1, index of the 2nd event is 2 and so on). Then the following query would capture the patterns occurring within a length window of 6 events.
from every (( s1=tempStream[s1.val=='ice']-> s2= tempStream[s2.val=='cream'] )
-> ( a1=tempStream[a1.val=='apple'] -> a2 = tempStream[a2.val =='water' and a2.index- s1.index <= 6]))
select s1.meta_timestamp, s1.val
insert into filteredStream;
Hope this helps.

PostgreSQL: How to access column on anonymous record

I have a problem that I'm working on. Below is a simplified query to show the problem:
WITH the_table AS (
SELECT a, b
FROM (VALUES('data1', 2), ('data3', 4), ('data5', 6)) x (a, b)
), my_data AS (
SELECT 'data7' AS c, array_agg(ROW(a, b)) AS d
FROM the_table
)
SELECT c, d[array_upper(d, 1)]
FROM my_data
In the my data section, you'll notice that I'm creating an array from multiple rows, and the array is returned in one row with other data. This array needs to contain the information for both a and b, and keep two values linked together. What would seem to make sense would be to use an anonymous row or record (I want to avoid actually creating a composite type).
This all works well until I need to start pulling data back out. In the above instance, I need to access the last entry in the array, which is done easily by using array_upper, but then I need to access the value in what used to be the b column, which I cannot figure out how to do.
Essentially, right now the above query is returning:
"data7";"(data5,6)"
And I need to return
"data7";6
How can I do this?
NOTE: While in the above example I'm using text and integers as the types for my data, they are not the actual final types, but are rather used to simplify the example.
NOTE: This is using PostgreSQL 9.2
EDIT: For clarification, Something like SELECT 'data7', 6 is not what I'm after. Imagine that the_table is actually pulling from database tables and not the WITH statement the I put in for convenience, and I don't readily know what data is in the table.
In other words, I want to be able to do something like this:
SELECT c, (d[array_upper(d, 1)]).b
FROM my_data
And get this back:
"data7";6
Essentially, once I've put something into an anonymous record by using the row() function, how do I get it back out? How do I split up the 'data5' part and the 6 part so that they don't both return in one column?
For another example:
SELECT ROW('data5', 6)
makes 'data5' and 6 return in one column. How do I take that one column and break it back into the original two?
I hope that clarifies
If you can install the hstore extension:
with the_table as (
select a, b
from (values('data1', 2), ('data3', 4), ('data5', 6)) x (a, b)
), my_data as (
select 'data7' as c, array_agg(row(a, b)) as d
from the_table
)
select c, (avals(hstore(d[array_upper(d, 1)])))[2]
from my_data
;
c | avals
-------+-------
data7 | 6
This is just a very quick throw together around a similarish problem - not an answer to your question. This appears to be one direction towards identifying columns.
with x as (select 1 a, 2 b union all values (1,2),(1,2),(1,2))
select a from x;