use aggregates (min, max, avg) in CONSTRUCT query [duplicate] - aggregate-functions

I mostly use SPARQL SELECT while working on a query for debugging purposes but in the end I want to use the final result it in a CONSTRUCT way; as I want to work with a graph and not key/value query results.
What I don't get yet (and can't seem to find with search engines/docs) is if I can use functions as well that way. As an example, I use a property path to concatenate titles I get into a "superstring", which I later use for building a Lucene index to increase plain text search quality:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT (group_concat(?title ; separator = " ") AS ?fancytitle) WHERE {
GRAPH ?graph {
<http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> dc:relation+ ?relation .
?relation dc:title ?title .
}
}
Now I would like to have the same ?fancytitleas a new triple like
<http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> <fancytitle> ?fancytitle .
So I can store it directly in a new graph. Is this possible? I played with some queries but couldn't manage to get it accepted by the SPARQL processor. FYI I'm using Fuseki.
You can try it out at my SPARQL Endpoint

Yes this is possible
You can't use expressions directly in a CONSTRUCT template but you can assign the variable in the WHERE clause either via a SELECT expression in a sub-query or using BIND.
In your case as GROUP_CONCAT is an aggregate it can only be a SELECT expression so you just need to put your entire SELECT as a sub-query e.g.
PREFIX dc: <http://purl.org/dc/elements/1.1/>
CONSTRUCT
{
<http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> <http://fancyTitle> ?fancytitle
}
WHERE
{
SELECT (group_concat(?title ; separator = " ") AS ?fancytitle) WHERE {
GRAPH ?graph {
<http://data.staatsarchiv-bs.ch/id/archivalresource/CH-000027-1/pa-633c-a-312-fasc-163> dc:relation+ ?relation .
?relation dc:title ?title .
}
}
}
The above works fine on your endpoint

With the help of my colleague we got it to work, UNION and GROUP BY are essential. This query puts the string together for all locah:ArchivalResource in the graphs:
CONSTRUCT
{
?archresource skos:hiddenLabel ?supertitle
}
WHERE
{
SELECT ?archresource (group_concat(?title ; separator = ", ") AS ?supertitle) WHERE {
GRAPH ?graph {
{
SELECT ?title ?archresource WHERE {
GRAPH ?graph {
{
?archresource a locah:ArchivalResource ;
dc:title ?title .
} UNION
{
?archresource dc:relation+ ?relation .
?relation dc:title ?title .
}
}
}
}
}
} GROUP BY ?archresource
}

Related

Sort document by integer Firestore Kotlin

I want to sort documents by img in descending order.
I've tried to add indexes in firebase console. Here is a screenshot from indexes tab:
Here is my database structure:
Here is my code:
db.collection("images").whereEqualTo("user", uid).orderBy("img", Query.Direction.DESCENDING).get()
.addOnSuccessListener {
if (it.isEmpty) {
Log.i("Image", "Sorry, no image")
} else {
for (task in it) {
Log.i("Image", task.get("img").toString())
}
}
}
And here is log:
I/Image: 2
I/Image: 1
I/Image: 3
I/Image: 4
I/Image: 6
I/Image: 7
What am I doing wrong?
Notice that as stated on the documentation there could be two explanations behind the behavior you are experiencing:
If you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field
You cannot order your query by any field included in an equality (=) or in clause.
Based on the following snippet provided on the documentation:
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2)
You could try the following (but I'm not sure if it'll work because of 2.):
db.collection("images").whereEqualTo("user", uid).orderBy("user").orderBy("img", Query.Direction.DESCENDING).get()
.addOnSuccessListener {
if (it.isEmpty) {
Log.i("Image", "Sorry, no image")
} else {
for (task in it) {
Log.i("Image", task.get("img").toString())
}
}
}

Laravel collection condition in sum

I have a collection of transactions with relations and I would like to sum column separated by condition of relation column. Right now I have this:
$delegatedProvision = 0;
$ownProvision = 0;
foreach ($transactions as $transaction) {
if ($transaction->discount->consider_improvement) {
$delegatedProvision += $transaction->stats->$column;
continue;
}
$ownProvision += $transaction->stats->$column;
}
$this->salesCollection->put('delegatedProvision', $delegatedProvision);
$this->salesCollection->put('ownProvision', $ownProvision);
It works but I would like to use Laravel collections. So far I have just this:
$provision = $transactions->sum(function ($transaction) use ($column) {
return $transaction->stats->$column;
});
And I don't know how to use condition in sum() method and according column $transaction->discount->consider_improvement (which is boolean) have sum in separated variables. I can use filter each for different consider_improvement but it means that I have to iterate all transactions twice.
Try this:
$collection->where(/* your condition */)->sum($column);

Phonegap - Assemble with a query result from another query - Sqlite Query

I can not make a selection via the result of the first
Goal is:
Do the query on the table "line" pick up your ID and search customers that line the "customer" table
This is my code:
db = window.openDatabase("test", "1.0", "Test DB", 1000000);
db.transaction(SelectData,erroSelect,sucessSelect);
function SelectData(tx)
{
tx.executeSql("select id from linha",[],function(tx, response)
{
for(i=0; i<response.rows.length; i++)
{
tx.executeSql("SELECT * FROM customers WHERE line= ?", [response.rows.item(i).id],
function (tx, results)
{
for(r=0; r<results.rows.length; r++)
{
alert(results.rows.item(r).nome); //never worked
}
}
}
},SelectError);
}
It is diffcult to understand from your post, where is the actual error.
Have you tried using console.log() call inside each tx.executeSql() to ascertain that the function is being executed.
Alternatively you can use a single query instead of using two SELECT statements.
Replace
select id from linha
with
SELECT customers.columnName1, customers.columnName2
FROM customers INNER JOIN linha ON customers.line = linha.id

Using jsonPath looking for a string

I'm trying to use jsonPath and the pick function to determine if a rule needs to run or not based on the current domain. A simplified version of what I'm doing is here:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant=='Telefora')]");
}
emit
<|
console.log(merchantData);
|>
}
The console output I expect is the telefora object, instead I get all three objects from the json file.
If instead of merchant=='Telefora' I use merchantID==16 then it works great. I thought jsonPath could do matches to strings as well. Although the example above isn't searching against the merchantDomain part of the json, I'm experiencing the same problem with that.
Your problem comes from the fact that, as stated in the documentation, the string equality operators are eq, neq, and like. == is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of the eq string equality operator.
Simply swap == for eq in you JSONpath filter expression and you will be good to go:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant eq 'Telefora')]"); // replace == with eq
}
emit
<|
console.log(merchantData);
|>
}
I put this to the test in my own test ruleset, the source for which is below:
ruleset a369x175 {
meta {
name "test-json-filtering"
description <<
>>
author "AKO"
logging on
}
dispatch {
domain "exampley.com"
}
global {
dataset merchant_dataset <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule filter_some_delicous_json {
select when pageview "exampley.com"
pre {
merchant_data = merchant_dataset.pick("$.merchants[?(#.merchant eq 'Telefora')]");
}
{
emit <|
try { console.log(merchant_data); } catch(e) { }
|>;
}
}
}

Filtering on linked table in Axapta/Dynamics Ax

I have a form in Axapta/Dynamics Ax (EmplTable) which has two data sources (EmplTable and HRMVirtualNetworkTable) where the second data source (HRMVirtualNetworkTable) is linked to the first on with "Delayed" link type.
Is there a way to set an filter on the records, based on the second data source, without having to change the link type to "InnerJoin"?
You could use "Outer join" instead of "Delayed" then change the join mode programmaticly when there is search for fields on HRMVirtualNetworkTable.
Add this method to class SysQuery:
static void updateJoinMode(QueryBuildDataSource qds)
{
Counter r;
if (qds)
{
qds.joinMode(JoinMode::OuterJoin);
for (r = 1; r <= qds.rangeCount(); r++)
{
if (qds.range(r).value() && qds.range(r).status() == RangeStatus::Open)
{
qds.joinMode(JoinMode::InnerJoin);
break;
}
}
}
}
In the executeQuery() on the EmplTable datasource:
public void executeQuery()
{;
SysQuery::updateJoinMode(this.queryRun() ? this.queryRun().query().dataSourceTable(tableNum(HRMVirtualNetworkTable)) : this.query().dataSourceTable(tableNum(HRMVirtualNetworkTable)));
super();
}
Sometimes this.queryRun() return null so use this.query() instead.
Update:
Note that the above is not relevant for AX 2012 and later, where you can use query filters in outer joins. See How to Use the QueryFilter Class with Outer Joins.
You can do it programmaticaly by joining QueryBuildDataSource or by extended filter (Alt+F3, Right click on datasorce, 1:n and find sev\condary DS)

Resources