oData NOT In with subquery equivalent - odata

Is it possible to reproduce a query such as the following using an oData filter?
SELECT
st.*
FROM
SomeTable st
WHERE
st.ID NOT IN
(SELECT LinkedID FROM SomeTable)
This isnt quite my real world example, but essentially what I'm trying to achieve in to get all the results that aren't returned in sub-query, but being able to do the above would achieve what I need, if its possible.

Related

Subtract two queries results

Cannot find per the docs, and there is an old issue related to this.
It is possible to subtract the results of 2 queries?
Something like this in SQL:
(SELECT COUNT(*) FROM test - (SELECT COUNT(*) FROM test WHERE …)
As far as I know, currently it is not possible (at least not with InfluxQL).
It should, however, be possible with their new query language called Flux (as of InfluxDB1.7)
https://docs.influxdata.com/flux/v0.24/introduction/getting-started/

Understanding Active record Queries

I needed to do some complex subquery for a project i am working on. I could't figure out how to do it in plain active record. I tried several things and ended up opting for generic SQL query using
ActiveRecord::Base.connection.exec_query(
"
SELECT gid as id, (dp).path, (dp).geom, ST_AsText((dp).geom), ST_X((dp).geom) as longitude, ST_Y((dp).geom) as latitude
FROM (
SELECT gid, ST_DumpPoints(ST_AsText(ST_Transform(geom, 4326))) as dp
FROM apples
WHERE gid in (#{#object.id})
) AS GEO;
"
)
Here i am selecting from a subquery and transforming geometry to Points using plain postgresql. If i were to do it in ActiveRecord queries, how do i do it? Is this type of query even possible in ActiveRecord?
ActiveRecord does not provide you with anything fancy. You can perform simple stuff, but when there's a need for a complex query you just go with plain SQL as you did.
You can check Arel which might give you more flexibility. But still, in the end, I think, it's not worth - it won't be as effective as a native query.
Do simple stuff with ORM. Do complex stuff with plain SQL.

Dynamics CRM OData query filtering on expanded attributes only works if no results come out?

I have a requirement to fetch Price List Item records which adhere to the following requirements:
Filter by a specific PriceList
Filter by a specific currency
Filter by the Name of the related Product containing a given string
I got the first two points working no problem, but it feels like expanding doesn't cope well with filtering. I started from a "straight" query on Product entity:
.../ProductSet?$filter=substringof('sometext', Name)
Equivalent SQL (targeting the corresponding CRM filtered views for clarity):
SELECT * FROM FilteredProduct WHERE ProductNumber LIKE '%sometext%'
The above query works, I can tweak it and have no issues. However, if I attempt to move on to ProductPriceLevel (thus expanding the relationship with Product, which is product_price_levels) I end up with this:
.../ProductPriceLevelSet?$expand=product_price_levels&$filter=substringof('sometext', product_price_levels/Name)
Equivalent SQL (again, targeting the relevant filtered views):
SELECT * FROM FilteredProductPriceLevel PPL JOIN FilteredProduct P
ON PPL.ProductId = P.ProductId WHERE P.ProductNumber LIKE '%sometext%'
Which has two different outcomes I see:
If the $filter has no matches, it works fine and returns an empty result set
If the $filter matches something, I get an error
code: -2147220970
message: The result selector of the 'Join' operation must return an anonymous type of two properties.
AFAIK that's what happens when you hit a limitation of LINQ-to-CRM regarding using .Where() on multiple entities at once... doesn't seem relevant!
What's wrong with my query ?
NOTE: The CRM 2013 I'm using is On-Premise, without any update rollup / service pack.
ALSO NOTE: The equivalent SQL, as can be expected, works perfectly
I don't think CRM OData supports adding a filter on a joined entity. Try reversing the entity you're actually starting with, and add a path to the referencing entity:
ProductSet()?$filter=substringof('sometext',ProductNumber)&$expand=product_price_levels&$select=product_price_levels/*
P.S. If you have linqPad, this is the query I used to generate this:
from p in ProductSet
where p.ProductNumber.Contains("sometext")
select new { p.product_price_levels }

How to paginate query results with cypher?

Is it possible to have cypher query paginated. For instance, a list of products, but I don't want to display/retrieve/cache all the results as i can have a lot of results.
I'm looking for something similar to the offset / limit in SQL.
Is cypher skip + limit + orderby a good option ? http://docs.neo4j.org/chunked/stable/query-skip.html
SKIP and LIMIT combined is indeed the way to go. Using ORDER BY inevitably makes cypher scan every node that is relevant to your query. Same thing for using a WHERE clause. Performance should not be that bad though.
Its like normal sql, the syntax is as follow
match (user:USER_PROFILE)-[USAGE]->uUsage
where HAS(uUsage.impressionsPerHour) AND (uUsage.impressionsPerHour > 100)
ORDER BY user.hashID
SKIP 10
LIMIT 10;
This syntax suit to last version (2.x)
Neo4j apparently uses "indexed-backed order by" nowadays, which means if you are using alphabetical ORDERBY on indexed node properties within your SKIP/LIMIT query, then Neo4j will not perform a full scan of all "relevant nodes" as other have mentioned (their responses were long ago, so keep that in mind). The index will allow Neo4j to optimize on the basis that it already stores indexed properties in ORDERBY order (alphabetical), such that your pagination will be even faster than without the index.

Map a Rails Custom SQL Query to an ActiveRecord Model

I'm setting up a custom query that uses a range of OR statements in conjunction with BETWEEN statements and a final GROUP BY id HAVING COUNT(*) >= #{tolerance}. Not to mention INNER and LEFT join operations.
I would assume that it would not be possible to setup using active record. So I used the Model.connection.select_all() command to fire a query. This works, but how do I not map all of the rows to that specific model?
Rails is pretty powerful especially if you are using Rails 3 & ARel. So I wouldn't be surprised if you actually could write your query using rails.
However, there will always be times when writing raw SQL is desired.
To do that, instead of Model.connection use Model.find_by_sql(QUERY_STRING).
This way the query will get parsed for you automatically just make sure you only select "model.*"

Resources