How to apply distinct on google datastore using objectify? - google-app-engine

I am trying to fetch some entities with distinct values on a property. Say, I have an entity called messages. And it has some properties say personId, typeId, convId, createdTime, etc. I want fetch messages of personId p1 with distinct convIds. How can I do that.
I already referred to
Executing DISTINCT query with objectify for app engine
and some others. And tried something like this.
ofy().load().type(messages.class).limit( 10 ).filter("personId ==", "p1").order("-createdTime").project("convId").distinct(true).list();
I am sure that there are some entities with this combination. But it is fetching no entities.
Please help me with this.

Unfortunately, your query isn't valid. For projection queries you need distinct to be on the values that you are sorting by (i.e. -createdTime). To use distinct you'll need to order by convId.
https://cloud.google.com/datastore/docs/concepts/queries#restrictions_on_queries has more details.

Related

Is there a way to select all Objects in Salesforce that start with a value?

I can select all apex triggers by using
select id, name from ApexTrigger
and classes by using
select id, name from ApexClasses
but is there a way to select all objects ?
I have tried
select id, name from Objects but doesnt seem to work ?
SOQL queries run on exactly one object.
If you need to perform a full-text search across multiple objects, what you need is SOSL, the Salesforce Object Search Language. Note however that SOSL is quite different from SOQL. See the linked developer guide for more information.

parent-child and child-parent relationships in one query SOQL

Is it possible to write a query to access parent-child and child-parent objects, in one SOQL query?
I have a scenario where, I need to access Account Objects from child and child of Account too, in the same query.
Example:
Select Id,
(Select Id,Name, (Select Id, Address from Addresses__r) from x__r.Account),
x__r.Account.Name
From x
.
(Pardon me, if I use any wrong terms. I am pretty new to Salesforce)
Yes, it is possible to do so.
In the example below, we are using sub-query to retrieve Ids of all Account Territories (parent-to-child relationship) and, at the same time, we are using child-to-parent relationship to retrieve TimeZoneSidKey of the User who has created the record.
SELECT Id, (SELECT Id FROM Accounts_Territories__r), CreatedBy.TimeZoneSidKey FROM Account
Documentation on the topic

Subselect in GQL syntax (Datastore viewer)

I have two entities: Cars (ownerId->String) and Users (userId->String), where ownerId is the foreign key representing userId.
I am trying to execute this Query, but I get "GQL syntax" at Datastore Viewer. How can I do this SUBSELECT?
SELECT * FROM Cars WHERE ownerId IN (SELECT userId FROM Users);
You can't. GQL is not SQL.
You will need to to do one select, then iterate and fetch/filter or do two selects and merge in code.
Alternately add a flag in the Car object that tells you that the Owner is a User, then you don't need to
If you are not trying to write code, but just explore then the remote_api shell is a place to try these things. Though this is much easier in python ;-)

Salesforce SOQL - DefaultDivision with User info

I'm trying to write a SOQL query that pulls back User information along with the DefaultDivision's name. When executing the following:
Select u.Email, u.FirstName, u.LastName, u.DefaultDivision from User u
the value for 'DefaultDivision' is an Id (like 02d3498202020222) instead of the friendly name (like North America). I know that the DefaultDivision maps to the Division object's Id, but how do I get the name?
I have tried things like:
select u.Email, (select Name from Division where Id = u.DefaultDivision) from User u
but that doesn't work because there is no direct relationship (and yet, there is...!).
Unlike SQL, SOQL does not support subqueries of the type you have given there.
In most cases with SOQL where there is an explicit relationship defined, you can query data from that explicit relationship directly, like
select u.Email, u.DefaultDivision.Name from User u where Id=blahblahblah
However there are some cases where such a relationship is not explicitly defined, and I believe DefaultDivision is one of those (so in fact my query above probably will not work). When this is the case, there's no single-query way to get the information you want -- you'll have to first query on divisions and their IDs, then query users with their DefaultDivision, and then resolve the division names from the IDs using the results of the first query.
Fortunately this type of situation doesn't happen very often anymore, but in using Divisions you're plumbing the depths of Salesforce.com a bit so you may find some unusual stuff there.

How to select lots of counts of lots of different criterias

I'm trying to do a detailed Member Search page. It uses Ajax in every aspect like Linkedin did on search pages.
But I don't know how I can select counts of multiple criterias. You can see what I meant by the attachment. I mean, if I select every count with different queries it's gonna take forever.
Should I store the count values on another table? Then, further development will be hard and time consuming.
I need your advices.
In this web site, you enter just a keyword and it shows you the all available fields order by count DESC;
You can create an Indexed View that groups by your criteria and uses COUNT_BIG to get totals.
CREATE VIEW dbo.TagCount
WITH SCHEMABINDING
AS
SELECT Tag, COUNT_BIG(*) AS CountOfDocs
FROM dbo.Docs
GROUP BY Tag
GO
CREATE UNIQUE CLUSTERED INDEX IX_TagCount ON dbo.TagCount (Tag)

Resources