Facebook FQL Query Order By Using Multiple Columns - facebook

I have a Facebook FQL query which in which everything is going good but what I want is to Order the results by 2 columns.
Here is the last line of the script and until this it is working.
IN (SELECT aid FROM album WHERE owner=133025843429181)ORDER BY created desc
What I want is to order it by created desc and like_info desc, so this is what I am trying and it returns an error.
IN (SELECT aid FROM album WHERE owner=133025843429181)ORDER BY created desc, like_info desc
This is not working. I tried to remove the comma in between and still not working
IN (SELECT aid FROM album WHERE owner=133025843429181)ORDER BY created desc like_info desc
Any solution for this?

Please try the below code: It might be the fix you need
IN (SELECT aid FROM album WHERE owner=133025843429181) ORDER BY (created desc * like_info desc) DESC

This isn't possible.
FQL can handle simple math, basic boolean operators, AND or NOT logical operators, and ORDER BY and LIMIT clauses. ORDER BY can contain only a single table.
https://developers.facebook.com/docs/technical-guides/fql#query

Related

PostgreSQL RANK() function over aggregated column

I'm constructing quite complex query, where I try to load users with their aggregated points altogether with their rank. I found the RANK() function that could help me to achieve this but can't get it working.
Here's the query that is working without RANK:
SELECT users.*, SUM(received_points.count) AS pts
FROM users
LEFT JOIN received_points ON received_points.user_id = users.id AND ...other joining conditions...
GROUP BY users.id
ORDER BY pts DESC NULLS LAST
Now I would like to select also the rank - but this way using RANK function it's not working:
SELECT users.*, SUM(received_points.count) AS pts,
RANK() OVER (ORDER BY pts DESC NULLS LAST) AS position
FROM users
LEFT JOIN received_points ON received_points.user_id = users.id AND ...other joining conditions...
GROUP BY users.id
ORDER BY pts DESC NULLS LAST
It tells: PG::UndefinedColumn: ERROR: column "pts" does not exist
I guess I get whole concept of window functions wrong. How can I select the rank of user sorted by aggregated value like pts in example above?
I know I can assign ranks manually afterwards but what if I want to also filter the rows according to users.name in query and still get user's rank in general (not-filtered) leaderboard...? Dont know if I'm clear...
As Marth suggested in his comment:
You can't use pts here as the alias doesn't exist yet (you can't reference an alias in the same SELECT it's defined). RANK() OVER (ORDER BY SUM(received_points.count) DESC NULLS LAST) should work fine.

fql OR and order by desc with offset

I have some issue with a Facebook's fql request :).
When I request like this
SELECT uid, profile_url
FROM user where contains('potatoe') OR uid in(select id from profile where contains ('potatoe'))
ORDER BY mutual_friend_count desc LIMIT 0,2
I have always the same results, even if I change the offset...
But
If I change DESC to ASC the "offset" works...
If I change OR to AND
and let the order by desc => the "offset" works too!
Something is wrong with my OR/desc/offset combo?
Or I just have misunderstood something with "or" operators? :/
Thanks a lot.
You should write your FQL query like that:
SELECT uid, profile_url
FROM user
where
uid in(select uid from user where contains ('potatoe') )
OR uid in(
select id from profile where contains ('potatoe')
)
ORDER BY mutual_friend_count desc
LIMIT 0,2
FQL couldn't interpret in boolean expression the result of "contains", so your OR operator failed here...
I think the problem is your query is asking the same question two different ways:
Show me all my friends user profiles that contain 'potatoe' OR
Show me all profiles that belong to users who are my friends that contain 'potatoe'.
Simplify your query to this and see if it works as expected.
SELECT uid, profile_url FROM user WHERE contains('potatoe')
ORDER BY mutual_friend_count DESC LIMIT 0,2

FQL Multiquery Returned Info

I'm trying to use an FQL Multiquery to obtain my most recent Facebook question and options with one query using an http request.
So far the query I tried to use is:
SELECT name, votes FROM question_option WHERE question_id IN
(SELECT id, question FROM question WHERE owner = me()
ORDER BY created_time DESC LIMIT 1)
Unfortunately this only returns the name and votes from the outer query and not the question text from the inner one. Is there a way to retrieve all 3 without making 2 queries?
What you posted isn't a multiquery. A proper multiquery should get you what you want:
{
'question_detail':
'SELECT id, question FROM question WHERE owner = me()
ORDER BY created_time DESC LIMIT 1',
'question_answers':
'SELECT name, votes FROM question_option WHERE question_id IN
(SELECT id FROM #question_detail)'
}
You'll need to get rid of the whitespace for this to properly execute.

FQL - Order photos in album by like_count

I'm trying to get the photos of a Facebook album ordered by like count in FQL with a this query:
SELECT like_info, object_id FROM photo WHERE aid="xxxxxxxxxx_xxxx" ORDER BY like_info.like_count desc
But I get an error (#602) like_count is not a member of the like_info scalar.
SELECT like_info, object_id FROM photo WHERE aid="xxxxxxxxxx_xxxx" ORDER BY like_info desc
Does the trick, but I don't know why: isn't *like_info* an object? it is possible to order a query using a member of an object?
Just use ORDER BY like_info. Don't forget to put LIMIT, otherwise query might crash for large number of pics:
SELECT like_info, object_id FROM photo WHERE aid="20531316728_324257" ORDER BY like_info desc LIMIT 10
Run in Facebook API explorer

Single fql query get latest photos for array of friends

SELECT object_id, src_small FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner = me())ORDER BY created DESC LIMIT 1
in the example me() can be replaced with any friend_id , order by "created" is also posible
I am looking for a way to declare friend_id's as array and avoid using fql.multiquery (very slow and got timeouts using node.js)
Thanks