I need to count elements that was in result of SQL query:
db._query('FOR v in visitors FILTER v.ip == "127.0.0.1" return COUNT(v.guid) ')
This request is return my a length of every GUID, but I need to get total number of GUIDs for example: 2.
You need to use the result of the query as input for the COUNT function, and then RETURN this result.
You can replace the RETURN value of the actual query by 1 for performance reasons:
RETURN COUNT(FOR v IN visitors FILTER v.ip == "127.0.0.1" RETURN 1)
Version from 2022!
FOR m IN messages
FILTER DATE_HOUR(m.date) == 3
COLLECT WITH COUNT INTO length
RETURN length
Related
Here is an example:
`
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
`
What i need to do is if bracketSequenceNumber holds the same value trow an exception.
Number of elements is N there can be more than 3. How can i achieve this using xquery.
I tried something like this without success and i cant say i understand xQuery completley:
`
let $count := ( for $bracketSequenceNumber in $bracketQualifier/bracketSequenceNumber return count(bracketQualifier[#bracketSequenceNumber = $bracketSequenceNumber ])) return
if($GDSN_PriceSyncPriceSegmentTM/value ='250' and $count >= 1) then something
`
You can use
if (count(//bracketSequenceNumber)
!= count(distinct-values(//bracketSequenceNumber) then ...
If you actually want to find the duplicates, use group by in XQuery 3.1 to process each group of equal values and test whether the group size is 2 or more.
I ran following query to use multiple apoc.do.when but it seems only my first apoc.do.when is executing
load csv from "file:///D:leads.csv"
as row
FIELDTERMINATOR ','
WITH row[0] as id,
row[1] as fname,
row[2] as lname,
row[4] as email1,
row[5] as email2,
row[6] as phone1,
row[7] as phone2,
row[8] as phone3,
split(row[11]," ") as birthDay
LIMIT 5
MERGE (l:Lead {id:id})
with l as leadRef,email1,email2,phone1,phone2,phone3,fname,lname,id
CALL apoc.do.when(email1 is not null,'MERGE (e1:Email {value:email}) MERGE (l)-[r:Has_Email]->
(e1)','',{email:email1,l:leadRef}) YIELD value WITH value AS
ignored,leadRef,email1,email2,phone1,phone2,phone3,fname,lname,id
CALL apoc.do.when(phone1 is not null,'MERGE (p1:Phone {value:phone}) MERGE (l)-[r:Has_Phone]->
(p1)','',{phone:phone1,l:leadRef}) YIELD value WITH value AS
ignored2,leadRef,email1,email2,phone1,phone2,phone3,fname,lname,id
CALL apoc.do.when(phone2 is not null,'MERGE (p2:Phone {value:phone}) MERGE (l)-[r:Has_Phone]->
(p2)','',{phone:phone2,l:leadRef}) YIELD value WITH value AS
ignored3,leadRef,email1,email2,phone1,phone2,phone3,fname,lname,id
return true
is there a way to execute multiple query based on multiple condition ? without break in first statement
Make use of UNION between each CALL apoc.do.when.
I'm performing an Sqlite3 query similar to
SELECT * FROM nodes WHERE name IN ('name1', 'name2', 'name3', ...) LIMIT 1
Am I guaranteed that it will search for name1 first, name2 second, etc? Such that by limiting my output to 1 I know that I found the first hit according to my ordering of items in the IN clause?
Update: with some testing it seems to always return the first hit in the index regardless of the IN order. It's using the order of the index on name. Is there some way to enforce the search order?
The order of the returned rows is not guaranteed to match the order of the items inside the parenthesis after IN.
What you can do is use ORDER BY in your statement with the use of the function INSTR():
SELECT * FROM nodes
WHERE name IN ('name1', 'name2', 'name3')
ORDER BY INSTR(',name1,name2,name3,', ',' || name || ',')
LIMIT 1
This code uses the same list from the IN clause as a string, where the items are in the same order, concatenated and separated by commas, assuming that the items do not contain commas.
This way the results are ordered by their position in the list and then LIMIT 1 will return the 1st of them which is closer to the start of the list.
Another way to achieve the same results is by using a CTE which returns the list along with an Id which serves as the desired ordering of the results, which will be joined to the table:
WITH list(id, item) AS (
SELECT 1, 'name1' UNION ALL
SELECT 2, 'name2' UNION ALL
SELECT 3, 'name3'
)
SELECT n.*
FROM nodes n INNER JOIN list l
ON l.item = n.name
ORDER BY l.id
LIMIT 1
Or:
WITH list(id, item) AS (
SELECT * FROM (VALUES
(1, 'name1'), (2, 'name2'), (3, 'name3')
)
)
SELECT n.*
FROM nodes n INNER JOIN list l
ON l.item = n.name
ORDER BY l.id
LIMIT 1
This way you don't have to repeat the list twice.
How can I get a single row result (e.g. in form of a table/array) back from a sql statement. Using Lua Sqlite (LuaSQLite3). For example this one:
SELECT * FROM sqlite_master WHERE name ='myTable';
So far I note:
using "nrows"/"rows" it gives an iterator back
using "exec" it doesn't seem to give a result back(?)
Specific questions are then:
Q1 - How to get a single row (say first row) result back?
Q2 - How to get row count? (e.g. num_rows_returned = db:XXXX(sql))
In order to get a single row use the db:first_row method. Like so.
row = db:first_row("SELECT `id` FROM `table`")
print(row.id)
In order to get the row count use the SQL COUNT statement. Like so.
row = db:first_row("SELECT COUNT(`id`) AS count FROM `table`")
print(row.count)
EDIT: Ah, sorry for that. Here are some methods that should work.
You can also use db:nrows. Like so.
rows = db:nrows("SELECT `id` FROM `table`")
row = rows[1]
print(row.id)
We can also modify this to get the number of rows.
rows = db:nrows("SELECT COUNT(`id`) AS count FROM `table`")
row = rows[1]
print(row.count)
Here is a demo of getting the returned count:
> require "lsqlite3"
> db = sqlite3.open":memory:"
> db:exec "create table foo (x,y,z);"
> for x in db:urows "select count(*) from foo" do print(x) end
0
> db:exec "insert into foo values (10,11,12);"
> for x in db:urows "select count(*) from foo" do print(x) end
1
>
Just loop over the iterator you get back from the rows or whichever function you use. Except you put a break at the end, so you only iterate once.
Getting the count is all about using SQL. You compute it with the SELECT statement:
SELECT count(*) FROM ...
This will return one row containing a single value: the number of rows in the query.
This is similar to what I'm using in my project and works well for me.
local query = "SELECT content FROM playerData WHERE name = 'myTable' LIMIT 1"
local queryResultTable = {}
local queryFunction = function(userData, numberOfColumns, columnValues, columnTitles)
for i = 1, numberOfColumns do
queryResultTable[columnTitles[i]] = columnValues[i]
end
end
db:exec(query, queryFunction)
for k,v in pairs(queryResultTable) do
print(k,v)
end
You can even concatenate values into the query to place inside a generic method/function.
local query = "SELECT * FROM ZQuestionTable WHERE ConceptNumber = "..conceptNumber.." AND QuestionNumber = "..questionNumber.." LIMIT 1"
SELECT COUNT (DISTINCT S.PK_Submission)
FROM Fact_Submission FS, Submission S
WHERE
FS.FK_Submission = S.PK_Submission
AND FS.FK_Submission_Date >= 20100101
AND FS.FK_Submission_Date <= 20101231
I've tried this:
SELECT
{[Measures].[Fact Submission Count]} ON AXIS(0),
Distinct({[Submission].[PK Submission] }) ON AXIS(1)
FROM [Submission]
WHERE
([Date].[Calendar Year].[2010])
but the result is the same
any idea how to write this in MDX? I'm pretty new at this so still haven't figured it out.
This is correct answer:
WITH SET MySet AS
{[Measures].[Fact Submission Count]}
*
DISTINCT({ EXCEPT([Submission].[PK Submission].Members, [Submission].[PK Submission].[All]) })
MEMBER MEASURES.DistinctSubmissionCount AS
DISTINCTCOUNT(MySet)
SELECT {MEASURES.DistinctSubmissionCount} ON 0
FROM [Submission]
WHERE
([Date].[Calendar Year].[2010])
I have excluded "All" row because it's also being counted by COUNT function so I always had +1.