Query Drupal 8 content types with expression - drupal-8

In short, I need to do an entity query while running an expression. I can't find any way to accomplish this. I'm assuming there should be two ways.
An entity query with an expression - When I try this I can't get any expressions to work
a raw DB query, but I'm not familiar with how to join all the fields related to the content type of nodes I need.
Here is a shorthand example of what I need to accomplish
X = content type
y = field on x content type
z = field on x content type
My expression below is just an example, but need to run this in the database query
- Select y and z from x
- if x > y return node id
Any help would be great. This is going to run on a very large dataset, trying to find the fastest way to do the query in the database.

Comparing two fields is not possible using the entity query.
To do this, you may need to use the more low level Dynamic Queries and its where method:
$query = \Drupal::database()->select('node_field_data', 'n');
$query->condition('n.type', 'x'); // to get content type x
$query->innerJoin('node__field_y', 'y', 'y.entity_id = n.nid'); // join with field y
$query->innerJoin('node__field_z', 'z', 'z.entity_id = n.nid'); // join with field z
$query->where('z.field_z_value > y.field_y_value'); // your condition
$query->addField('n', 'nid'); // get node id in result
$results = $query->execute()->fetchAllKeyed(0, 0);

Related

UPDATE with JOIN not SELECTing expected value

I'm trying to UPDATE a temporary transaction table with values from a holdings table. The fields I want to get are from the holding with the lowest date that is higher than the transaction date.
When I use below SELECT statement, the right values are shown:
SELECT h.*
FROM transaction_tmp tt
JOIN holdings h
ON tt.isin = h.isin
AND tt.portfolio = h.portfolio
WHERE h.start_date > tt.tr_date
ORDER BY h.start_date
LIMIT 1
However, when I use below UPDATE statement, incorrect values are selected/updated in transaction_tmp:
UPDATE transaction_tmp tt
JOIN holdings h
ON tt.isin = h.isin
AND tt.portfolio = h.portfolio
SET
tt.next_id = h.id,
tt.next_start_date = h.start_date
WHERE h.start_date > tt.tr_date
ORDER BY h.start_date
LIMIT 1
I'm thinking the WHERE statement is not working appropriately, but unfortunately I cannot figure out how to fix it.
Appreciate any help here!
-Joost
should work using a subquery
UPDATE transaction_tmp tt
JOIN (
SELECT h.*
FROM transaction_tmp tt
JOIN holdings h
ON tt.isin = h.isin
AND tt.portfolio = h.portfolio
WHERE h.start_date > tt.tr_date
ORDER BY h.start_date
LIMIT 1
) tx on ON tt.isin = tx.isin
AND tt.portfolio = tx.portfolio
SET
tt.next_id = tx.id,
tt.next_start_date = tx.start_date
I'm surprised your syntax works. The MySQL documentation is pretty clear that LIMIT and ORDER BY are only allowed when there is a single table reference:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET assignment_list
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
They are not allowed for the multiple table version of UPDATE:
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET assignment_list
[WHERE where_condition]
. . .
For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. Each matching row is updated once, even if it matches the conditions multiple times. For multiple-table syntax, ORDER BY and LIMIT cannot be used.
I get an error if I try such syntax.

Ignite SqlFieldsQuery specific keys

Using the ignite C++ API, I'm trying to find a way to perform an SqlFieldsQuery to select a specific field, but would like to do this for a set of keys.
One way to do this, is to do the SqlFieldsQuery like this,
SqlFieldsQuery("select field from Table where _key in (" + keys_string + ")")
where the keys_string is the list of the keys as a comma separated string.
Unfortunately, this takes a very long time compared to just doing cache.GetAll(keys) for the set of keys, keys.
Is there an alternative, faster way of getting a specific field for a set of keys from an ignite cache?
EDIT:
After reading the answers, I tried changing the query to:
auto query = SqlFieldsQuery("select field from Table t join table(_key bigint = ?) i on t._key = i._key")
I then add the arguments from my set of keys like this:
for(const auto& key: keys) query.AddArgument(key);
but when running the query, I get the error:
Failed to bind parameter [idx=2, obj=159957, stmt=prep0: select field from Table t join table(_key bigint = ?) i on t._key = i._key {1: 159956}]
Clearly, this doesn't work because there is only one '?'.
So I then tried to pass a vector<int64_t> of the keys, but I got an error which basically says that std::vector<int64_t> did not specialize the ignite BinaryType. So I did this as defined here. When calling e.g.
writer.WriteInt64Array("data", data.data(), data.size())
I gave the field a arbitrary name "data". This then results in the error:
Failed to run map query remotely.
Unfortunately, the C++ API is neither well documented, nor complete, so I'm wondering if I'm missing something or that the API does not allow for passing an array as argument to the SqlFieldsQuery.
Query that uses IN clause doesn't always use indexes properly. The workaround for this is described here: https://apacheignite.readme.io/docs/sql-performance-and-debugging#sql-performance-and-usability-considerations
Also if you have an option to to GetAll instead and lookup by key directly, then you should use it. It will likely be more effective anyway.
Query with operator "IN" will not always use indexes. As a workaround, you can rewrite the query in the following way:
select field from Table t join table(id bigint = ?) i on t.id = i.id
and then invoke it like:
new SqlFieldsQuery(
"select field from Table t join table(id bigint = ?) i on t.id = i.id")
.setArgs(new Object[]{ new Integer[] {2, 3, 4} }))

Power Query - Count if custom function

Following on from this question.
I'd like to create a custom function where I pass in a table, a column and value and it counts all the rows where those conditions are met.
So far I have
let
fCountif = (tbl as table, col as text, value as any) as number =>
let
select_rows = Table.SelectRows(tbl, each [col] = value),
count_rows = Table.RowCount(select_rows)
in
count_rows
in
fCountif
But am running into all sorts of issues. The first is invoking the function to test it, when I pass in a string for the Table name it sends a literal string to the function instead of converting it to table. This is probably only an issue when invoking it through the query editor button and should resolve itself when using the function throughout the rest of my M code.
The second is how to pass the col value into the [col] part of the function. I have seen fat rockets used in other examples ie [column name] <= col but just can't get it to work.
I'm trying hard to get my head around functions and was comfortable doing this in vba so if you have any tips please shout out. Documentation on this is hard to come by. Cheers.
Tables can be thought of as a list of records, where each record is a row. In the functions for Table.AddColumn and Table.SelectRows, [col] is a shorter way of writing Record.Field(_, "col"), which accesses the "col" field of the record.
Here is one way to write your function:
let
fCountif = (tbl as table, col as text, value as any) as number =>
let
select_rows = Table.SelectRows(tbl, each Record.Field(_, col) = value),
count_rows = Table.RowCount(select_rows)
in
count_rows
in
fCountif
This should work:
let
fCountif = (tbl as table, col, value) =>
let
select_rows = Table.SelectRows(tbl, Expression.Evaluate("each "&Text.Replace("["&col&"] = *"&value&"*","*",""""))),
count_rows = Table.RowCount(select_rows)
in count_rows
in fCountif

A simple order_by() query

I have a table with columns, a, b, c, d, e.
Each column is given a random numerical value.
I can then order by that numerical value.
query_results = UserProfile.objects.all().order_by('a')
What i want is the ability to order by the sum of 2 or 3 or even 5 columns. But i cant figure out how to do it lightly.
My answer so far has been some what convoluted. I add an extra column x.
cursor.execute("UPDATE mysite_userprofile SET x = a+b;")
I then order by x. I have to update the column x each time i want different columns added together.
This is not the way to do it i know. i know order_by('a+b') doesn't work but is their another solution just as simple. I looked on django's site but couldn't find anything
The best way is to use extra
UserProfile.objects.extra(
select={'sum_of_fields': 'a + b + c + d + e'},
order_by=('sum_of_fields',)
)
I think you can order the result in the SQL query. Before you run the SQL query, when you select the columns, you can build the query string in the background.

How to write a DQL select statement to search some, but not all the entities in a single table inheritance table

So I have 3 entities within one table. I need to be able to search 2 out of the 3 entities in one select statement, but I'm not sure how to do this.
Use the INSTANCE OF operator in your dql query like this (where User is your base class):
$em->createQuery('
SELECT u
FROM Entity\User u
WHERE (u INSTANCE OF Entity\Manager OR u INSTANCE OF Entity\Customer)
');
Doctrine translates this in the sql query in a WHERE user.type = '...' condition.
See here for more details on the dql query syntax.
The answer for multiple instances actually doesn't work. You would have to do something like this to check for multiple instances.
$classes = ['Entity\Manager', 'Entity\Customer'];
$qb = $this->createQueryBuilder('u');
->where('u.id > 10') //an arbitrary condition, to show it can be combined with multiple instances tests
->andWhere("u INSTANCE OF ('" . implode("','", $classes) . "')");
As commented by flu, if you want to retrieve some entities from different instances with a QueryBuilder instead of a DQL query, you can use an array as parameter:
$qb = $this->createQueryBuilder('u');
->where('u.id > 10') //an arbitrary condition, to show it can be combined with multiple instances tests
->andWhere('u INSTANCE OF :classes')
->setParameter('classes', ['Entity\Manager', 'Entity\Customer'])
;