fql to get location posts by me or my friends with me tagged - union

Here is what i'm looking for:
select author_uid, tagged_uids, page_id from location_post where author_uid = me() or ( author_uid in (select uid2 from friend where uid1=me()) and me() in tagged_uids)
When i split the queries they return the expected results.
i.e, both
select author_uid, tagged_uids, page_id from location_post where author_uid = me()
select author_uid, tagged_uids, page_id from location_post where author_uid in (select uid2 from friend where uid1=me()) and me() in tagged_uids
work as expected. What I need is a union of the 2.
I'm tempted to say this is either a bug (or a limitation) in the API
Note:
The OR clause itself works if the hard code one of my friend's id and my id. i.e author_uid=<my-id> or (author_uid=<frnd-id> and <my-id> in tagged_uids)

The problem was me() in the query. I had the user's ID around and replace me() with the actual ID did the the job
SELECT id, author_uid, page_id, tagged_uids, timestamp, coords FROM location_post WHERE (<id> IN tagged_uids or author_uid = <id>)

What you are attempting to do will not work. You could try and run a multi-query and get back the results at once and compare them on your end.

Related

Return blank records instead of No Records found from Salesforce

I am working on mobile app using SalesForce Mobile SDK and for a functionality to work, I am building the mapping between the customer_id and corresponding Address. So to have a mapping, I have modified the SOQL Query to get the customer_id value back once the query is executed.
This mapping works fine when there is Address is found. But when there is no address associated, I doesnot get any response. So, mapping a customer_id and put a null address in it, I would need the customer_id back even when address is not found.
To better explain: The following query doesnot return any result since there is no address found:
SELECT Account_vod__c,Address_line_2_vod__c, City_vod__c, Name FROM Address_vod__c WHERE Account_vod__c = '001e000000qVp8WAAS'
But I want to create a query such that if there are no records found, it would return something like:
you could try by using coalesce
SELECT coalesce(Account_vod__c,001e000000qVp8WAAS),
Address_line_2_vod__c, City_vod__c, Name
FROM Address_vod__c WHERE Account_vod__c = '001e000000qVp8WAAS'
THIS ANSWERS THE QUESTION BASED ON INITIAL DBMS TAG :
SELECT a.Account_vod__c, a1.Address_line_2_vod__c, a1.City_vod__c, a1.Name
FROM (SELECT '001e000000qVp8WAAS' AS Account_vod__c) a LEFT OUTER JOIN
Address_vod__c a1
ON a1.Account_vod__c = a.Account_vod__c;
If you have more than one Account_vod__cs then use UNION ALL & do LEFT JOIN :
SELECT a.Account_vod__c, a1.Address_line_2_vod__c, a1.City_vod__c, a1.Name
FROM (SELECT '001e000000qVp8WAAS' AS Account_vod__c
UNION ALL
. . .
) a LEFT OUTER JOIN
Address_vod__c a1
ON a1.Account_vod__c = a.Account_vod__c;
Nested SOQL queries did the trick:
SELECT Id, Name,(SELECT Id, Account_vod__c, Name, Address_line_2_vod__c, City_vod__c FROM Address_vod__r)FROM Account WHERE Id = '001e0000008Ch5nAAC' LIMIT 1

Query to show which games which friends don't have

I have three tables set up in Access. I want to make a query that shows which games someone doesn't have in common with me.
I tried using an unmatched query, but that didn't work since each person has at least one game in common with me.
I guess I'm unsure how to handle this. The GameTimePlayed table basically has the opposite of the information I want to query, so is it possible to query that and add a "Not" conditional to "GameName" or something?
This is for a final project for class, and isn't due for about another month. I don't expect anyone to answer this for me, but even just a point in the right direction would be greatly appreciated. Everything I've tried to find so far is basically about unmatched queries, which did not work for me.
--EDIT TO PROVIDE MORE INFO--
I have all of the games in FavoriteGames. However, not all of my friends (PersonID) have all of my FavoriteGames. I'd like a query to show a record of FirstName, LastName, GameName, for each PersonID, for each GameName that he/she does not have.
Expected Behavior Example: PersonID 10 only has one GameName in common with me. The query should return five records for PersonID 10
(every game except Rocket League).
Sample Data:
tbl_FavoriteGames
tbl_FriendsWithGame
tbl_GameTimePlayed
GameName is the Primary Key for tbl_FavoriteGames
PersonID is the Primary Key for tbl_FriendsWithGame
PersonID, GameName Foreign Keys form a Composite Primary Key for tbl_GameTimePlayed
This is the closest I have gotten so far (still way off though) in that it removes the specified GameName:
SELECT *
FROM tbl_GameTimePlayed
WHERE NOT EXISTS
(
SELECT *
FROM tbl_FriendsWithGame
WHERE tbl_GameTimePlayed.PersonID = tbl_FriendsWithGame.PersonID
AND tbl_GameTimePlayed.GameName = tbl_FavoriteGames.GameName
);
It prompts me to enter a GameName (no idea why). When I enter a GameName, it returns all records that don't have that specific GameName.
This returns 6 games for each person, whether or not the person actually has that game. Could be useful since it contains the people/games that aren't in common.
SELECT PersonID, GameName
FROM tbl_FriendsWithGame, tbl_FavoriteGames
WHERE EXISTS (SELECT PersonID FROM tbl_GameTimePlayed WHERE GameName = tbl_GameTimePlayed.GameName);
I tried "WHERE NOT EXISTS" and that returned 0 results.
--SECOND EDIT: SOLVED!!--
I took a fresh look at the problem today, and figured it out! I used the code mentioned above to query (qry_AllPeopleAllGames) a list of all of the games, for all of the people (so 6 entries per person):
SELECT PersonID, GameName
FROM tbl_FriendsWithGame, tbl_FavoriteGames
WHERE EXISTS (SELECT PersonID FROM tbl_GameTimePlayed WHERE GameName = tbl_GameTimePlayed.GameName);
Then, I made another query that compared the qry_AllPeopleAllGames list to my tbl_GameTimePlayed (which is the list of people, games they actually own, and hours played) and spit out a list of FirstName & LastInitial and GameName that don't exist in the real list:
SELECT [tbl_FriendsWithGame]![FirstName] & " " & [tbl_FriendsWithGame]![LastInitial] AS FullName, GameName
FROM qry_AllPeopleAllGames INNER JOIN tbl_FriendsWithGame ON qry_AllPeopleAllGames.PersonID = tbl_FriendsWithGame.PersonID
WHERE ((NOT Exists (SELECT PersonID, GameName
FROM tbl_GameTimePlayed
WHERE qry_AllPeopleAllGames.PersonID = tbl_GameTimePlayed.PersonID AND qry_AllPeopleAllGames.GameName = tbl_GameTimePlayed.GameName
)));
****NOTE:**** The first part of the SELECT is not needed, I just used it for easier viewing in my actual query results (showing first name/last initial in one field).
I'm really excited that I figured this out! I'm sure there are better/more efficient ways to do this, and if you want to share, please let me know!
I included this in my initial post, but I'll post this as the answer as well.
I took a fresh look at the problem today, and figured it out! Last night while trying to test random possible solutions, I accidently made a query that lists all of the games, for all of the people (so 6 entries per person). Today, I used it as part of the solution, qry_AllPeopleAllGames:
SELECT PersonID, GameName
FROM tbl_FriendsWithGame, tbl_FavoriteGames
WHERE EXISTS (SELECT PersonID FROM tbl_GameTimePlayed WHERE GameName = tbl_GameTimePlayed.GameName);
Then, I made another query that compared the qry_AllPeopleAllGames list to my tbl_GameTimePlayed, which is the real list of people/games/hours played.
It returns the FirstName&LastInitial and the GameName for each PersonID/GameName combo that doesn't appear in the tbl_GameTimePlayed table. Here is the code:
SELECT [tbl_FriendsWithGame]![FirstName] & " " & [tbl_FriendsWithGame]![LastInitial] AS FullName, GameName
FROM qry_AllPeopleAllGames INNER JOIN tbl_FriendsWithGame ON qry_AllPeopleAllGames.PersonID = tbl_FriendsWithGame.PersonID
WHERE ((NOT Exists (SELECT PersonID, GameName
FROM tbl_GameTimePlayed
WHERE qry_AllPeopleAllGames.PersonID = tbl_GameTimePlayed.PersonID AND qry_AllPeopleAllGames.GameName = tbl_GameTimePlayed.GameName
)));
NOTE: The first part of the SELECT is not needed, I just used it for easier viewing in my actual query results (showing first name/last initial in one field).
I'm really excited that I figured this out! I'm sure there are better/more efficient ways to do this, and if you want to share, please let me know!
You need a dataset of all possible pairs of friends/games in order to determine which games each friend does not have. Do you have a tbl_Friends? Consider:
Query1:
SELECT tblFriends.ID, tbl_FavoriteGames.ID FROM tblFriends, tbl_FavoriteGames;
That is a Cartesian query - without JOIN clause every record of each table will associate with each record of other table.
Query2:
SELECT Query1.tblFriends.ID, Query1.tbl_FavoriteGames.ID
FROM tbl_FriendsWithGame RIGHT JOIN Query1 ON (tbl_FriendsWithGame.GameID = Query1.tbl_FavoriteGames.ID) AND (tbl_FriendsWithGame.FriendID = Query1.tblFriends.ID) WHERE tbl_FriendsWithGame.GameID IS NULL;
Or if you don't have tbl_Friends
SELECT DISTINCT tbl_FriendsWithGame.FriendID, tbl_FavoriteGames.ID
FROM tbl_FavoriteGames, tbl_FriendsWithGame;
Then adjust Query2.

SQL - Insert multiple records from select

I have been searching all day but could not find answer to this:
I have a table on SQL Server:
dbo.Program
with fields:
Program.id...PK autoincrement
Program.user...varchar
Program.program...varchar
Program.installed...boolean
Program.department...varchar
Program.wheninstalled...date
Now, I want to insert a new record for every distinct user and copy the department from his latest(Program.wheninstalled) record with other values the same for every user:
Program.user...every unique user
Program.program...MyMostAwesomeProgram
Program.installed...false
Program.department...department of the record with the latest program.wheninstalled field of all the records of the unique user in program.user
Program.wheninstalled...null
I know how to do it in an ugly way:
select the latest records for every user and their department in that record
extract values from 1) and make it into insert into
(field1, field2...fieldX) values
(1records_value1, 1records_value2...1records_valueX),
(2records_value1, 2records_value2...2records_valueX),
...
(Nrecords_value1, Nrecords_value2...Nrecords_valueX)
but I would like to know how to do it in a better way. Oh I cannot use some proper HR databse to make my life easier so this is what I got to work with now.
I'm a postgres guy, but something akin to the below should work:
insert into Program (user, program, installed, department, wheninstalled)
select user,
'MyMostAwesomeProgram',
false,
(select department from someTable where u.user = ...),
null
from users as u;
from https://stackoverflow.com/a/23905173/3430807
you said you know how to do the select
insert into Program (user, program, installed, department, wheninstalled)
select user, 'MyMostAwesomeProgram', 'false' , department, null
from ...
This should do it:
insert into Program (user, program, installed, department, whenInstalled)
select user, program, installed, department, whenInstalled
from
(
select User
, 'MyMostAwesomeProgram' program
, 0 installed
, department
, null whenInstalled
, row_number() over (partition by user order by whenInstalled desc, id desc) r
from Program
) p
where p.r = 1
The interesting bit is the row_number() over (partition by user order by whenInstalled desc, id desc) r.
This says to return a column, r, which holds values 1..n for each user, counting up according to the order by clause (i.e. starting with the most recent whenInstalled and working backwards).
I also included the id field in the order by clause in case there were two installs for the same user on the same date; in such a case the most recently added (the one with the higher id is used first).
We then put this in a subquery, and select only the first record; thus we have 1 record per user, and it's the most recent.
The only values we use from this record are the user and department fields; all else is defined per your defaults.
So I am gonna answer this for some other people who might google this:
To get a records from a table to another table you need to use Select into statement
I like using With XXX as ( some select) to specify, say, "virtual" table with which you can work during the query
As JohnLBevan meantioned a very useful function Row_number, over, partition by and what i missed is a rank
So once you read on these you should be able to understand how to do what I wanted to do.

How can I get T-SQL to show a value that doesn't occurs more than once

I'm sorry, I'm newbie using T-SQL AND I would like to know how can i get value that doesn't occurs more than once. I already tried this but it didn't work.
SELECT DISTINCT *
FROM Orders
WHERE PmtType NOT IN ('VISA','DISC','FUNDING','DEALER CHECK','MC'
,'AMEX','BONUS POOL','DLR CK - NET30'
,'WIRE','MO','EXCHANGE','ONLINE','NULL')
AND OrderDate BETWEEN '2014-03-01 00:00:00'
and '2015-03-01 00:00:00'
AND CompanyId IN ('1311','8390','8394','8396','8397','8399','3966',
'8407','8408','8315','8411','8413','8414','8416'
,'8419','4850','8426','8428','8429','8430')
What I'm trying to get is this. Companies that receive Free Demos. Which the PmtType would be free, but never purchased a product.
If the customer never purchase a product the customer Id shouldn't appear in the
PmtType IN ('VISA','DISC','FUNDING','DEALER CHECK'
,'MC' ,'AMEX','BONUS POOL','DLR CK - NET30'
,'WIRE','MO','EXCHANGE','ONLINE','NULL')
if i read the question correctly you want to know which IDs have only 1 order, this will do it. I used generic field name as you didnt specify what "value" you want to find...
EDIT: added the NOT EXISTS clause after OP Comment, you may no longer need the group by, that is up to you...
SELECT CompanyId --add fields here as needed.
,Count(*) [Occurences]
FROM Orders o
WHERE PmtType = 'FREE'
AND NOT EXISTS (SELECT CompanyId
FROM Orders io
WHERE o.CompanyId = io.CompanyId
AND PmtType <> 'FREE' )
GROUP BY CompanyId --add fields here as needed.
HAVING Count(*) = 1 --leave this out to see how many free demos each company got.

soql select individual CaseComment with all its FeedComments

I am trying to select all comments ,feeds and feedcomments for an individual case.
The hierarchy is like
Case
|
CaseComment
|
FeedComments(commnets or feeds under a CaseComment)
I could not find any relation between CaseComments and FeedComments nor CaseComments and CaseFeeds.
How can I select all together in a soql or individual soqls which relates Case, CaseComment,CaseFeed,FeedComment?
EDIT
The query you've included in the comment looks good. I'd write it as something like that:
SELECT Id, Body, ParentId, Parent.CaseNumber, CreatedDate,
(SELECT Id, CommentBody, CommentType FROM FeedComments)
FROM CaseFeed
ORDER BY Parent.CaseNumber, CreatedDate
(this is sample output rendered in Real Force Explorer, a pretty neat tool)
If I'll click into the "2 records" bit I can drill down to the fields selected from FeedComment for "this" CaseFeed:
If your query renders differently for you (some stuff is blank) - maybe try this different editor or even go to https://workbench.developerforce.com
If only some comments contain text - they might be uploaded images for example - filter them by CommentType = 'TextComment'?
ORIGINAL
FeedComments(commnets or feeds under a CaseComment)
No, not really. FeedComment is a Chatter table that can link to many objects but CaseComment is not one of them.
Maybe study the Chatter Entity Relationship Diagram?
Anyway - relationship to feed* objects doesn't have a nice name exposed so we can't query it all in one go:
I think you'll need something like this:
SELECT Id, CaseNumber,
(SELECT Id, CommentBody FROM CaseComments),
(SELECT Id, Body FROM Feeds)
FROM Case
SELECT Id, FeedItemId, ParentId, CommentBody
FROM FeedComment
WHERE ParentId = :caseIdHere

Resources