Sanity GROQ: Order by geo::distance - sanity

I would like to order results in a GROQ by distance. Something like:
"places": *[
_type == 'place'
] {
name
} | order(geo::distance(^.start->location, location) desc),
but this doesn't seem to have the intended result.
Is it possible to order results by geo::distance?

Related

SELECT DISTINCT ON expressions must match initial ORDER BY expressions in typeorm query

I have a query that I want get rows with unique item_id and sort them according to timestamp.
But I get this error everytime from this code
const boxPreviewActivities = await this.activityLogEntryRepository
.createQueryBuilder('activityLogEntry')
.where("activityLogEntry.activity = 'BOX_PREVIEW'")
.andWhere('activityLogEntry.projectId = :projectId', { projectId })
.andWhere('activityLogEntry.userSub = :userSub', { userSub: user?.sub })
.andWhere((qb) => {
const subQuery = qb
.subQuery()
.select(
"DISTINCT activityLogEntry.originalData ::jsonb -> 'source' ->> 'item_id'"
)
.from(ActivityLogEntryEntity, 'activityLogEntry')
.where("activityLogEntry.activity = 'BOX_DELETE'")
.getQuery();
return `activityLogEntry.originalData ::jsonb -> 'source' ->> 'item_id' NOT IN (${subQuery})`;
})
.distinctOn([
"activityLogEntry.originalData ::jsonb -> 'source' ->> 'item_id' ",
])
.orderBy('activityLogEntry.timestamp', 'DESC')
.limit(limitNumber)
.getMany();
return boxPreviewActivities;
}
** ERROR [ExceptionsHandler] SELECT DISTINCT ON expressions must match initial ORDER BY expressions
QueryFailedError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions**
if I add this two order functions at the and, I dont get error but my query not sorting according to timestamp, instead sort only with item_id. Actually I only want to sort according to timestamp. How can I refactor my query to make it working ?
.orderBy(
"activityLogEntry.originalData ::jsonb -> 'source' ->> 'item_id'",
'DESC'
)
.addOrderBy('activityLogEntry.timestamp', 'DESC')

sql query to select multiple items in sorted order

I am writing a post api in c# to select some values in Azure Cosmos db and is using direct sql queries.
The aim to get the highest value against each id from the request.
request body:
[
{
"userid":"1"
},
{
"userid":"4"
}
]
Db looks like:
{
"userid":"1",
"value":"10",
"Date":"10-9-19"
}
{
"userid":"1",
"value":"20",
"Date":"11-8-19"
}
{
"userid":"4",
"value":"30",
"Date":"10-9-19"
}
{
"userid":"4",
"value":"40",
"Date":"11-9-19"
}
Expected output:
[
{
"userid":"4",
"value":"40",
"Date":"11-9-19"
},
{
"userid":"1",
"value":"20",
"Date":"11-8-19"
}
]
I tried to get the id's into an array then used 'IN' operator, but it would be helpful and appreciated is there more simple query would help.
try the following to get the results.
As per your data, this will work.
SELECT userid,
MAX(value) value,
MAX(Date) Date
FROM YourTable
GROUP BY userid
ORDER BY userid
If you want related date for the MAX(Value), then try this.
SELECT Y.userid, Y.Value, Y.Date
FROM YourTable Y
JOIN
(
SELECT userid,
MAX(value) value
FROM YourTable
GROUP BY userid
)D ON D.userid = Y.userid AND D.value = Y.value

In typescript, how to sort exactly like MSSQL's 'order by' clause for special characters like * , + - etc

I have a Angular5 based front-end which needs to show a list of persons. A similar list is shown at the back-end application. Both lists need to be sorted exactly the same. The back-end list is fetched via a query in MSSQL DB which uses an 'order by' clause like this :
select * from PERSON where GROUP=1 order by PERSON.LAST_NAME ASC, PERSON.INITIALS ASC, PERSON.PREFIX ASC, PERSON.FIRSTNAME ASC
On the frontend, my person model looks like :
export interface Person {
id?: number;
fullName?: string;
}
The fullName here is as per this format : [LastName, Initials Prefix FirstName] eg : Adams, Mr P John.
I am using this method to sort it :
public sortByName(aPersonArr: Person[]) {
aPersonArr.sort((p1, p2) => {
let name1 = p1.fullName;
let name2 = p2.fullName;
// If both names are blank, consider them equal, If one name is blank, place it at the last
if (!name1 && !name2) {
return 0;
} else if (name1 && !name2) {
return -1;
} else if (name2 && !name1) {
return 1;
}
name1 = name1.toLowerCase();
name2 = name2.toLowerCase();
if (name1 < name2) {
return -1;
}
if (name1 > name2) {
return 1;
}
return 0;
});
}
}
But the problem occurs when a person's fullName begins with a special character because SQL Query fetches the names in this order : (Showing only the lastNames here)
*Account
,Adams
.Alkin
+Account
-Adams
Whereas my typescript code sorts them like this : (Showing only the lastNames here)
*Account
+Account
,Adams
-Adams
.Alkin
The reason that I need to have this logic at frontend is that many a times my person list is dynamically prepared and needs to be sorted right away. Is there a way to know what exact logic is used by SQL query to compare strings, so that i can use the same in my sorting method.

Sort a bag with pig

I have the following structure
GROUPED_ANSWERS_PARENT_ID: {group: chararray,ANSWERS: {(id: chararray,score: long,parentId: chararray)}}
My data where score is respectively 27,287,35,37,46,48
((4,{(305467,27,4),(7,287,4),(2791,35,4),(594436,37,4),(110198,46,4),(7263,48,4)}))
I want it to be ordered by score DESC and return the following:
((4,{(7,287,4),(7263,48,4),(110198,46,4),(594436,37,4),(2791,35,4),(305467,27,4)}))
I have tried the following but the result is still incorrect.
SORTED_GROUPED_ANSWERS_PARENT_ID = FOREACH GROUPED_ANSWERS_PARENT_ID {
ORDER_BY_SCORE = ORDER $1 BY score;
GENERATE (group,ORDER_BY_SCORE);
};
Any help would be greatly appreciated.
PS: I have looked at this post, but it did not help me
You are missing the DESC keyword
SORTED_GROUPED_ANSWERS_PARENT_ID = FOREACH GROUPED_ANSWERS_PARENT_ID
{
ORDER_BY_SCORE = ORDER ANSWERS BY score DESC;
GENERATE (group,ORDER_BY_SCORE);
};

Grails: "where" query with optional associations

I'm trying to run a "where" query to find a domain model object that has no association with another domain model object or if it does, that domain model object has a specific property value. Here's my code:
query = Model.where({
other == null || other.something == value
})
def list = query.list()
However, the resulting list only contains objects that match the second part of the OR statement. It contains no results that match the "other == null" part. My guess is that since it's checking a value in the associated object its forcing it to only check entries that actually have this associated object. If that is the case, how do I go about creating this query and actually having it work correctly?
You have to use a LEFT JOIN in order to look for null associations. By default Grails uses inner join which will not be joined for null results. Using withCriteria as below you should get the expected results:
import org.hibernate.criterion.CriteriaSpecification
def results = Model.withCriteria {
other(CriteriaSpecification.LEFT_JOIN){
or{
isNull 'id'
eq 'something', value
}
}
}
UPDATE
I know aliasing is not possible in DetachedCritieria where one would try to specify the join as in createCriteria/withCriteria. There is an existing defect regarding adding the functionality to DetachedCriteria. Just adding the work around for where query as mentioned in defect.
Model.where {
other {
id == null || something == value
}
}.withPopulatedQuery(null, null){ query ->
query.#criteria.subcriteriaList[0].joinType = CriteriaSpecification.LEFT_JOIN
query.list()
}
I would rather use withCriteria instead of the above hack.
this might work:
query = Model.where({
isNull( other ) || other.something == value
})
If that wouldn't work, try something like:
other.id == null || other.something == value
UPDATE:
or with good'ol criteria query:
list = Pack.withCriteria{
or{
isNull 'other'
other{ eq 'something', value }
}
}