Returning array from complex documents - azure-cosmosdb

I am trying to get an array of id's into an array of strings and not objects.
Folowing query works:
SELECT * FROM c.id
Returns:
[
"92226d610a1047ecb207c98f845bde14",
"3936d9172f8c4bfd891a91457935f1ac",
"ab466a79f1b44fc99c0f2fcefbec0aa2",
"0d5254279e02430ba8f09afbbd6f2259",
"b768a932ac9042f096557eea3ad5bc4e",
"6917916a41724249a1ae85963f46d58d",
"593ff8f0ceab4b2c8279d77b270307e4"]
However when I add a where clause it fails:
SELECT * FROM c.id where c.vesselId="6c0f3e571b0f4e3bb96f82e6b08d8d36"
What is the best approach to get an array with a where clause?

You can use distinct value clause to derive ids as array.
select distinct value c.id from c where c.vesselId ='6c0f3e571b0f4e3bb96f82e6b08d8d36'

Related

Use ARRAY_CONTAINS with a select statement

I have a query which nearly returns the data I need:
SELECT *
FROM a in c.Things
WHERE ARRAY_CONTAINS(['ThingA', 'ThingB'], a.Name)
The problem is that the array ['ThingA', 'ThingB'] can't be hard-coded, as the values in the array should by dynamically generated based off some query. For this example, that query is this:
select VALUE ARRAY (
SELECT VALUE a.Name
FROM a in c.Things
where a.Visible)
from c
WHERE c.Discriminator='Type'
Which returns something like: ['ThingOne', 'ThingTwo']
Is it possible to include a query inside ARRAY_CONTAINS like this:
SELECT *
FROM a in c.Attributes
WHERE ARRAY_CONTAINS(
( select VALUE ARRAY(
SELECT VALUE a.Name
FROM a in c.Things
where a.Visible)
from c
WHERE c.Discriminator='Type'
)
, a.Name)
If I run this in Cosmos DB Studio I get this error:
Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException: {"errors":[{"severity":"Error","location":{"start":147,"end":148},"code":"SC2001","message":"Identifier 'c' could not be resolved."}
If I understand correctly, your inner query is independent of the outer query, so what you want is an uncorrelated subquery. This is not supported in Cosmos DB; the documentation says:
Azure Cosmos DB supports only correlated subqueries.

Give alias to anonymous column of global collection type in SELECT FROM TABLE(collection)

I'm trying to use values from an apex_t_numbers collection in a SELECT query as WITH subquery:
atn_cur_ids := apex_string.split_numbers(arg_v_ids, ':');
-- So if arg_v_ids := '1:2:3', then atn_cur_ids := apex_t_numbers(1, 2, 3)
with t_cur_ids as (
select * as id from table(atn_cur_ids);
)
select text from t_texts
join t_cur_ids on t_texts.id = t_cur_ids.id
And here's the problem - apex_t_numbers is a table of number, not of record type with named fields. Oracle SQL doesn't allow to give aliases to a * even if it has only one single "anonymous" column.
A possible solution could be a function that both
receives * and
returns value per row
But I am aware of only one that can get a * and return something - count(*), and it doesn't meet the second requirement.
Alright, found a solution. It could be done with column_value as a name of column:
with t_cur_ids as (
select column_value as id from table(atn_cur_ids);
)
select text from t_texts
join t_cur_ids on t_texts.id = t_cur_ids.id

string_agg function with IN operator not working in PostgreSQL Query

here is my query
select *
from table
where id in ( select string_agg(CAST(id as varchar), '","') FROM table)
string_agg() is completely useless and unnecessary for that:
select *
from table_one
where id in (select id FROM other_table)
I assume you are doing that for two different tables, otherwise that would be a very expensive way of writing: select * from table where id is not null

selecting only max clause without group by properties in subquery using Nhibernate

I have SQL query like this:
select * from dbo.table1 where Id in
(
select max(id) as id from dbo.table1 group by prop1, prop2, prop3
)
I want to create NHibernate query which is be able to do this for me. I tried to use QueryOver but it doesn't work. Do you have any suggestions how to do it?
NHibernate supports even this kind of queries. Please, see more in documentation: 15.8. Detached queries and subqueries. We just have to split the query (as in your SQL snippet) into two parts:
inner select
the select with the IN clause
Let's assume, that the dbo.table1 in the Questin is mapped into MyEntity.
To create inner select, let's use the DetachedCriteria
EDIT (extended with the Group by, SqlGroupProjection)
There is an extract of the SqlGroupProjection method:
A grouping SQL projection, specifying both select clause and group by
clause fragments
// inner select
DetachedCriteria innerSelect = DetachedCriteria
.For(typeof(MyEntity))
.SetProjection(
Projections.ProjectionList()
.Add(
Projections.SqlGroupProjection(
" MAX(ID) ", // SELECT ... max(ID) only
" Prop1, Prop2, Prop3", // GROUP BY ... property1, p2...
new string[] {"ID"}, // could be empty, while not used for
new IType[] { NHibernate.NHibernateUtil.Int32 } // transformation
)
)
;
Note: I've provided even the last two paramters, but in this case they could be empty: new string[], new IType[] {}. These are used only for Transformation (materialization from data into entity). And this is not the case, we are just building inner select...
// the select with IN clause
var result = session.CreateCriteria(typeof(MyEntity))
.Add(Subqueries.PropertyIn("ID", innerSelect))
.List<MyEntity>();
Also related could be 15.7. Projections, aggregation and grouping

In query in SQLite

"IN" query is not working. Please guide me if i am wrong.
KaizenResultsInformationTable is MasterTable having field "recordinfo", this field contains Child table Ids as string.
kaizenResultsRecordInformationTable is Childtable having field "recordId".
I have to match records of child.
Query:
select recordinfo from KaizenResultsInformationTable
Output: ;0;1;2;3;4;5;6;7;8;9;10
Query:
select substr(replace(recordinfo,';','","'),3,length(recordinfo))
from KaizenResultsInformationTable`
Output: "0","1","2","3","4","5"
This query is not working:
select * from kaizenResultsRecordInformationTable
where substr(recordid,0,2) in (
select substr(replace(recordinfo,';','","'),3,length(recordinfo))
from KaizenResultsInformationTable
)
This query is working:
select * from kaizenResultsRecordInformationTable
where substr(recordid,0,2) in ("0","1","2","3","4","5")
You can't use in like that. In your second query, you are passing in a single string containing a comma-separated list of values.
It is better to represent a list of IDs as one record for each value.
Also, I'm not sure why you are taking a substring of your recordid. You should usually be storing one value per column.
However, if you can't change the schema, you can use string matching with 'like' instead of 'in'. Something like this should work:
select a.* from kaizenResultsRecordInformationTable a
join KaizenResultsInformationTable b
on (';'+b.recordinfo+';') LIKE ('%;'+trim(substr(recordid,0,2))+';%')
So if your recordinfo looks like 1;2;3;4;5;6, and your substr(recordid,0,2) looks like 1, this will include that row if ";1;2;3;4;5;6;" LIKE "%;1;%", which is true.

Resources