Query to find DISTINCT between array of objects in Cosmos DB [ SQL API ] - distinct

I am using Azure Cosmos DB with SQL API and we need to find the DISTINCT values between array of objects in the same document.
I have structured the collection in the following ways
I have 2 main attribute RID and RNAME. In the first collection, I have only one document which contains all the RID and RNAME mapping in the array of objects.
"Details":[
{
"RID":"1",
"RNAME:"Car"
},
{
"RID":"1",
"RNAME:"Car"
}]
In second collection, I have multiple documents for each RID and RNAME mapping.
{
"RID":"1",
"RNAME:"Car"
}
I am using Stored procedure and I need to know which one is a good way to get DISTINCT of RNAME using stored procedure.
Using first collection, I am not sure how to query to find DISTINCT RNAME between objects in the array.
Using second collection. when I use the SQL editor, the distinct Query works but not sure how to put it in Stored procedure.
DISTINCT Values of RNAME

1.multiple documents
use sql:
select distinct c.RNAME from c
2.single document
use sql:
SELECT distinct d.RNAME FROM c
join d in c.Details

Related

How to query distinct documents that contain Unordered List properties in FileNet P8?

I am trying to send a dynamically built query to FileNet 5.2 using the .Net API. The Document Class that I am trying to query has three properties in the select list that have a cardinality of Unordered List and type String. When I send over the query with the DISTINCT keyword, FileNet returns this error:
Can't select property with "distinct": RouteNumber.
Here is an example SQL statement that is getting passed to FileNet:
SELECT DISTINCT
td.DrawingNumber,
td.ProjectTitle,
td.WorkArea,
td.RouteNumber,
td.City,
td.County,
td.DrawingNumberAssignedYear,
td.Comment,
td.MajorVersionNumber,
td.IsCurrentVersion
FROM TrafficDocument AS td WITH EXCLUDESUBCLASSES
LEFT OUTER JOIN RoadwaySegment AS rwy WITH EXCLUDESUBCLASSES ON td.ID = rwy.ParentObjectID
WHERE td.IsCurrentVersion = True
AND '104' IN RouteNumber
ORDER BY td.DrawingNumber, td.TrafficPlanDiscipline
OPTIONS (TIMELIMIT 180)
I need the DISTINCT keyword because the joining document class, RoadwaySegment, is causing duplicates but it is needed in the query because their values can be filtered against as well.
So how can I achieve my goal of querying FileNet and retrieving unique results?
Is RouteNumber an orderable property? Only orderable ones are allowed for queries with DISTINCT.
DISTINCT restrictions:
A DISTINCT query can be performed only when all
of the SELECTed properties are orderable. For example, if property P1
is not orderable (Binary type, or String type with UsesLongColumn),
the following query produces an error message:
SELECT DISTINCT P1 From Object1

Elasticsearch query against multiple types

I have two types stored in ES:
Photos (fields: photo-id, description, createDateAsLong)
VisitHistory (username, photo-id - with 30days TTL)
Is it possible to query ES for records of type Photos which photo-id doesn't exists in VisitHistory (for some username value). I mean a query which is equivalent of sql query like this:
SELECT * FROM Photos WHERE photo-id NOT IN (SELECT photo-id FROM VisitHistory WHERE username = 'somefoousername');
If you make Photos a parent document and VisitHistory a child document of Photos, you should be able to use a combination of not and has_child filters to build such request.

How can I use Linq to join between objects and entities?

I have a collection of IDs in memory, and I would like to fetch only rows from a DB matching those IDs.
In SQL, I could either write a query like SELECT * FROM mytable WHERE id IN (1,3,5,10) or do a join between tables.
My problem is that EF can't build a query where I join my EF-data with my local array or list.
(I'm using EF4.1, but I'm guessing the problem/solution would be similar in older versions, as well as with Linq-to-SQL.)
You can use Contains() with your ID collection myIDs to generate the equivalent WHERE id IN .. query:
var results = context.mytable.Where(x => myIds.Contains(x.Id));

Entity Framework 4 generated queries are joining full tables

I have two entities: Master and Details.
When I query them, the resulting query to database is:
SELECT [Extent2]."needed columns listed here", [Extent1]."needed columns listed here"
FROM (SELECT * [Details]."all columns listed here"...
FROM [dbo].[Details] AS [Details]) AS [Extent1]
LEFT OUTER JOIN [dbo].[Master] AS [Extent2] ON [Extent1].[key] = [Extent2].[key]
WHERE [Extent1].[filterColumn] = #p__linq__0
My question is: why not the filter is in the inner query? How can I get this query? I've tried a lot of EF and Linq expressions.
What I need is something like:
SELECT <anything needed>
FROM Master LEFT JOIN Details ON Master.key = Details.Key
WHERE filterColumn = #param
I'm having a full sequential scan in both tables, and in my production environment, I have milions of rows in each table.
Thanks a lot !!
Sometimes The entity Framework does not produce the best query. You can do a few of the following to optimize.
Modify the linq statement (test with
LINQPad)
Create a stored proc and map the stored proc to return an entity
Create a view that handles the join and map the view to a new
entity

Adding a random Guid column to a Linq to Entities query to grab random records

I've found some articles about using the RandomView view and the GetNewID function to pull back randomized records, but they are using this method with Linq to SQL which allows Functions and Stored Procs to be used with no return value or a scalar return value. From what I understand, a Stored Proc has to been returned as one of the Entity Framework objects from my generated model. I have been able to get that to work as an object, but not returning a scalar or no return set.
What I want to do is to simply add a column to my Linq query that contains a newly generated Guid so that I can order by the new Guids and take a specific number of records randomly. Can anyone help with some sort of lambda expression or join that would enable me to do this? It seems like it should be something built into EF, but I understand that we are on EF v1.
(Please provide code in VB.net)
In the Select clause of your Linq query, you should be able to insert a GUID like this:
var result = from myRecord in myTable
select new {
field1 = myRecord.field1,
field2 = myRecord.field2,
guidField = Guid.NewGuid()
};
Well, my VB is a little rusty, but I think this will work...
Dim result =
From myRecord in myTable _
Select field1, _
field2, _
guidField = System.Guid.NewGuid()

Resources