Using a query for Cosmos, how do I select out all Roles that have an ID of '11'?
{
"Roles":[
{
"Name":"Admin",
"ID":[
"11",
"22",
"33"
]
},
{
"Name":"User",
"ID":[
"11"
]
}
]
}
I think it sort of looks like this but not sure about the where clause...
select value r from c join r in c.Roles join i in r.ID // where i = ??
Please try something like this:
select value r from c join r in c.Roles where ARRAY_CONTAINS(r.ID,'11',false)
Hope this can help you:).
Related
For this table results display top of the results.
For this table results display bottom of the results.
I tried a few ways, a join. But the join takes alternates
table1 record
table2 record
table1 record
table2 record
I need
table1 record
table2 record
table2 record
table2 record
{
for $an in /db/table1/row
where $an/ACCOUNT = "something"
return $an
}
{
for $a in /db/table2/row
where $a/PAT_ACCT_NBR = "something"
return $a
}
results
$an here
$a here.
If I understand you correctly you could simply query the tables as needed and combine them to put them in the desired order:
let $table1 := //db/table1/row/ACCOUNT/[text() = 'something']
let $table2 := //db/table2/row/PAT_ACCT_NBR/[text() = 'something']
return ($table1, $table2)
The XPath part is just a suggestion; use whatever works for you.
I have the following values inside a cell of a json column in MariaDB 10.4:
{
"0": [
21,
"Pallet da 1250kg (50 * Sacco da 25kg)",
"1250",
"kg"
],
"1": [
21,
"Sfuso",
"12",
"kg"
],
"2": [
12,
"Sacco da 5kg",
"10",
"kg"
],
"3": [
12,
"Pallet da 2500kg (2 * Pallet da 1250kg (50 * Sacco da 25kg))",
"5000",
"kg"
]
}
The keys ("0", "1", "2", "3") are automatically generated.
I would like to count the number of rows that have the second element of each array identical to the one I pass in the condition.
For now I'm capable of doing such a thing:
query = '''SELECT COUNT(*) AS rowcount FROM ordine_al_fornitore WHERE JSON_CONTAINS(fca_ordinati, '"''' + myVar + '''"', '$.[*]')'''
Which print is:
SELECT COUNT(*) AS rowcount FROM ordine_al_fornitore WHERE JSON_CONTAINS(fca_ordinati, '"Sacco da 5kg"', '$.[*]')
I just know how to pass the key in a fixed way ($.[*]), while actually I would like to iter through the keys to check if that value exists in cell 1 of the array (and consequently count).
I would like to know how I can improve my query.
Thanks in advance!
In order to do this, you need to serve JSON_CONTAINS a flat array of strings to search in.
a) JSON_EXTRACT(fca_ordinati, '$.*') to get an array of object's values
b) JSON_EXTRACT(fca_ordinati, '$.*[1]') to get an array of each entry's 2nd value (index 1)
c) JSON_CONTAINS(..., '"Sacco da 5kg"') search for string appearance in that array
SELECT COUNT(*)
FROM ordine_al_fornitore
WHERE JSON_CONTAINS(
JSON_EXTRACT(fca_ordinati, '$.*[1]'),
'"Sacco da 5kg"' -- note the string needs to have quotes
);
This works on MySQL 5.7.22 or newer: https://www.db-fiddle.com/f/bNyV8wMbNhF1qTWBCBt7un/0
And MariaDB 10.3 or newer: https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=d1c3d750ee2ef58a60d234a58f0fc5d2
I have the following query:
SELECT * from tWords where WordAton IN ("bbb", "aaa2", "ccc", "aaa1")
the query returns first the results for "aaa1" then for "aaa2" then for "bbb" and then for "ccc". Is there a way to return the results in the order of the input array, which means first the results for "bbb" then for "aaa2"... etc.
Thank you in advance.
You can apply conditional ordering like this:
SELECT *
from tWords
where WordAton IN ('bbb', 'aaa2', 'ccc', 'aaa1')
order by case WordAton
when 'bbb' then 1
when 'aaa2' then 2
when 'ccc' then 3
when 'aaa1' then 4
end
In SQL (not just SQLite), the only way to always return rows in a given order is with a SQL ORDER BY ... clause. So the short answer is, "No", there's no simple way to return rows in the order given by the contents of an IN (...) clause.
You could use a common table expression (CTE) to define a sort order, but that's usually not worth the trouble. This isn't the same thing as ordering by the contents of a IN (...) clause, but it looks the same. (You're ordering by the sort order specified in the CTE.)
with ordered_words as (
select 1 as sort_order, 'bbb' as WordAton union
select 2, 'aaa2' union
select 3, 'ccc' union
select 4, 'aaa1'
)
select t.WordAton
from tWords t
join ordered_words o on t.WordAton = o.WordAton
where t.WordAton in ('bbb', 'aaa2', 'ccc', 'aaa1')
order by o.sort_order;
I would like to execute a fairly complex SQL statement using SQLite.swift and get the result preferably in an array to use as a data source for a tableview. The statement looks like this:
SELECT defindex, AVG(price) FROM prices WHERE quality = 5 AND price_index != 0 GROUP BY defindex ORDER BY AVG(price) DESC
I was studying the SQLite.swift documentation to ind out how to do it properly, but I couldn't find a way. I could call prepare on the database and iterate through the Statement object, but that wouldn't be optimal performance wise.
Any help would be appreciated.
Most sequences in Swift can be unpacked into an array by simply wrapping the sequence itself in an array:
let stmt = db.prepare(
"SELECT defindex, AVG(price) FROM prices " +
"WHERE quality = 5 AND price_index != 0 " +
"GROUP BY defindex " +
"ORDER BY AVG(price) DESC"
)
let rows = Array(stmt)
Building a data source from this should be relatively straightforward at this point.
If you use the type-safe API, it would look like this:
let query = prices.select(defindex, average(price))
.filter(quality == 5 && price_index != 0)
.group(defindex)
.order(average(price).desc)
let rows = Array(query)
example: SELECT title,ROW_NUM FROM article ORDER BY count_read.
What should ROW_NUM be replace by ?
I don't like to after getting the results generate the index by program, because I want to insert into a table Rank with the result data by querying the example DQL above.
What I want to achieve maybe like :
"INSERT INTO RANK r (title, index, lastIndex)
SELECT title,ROW_NUM,(SELECT index FROM RANK WHERE id = :id - 1) FROM article ORDER BY count_read"
Thanks in advance..
I think you might use variables, like this:
"
SET #row_num := 1;
INSERT INTO RANK r (title, index, lastIndex)
SELECT title,
(#row_num := #row_num + 1),
(SELECT index FROM RANK WHERE id = :id - 1)
FROM article ORDER BY count_read
"