3

I have a SPARQL query which returns result with a LIMIT of 20.. In this query I also want to know the total number of results without running the query two times (one with LIMIT and one without LIMIT)..

For Example-- On running a query total possible results are 500 with LIMIT it displays only 20 at a time, but in my response I want a field which displays total result count, i.e., 500 ...

Updated question

Suppose

enter image description here

Now if i do a query where sequence = abc_11 with LIMIT=2 I will get something like

enter image description here

which is fine, with addition to this output what i want is

enter image description here

where totalMatchedResult is 5 because query actually matched 5 results but returned only 2 because of our LIMIT=2

TallTed
  • 9,069
  • 2
  • 22
  • 37
fasal shah
  • 156
  • 1
  • 1
  • 8
  • what would be the returned format? I mean, a SELECT query are always "rows", how should this look like? – UninformedUser Nov 06 '19 at 06:53
  • 1
    `select * { { select * { GRAPH_PATTERN_HERE } limit 20 } { select (count(*) as ?cnt) { GRAPH_PATTERN_HERE } } }` - will return the total number in each row – UninformedUser Nov 06 '19 at 06:55
  • if you reuse the variable of the LIMIT subquery, you can add the total number to the original resultset. For example`select * { { select ?s { GRAPH_PATTERN_HERE } limit 20 } UNION { select (count(*) as ?s) { GRAPH_PATTERN_HERE } } } order by ?s` - and the total number is most likely the last element (as long as your returned entities aren't literals) – UninformedUser Nov 06 '19 at 06:57
  • @AKSW let me rephrase, as we know LIMIT only returns 'n' number of results but what i want is a field which will mention total count of results that query has matched.. something like this where total result is 500 but query returns only 20 which is the LIMIT. `{ **"totalResult": 500**, "sequences": [ { "Id": "3333", "desc":"....", }, .... Sequence having only 20 results as LIMIT is 20 ] }` – fasal shah Nov 06 '19 at 07:42
  • no, I gave you both option possible with SPARQL. You can't create your custom JSON format – UninformedUser Nov 06 '19 at 07:53
  • @AKSW thanks for response, please have a re-look at question I have updated it.. JSON format was just to give you an idea what i want not that i want a customized json.. anyway i guess after updating my question it should be clear enough.. – fasal shah Nov 06 '19 at 09:06
  • So, what's wrong with my first query suggestion? Ok, it writes the total number into each line, but who cares? You have to process the resultset anyways in the client, just take the total number value once from the resultset then – UninformedUser Nov 06 '19 at 09:33
  • @AKSW thanks alot..There is nothing wrong with your first query.. I was just making sure we are on the same page and question is clear for anyone who checks in future... Said that there is one more small change considering your first query now i had to add group by `select * { { select * { GRAPH_PATTERN} limit 20 } { select (count(*) as ?cnt) { } } }` in this case results now reduced to 300 from 500, with query you suggested it will still return 500 which is absolutely correct considering initial question. Considering groupby case can you please update the answer – fasal shah Nov 06 '19 at 10:46
  • not sure if I understand, but shouldn't you just do `{ select (count(distinct ) as ?cnt)` then in the second subquery? This should return the total number of groups according to your query – UninformedUser Nov 06 '19 at 11:44
  • @fasal shah, Did you find any solution to this problem without repeating the same where clause? – harish chava Jul 16 '20 at 15:12
  • @harishchava, unfortunately, I don't have that code with me but as far I remember I got it by using UNION of two queries ```SELECT * { {SELECT (COUNT(*) as ?total) { GRAPH PATTERN}} UNION { orderby desc(count)}} and outside you put LIMIT 5``` i dont remember exactly but something like this.. Not sure if this will help... :( – fasal shah Jul 22 '20 at 16:02

0 Answers0