Infer order of insertion into BST - binary-search-tree

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?

Related

Calculating the minimum of a rolling minimum grouped by another column value

I am writing a procedure to calculate a 6 month rolling minimum that also takes the lowest rolling minimum from each facility. The code below is what I've written so far, but the problem is that it tells me F.normalized_facility_id is a column that doesnt exist even though it very much does. Im thinking it has something to do with my joins and sub query, but I cannot understand how to make it recognize those column names. Please let me know if more info is needed to complete this query. I appreciate any feedback!
SELECT
min(F.rollmin) as 'minmin',
F.normalized_facility_id
FROM
(
SELECT SR.report_id
, SR.period_id
, Min(SR.benzene_concentration)
Over(order by F.normalized_facility_id
ROWS BETWEEN 11 preceding
and current ROW ) as rollmin
FROM PassiveBenzene.sampler_results SR
INNER
JOIN PassiveBenzene.report R
ON R.report_id = SR.report_id
INNER
JOIN PassiveBenzene.facility F
ON F.facility_id = R.facility_id
) F
GROUP BY F.normalized_facility_id
ORDER BY minmin

How to write relational algebra for SQL WHERE column IN?

select age from person where name in (select name from eats where pizza="mushroom")
I am not sure what to write for the "in". How should I solve this?
In this case the sub-select is equivalent to a join:
select age
from person p, eats e
where p.name = e.name and pizza='mushroom'
So you could translate it in:
πage (person p ⋈p.name=e.name (σpizza='mushroom'(eats e)))
Here's my guess. I'm assuming that set membership symbol is part of relational algebra
For base table r, C a column of both r & s, and x an unused name,
select ... from r where C in s
returns the same value as
select ... from r natural join (s) x
The use of in requires that s has one column. The in is true for a row of r exactly when its C equals the value in s. So the where keeps exactly the rows of r whose C equals the value in s. We assumed that s has column C, so the where keeps exactly the rows of r whose C equals the C of the row in r. Those are same rows that are returned by the natural join.
(For an expression like this where-in with C not a column of both r and s then this translation is not applicable. Similarly, the reverse translation is only applicable under certain conditions.)
How useful this particular translation is to you or whether you could simplify it or must complexify it depends on what variants of SQL & "relational algebra" you are using, what limitations you have on input expressions and other translation decisions you have made. If you use very straightforward and general translations then the output is more complex but obviously correct. If you translate using a lot of special case rules and ad hoc simplifications along the way then the output is simpler but the justification that the answer is correct is longer.

Get distance and duration to closest matrix in SQL

I have an logic to find the most optimised to perform delivery.
Lets say, I have location A,B,C. So need distance and duration from A to B, B to A, A to C, C to A, B to C and C to B.
I know how to come out with above query. Example result would be NewMatrix in fiddle.
http://sqlfiddle.com/#!6/9cce7/1
I have a table where I store current matrix we have based on past deliveries. (AppMatrix in table above)
So I need to lookup distance and duration in this table, to find closest matching origin and destination. I have created following function which works just perfect to get my answer :
SELECT TOP 1 Distance, ([Time]/60) as Duration FROM [AppMatrix]
ORDER BY ABS([OriginSiteLat] - #OriginLat) + ABS([OriginSiteLng] - #OriginLong)
,ABS([DestSiteLat] - #DestinationLat) + ABS([DestSiteLng] - #DestinationLong)
The problem is slowness. Since I need to perform these call with each matrix (I can have 700 different deliveries in a day, 700*700 = 14000, this just too slow - it takes few hours to return result)
I'm working on best how to limit the data, but any advise on how to optimize performance is appreciated. Maybe advice on how to use spatial here would help.
This is my current code :
SELECT * FROM CT as A
INNER JOIN CT AS B ON A.Latitude <> B.Latitude AND A.Longitude<>B.Longitude
CROSS APPLY [dbo].[ufn_ClosestLocation](A.Latitude,A.Longitude, B.Latitude, B.Longitude) R

Getting ordered subset of records

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

How do I return a value of an entity in a table that is less than but closest to the value in another table for each element in the last table in SQL?

I have two tables in MS Access and I am trying to add a field for one of those tables that tells which record from another table has a value that is less than the first field's value, but comes the closest? I have this query so far (just a select statement to test output and not alter existing tables), but it lists all values that are less than the querying value:
SELECT JavaClassFileList.ClassFile, ModuleList.Module
FROM JavaClassFileList, ModuleList
WHERE ModuleList.Order<JavaClassFileList.Order;`
I tried using things likeSELECT JavaClassFileList.Classfile, MAX(ModuleList.Module), which will only display the maximum module but combined it with the select statement above, but it would say that it would only return one record.
Output desired: I have some records, a, b, and c, I shall call them, each storing various information, while a is storing a value of 732 in a column, and b is storing a value of 731 in the same column. c is storing a value of 720. In another table, d is storing a value of 730 and e is storing a value of 718. I want the output like this (they are ordered largest to smallest):
a 732 d 730
b 731 d 730
c 720 e 718
There can be duplicates on the right, but no duplicates on the left. How can I get this result?
I would approach this type of query using a correlated subquery. I think the following words in Access:
SELECT jc.ClassFile,
(select top 1 ml.Module
from ModuleList as ml
where ml.[Order] < jc.[Order]
)
FROM JavaClassFileList as jc;
I'm assuming Order is unique for Module. If it isn't, JavaClassFileRecords may show up multiple times in the resultset.
If no module can be found for a JavaClassFile then it will not show up in the results. If you do want it to show up in cases like that (with a null module), replace INNER JOIN with LEFT OUTER JOIN.
SELECT j.ClassFile, m.Module
FROM JavaClassFileList j
INNER JOIN ModuleList m
ON m.Order =
(SELECT MAX(Order)
FROM ModuleList
WHERE Order < j.Order)