I have two sparql querys , simplifying my problem :
query 1:
select ?letter ?number
-> results ({a1}{b2}{d4})
....
query 2:
select ?anotherletter ?anotherNumber
-> results ({b2}{c3})
How can i join the 2 querys to obtain the sum of the second column, maybe same values of a one lista couldnt be in the second.
The result of the query has to be
{a,1}{b,4}{c,3}{d,4}
Is there a propper way to do that?
The solution that gives #UninformedUser worked:
select ?letter (sum(?number) as ?cnt) { {QUERY1} UNION {QUERY2} } group by ?letter
and yes, the variables should be the same in both UNION parts. –
UninformedUser
Related
I have a requirement to calculate a number value by using the results of two sql queries. I know this is possible in python etc but if I want to use the results of sql queries in say, apache superset, how can I achieve this?
For ex:
query1: select * from consumer;
result: 190 rows
query2: select * from producer;
result: 230 rows
value I need, called output : 190/230.
Is this possible using sub queries? How can I extract the output alone?
Welcome to everyone.
I would be very thanks if anyone help me. I am not too expert writing mdx query.
I am working in a software that build a mdx query to get info from a OLAP structure.
I get the result when I filter by a dimension, but I need to filter by many.
my structure is
CodGer : A, B, C, D
CodModel : A01, A02, A03, B01, B02,B03 ...
With this query I get all element from A1, and that is right.
Select {[Measures].allmembers} on columns
, non empty {
[ModelosGers].[CodGer].[A] *
{[ModelosGers].[CodModel].[A01] } } on rows
From [Tasas]
where {[Periodos].[Periodos].[Periodos].[260]}
But, in fact, I also need, for example, get all element from A and B01.
In sql could be like
Select * from Tasas where [Periodos].[Periodos].[Periodos]=260 and( [ModelosGers].[CodGer] like 'A' or [ModelosGers].[CodModel]='B01')
I have been research filters and where clause MDX but I do obtain the right query.
Can anyone help me?
Thanks
Finally I resolved my selft traying differents posts like
And-Or in MDX queries
Select {[Measures].allmembers} on columns
, non empty {
{([ModelosGers].[CodGer].[A] ,[ModelosGers].[CodModelo].defaultmember ) ,
([ModelosGers].[CodGer].defaultmember ,[ModelosGers].[CodModelo].[B01] ) }
} on rows
From [Tasas] where {[Periodos].[Periodos].[Periodos].[260]}
I have read answers here but I am still not quite sure how I would do this regarding two columns of a table and more than one result per query.
So, the first query would look like this in my Node app:
select stype, lid from Profiles where lid_P=${profile.lid} and stype_P='${profile.stype}';
// result can consist of 1-50 objects
I need the result for the following query:
select * from an where stype=stype and lid=lid;
// where lid and stype are results from the other query
I only need the results of the second query but I fail to implement only one query doing both. Can someone help me out? Any help is appreciated.
Thank you!
You seem to want exists:
select *
from an a
where exists(
select 1
from profiles p
where
p.stype = a.stype and p.lid = s.lid
and p.lid_p = #profile_lid and p.style_p = #profile_stype
)
#profile_lid and #profile_stype are the parameters to the query - that I would recommend using instead of concatenating variables in the query string.
Did you try
select *
from an
where (stype, lid) in (select distinct ... <your first query>)
Context: 14M triples, Blazegraph workbench. I'm currently attempting to design queries which combine SELECT and ASK. More exactly, I want to select results in my graph where an assumption is true.
For my example, imagine I've many books which have one author and one editor. I want to select the book from the author which his book is linked through random path length property to the client#1.
In my case, with my data, it takes a lot of time to realise the query directly like that:
SELECT ?id_book
WHERE {?id_book prefix:hasAuthor :author#1.
?id_book prefix:linkedToEditor*/prefix:hasClient :client#1}
ORDER by ?id_book
To reduce the time of calculus (x 1:1000), I'm using a script to realise these queries successively. The script selects the books which have as author the author n°1:
SELECT ?id_book
WHERE {?id_book prefix:hasAuthor :author#1}
ORDER by ?id_book
And I ask for each result for 1 to n (id_book#1, id_book#2, ..., id_book#n) if it's linked to client n°1:
ASK {id_book#i prefix:linkedToEditor*/prefix:hasClient :client#1}
The SELECT query followed by the ASK query is far faster than the first SELECT query for the same results. I don't want to explore all the possibilities of ?id_book prefix:linkedToEditor*/prefix:hasClient :client#1; I just want to save results where the link exists. I tried with FILTER EXISTS or two SELECT queries, but the query times are similarly long:
SELECT ?id_book
WHERE {?id_book prefix:hasAuthor :author#1.}
FILTER EXIST {?id_book prefix:linkedToEditor*/prefix:hasClient :client#1}
ORDER by ?id_book
or
SELECT ?id_book
WHERE {?id_book prefix:linkedToEditor*/prefix:hasClient :client#1.
{SELECT ?id_book
WHERE {?id_book prefix:hasAuthor :author#1.}
}
}
How can I optimise my queries into one query?
It's a bit surprising that there's such a difference in your query times; a SPARQL engine should probably be able optimize the query to perform the simple part first, and then do the more complicated query property path afterward. The ordering could also cause some time increase, and it's really not important if you're just interested in boolean results.
At any rate, since nested queries are performed innermost first, you can sort of force the "do this first, then do that" by nesting the queries like this:
select ?id_book {
#-- first, get the books by author one
{ select ?id_book { ?id_book prefix:hasAuthor :author#1 } }
#-- then, then check that the book is related to client one
?id_book prefix:linkedToEditor*/prefix:hasClient :client#1
}
order by ?id_book
I'm not getting proper result from this query.
SELECT Qty as op,
0 as secunit
FROM tbl_master
WHERE tb_sno = 1
UNION
SELECT main_qty as op,
main_unit as secunit
FROM purchase
WHERE tb_sno = 1
AND TRN_DATE < #2011/05/14#
AND trn_sno2 <> 0
This show less one record from actual. is there any way to get actual result
Have you tried using UNION ALL? Perhaps there is a duplicate record in one of those recordsets. (making my comment an answer)
It looks like both of your queries set the exact same criteria on the tb_sno field (WHERE tb_sno = 1). Because of that, if op and sec_unit have the same resulting values in each query, a UNION will eliminate one of the duplicate rows. A UNION ALL will keep all duplicates, as fortheworld mentioned.