SQL Sub Query when joining table - sql-server

I am back to a similar issue I have had previously. It should be a simple thing, but cannot get my head around it. Here is an overview of the tables and what I am trying to achieve with this subquery. I am using SQL Server 2008.
Users
This contains a list of users
Login Audit
Contains a list of login attempts. It holds the userID and the loginDate (datetime field)
What I am trying to achieve
I want to be able to show rows of users and their last login date from the audit table.
Here is my query, that would make sense from a laymans perspective! ;-)
SELECT u.userID, u.fName, u.sName, l.loginDate
FROM Users AS u LEFT OUTER JOIN
(SELECT TOP (1) loginDate, userID
FROM LoginAudit) AS l ON l.userID = u.userID
WHERE (u.cliID = 1)
This just pulls back the last loginDatefor the last user that logged in. I wanted all of the users to come back regardless if they logged in or not.
Any assistance would be appreciated.
Thanks
nick

select a.userid, a.fName, a.sName,
max(b.loginDate)
from
users a
left outer join lastLogin b on a.userid = b.userid
group by a.userid, a.fName, a.sName

You want to replace your subquery with
SELECT userID, max(loginDate) as lastLogin FROM LoginAudit GROUP BY userID
And then replace "l.loginDate" in your main query with "l.lastLogin"
The functionality you want is provided by the MAX() function in an aggregate query, not the TOP() function.

Related

How to JOIN 2 Tables in SalesForce (SOQL)

I'm new to SalesForce and SOQL and was surprised that simple JOIN can become a real problem for me.
There are 2 Tables (Account and Intake__c) that I want to INNER JOIN. The only data I need from Account Table is Client Name (Name).
I was able to run 2 queries separately with no errors.
Account:
SELECT
a.Id,
a.Name
FROM Account AS a
Intake__c:
SELECT
i.Client_Id__c,
i.Intake_Id__c,
i.Intake_Name__c,
i.Intake_Status__c
FROM Intake__c AS i
However, when I try to join them, I get the error:
MALFORMED_QUERY: ERROR at Row:1:Column:151 unexpected token: 'JOIN'
SELECT
i.Client_Id__c,
a.Name,
i.Intake_Id__c,
i.Intake_Name__c,
i.Intake_Status__c
FROM Intake__c AS i
JOIN Account AS a
ON (i.Client_Id__c = a.Id)
SOQL syntax for joins is special, looks bit object-oriented. https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm
You probably need
SELECT Client_Id__r.Name, Intake_Id__c, Intake_Name__c, Intake_Status__c
FROM Intake__c
The "__r" bit is called relationship name and acts bit like a table alias in JOIN. You can travel via "dot" up to 5 times (see, "object-oriented").
And if you'd need a top-down approach (left outer join starting from account) it'd probably be something like
SELECT Id, Name,
(SELECT Intake_Status__c FROM Intakes__r)
FROM Account

Return multiple rows with subquery in SQL select clause

Is there a way to return multiple rows in a SQL select clause, when your subquery does not have a join/key field? My query currently looks like this. I'm wanting to return list of users and a list of contracts when there is no key between the users and contracts.
There is a handful of users, but a whole lot of contracts and I'm wanting to generate a list of each contractID next to each userID.
select
userid,
(select contractid from contracts) as contractid
from users
New here, but the suggestion for a cross join did what I wanted. thanks!
You can generate all possible combinations of users with constracts by performing a CROSS JOIN. For example:
select
u.*,
c.*
from users u
cross join contracts c
You can, then, filter the result by appending WHERE <condition> as needed.

SQL name instead of ID in many to many relation

I am trying to make simple chat with database. So I created 2 tables:
Users (ID(PK), Name, Password)
Messages (ID(PK), senderID(FK from users), recipientID(FK from users)), text
I am really noob in SQL and I just can't write query that will return table Messages but with normal names instead of senderID and recipientID. Also I didn't find examples where 1 table uses twice, so JOIN didn't work for me, or I used it in wrong way.
SELECT US.Name as SenderName, UR.Name as RecipientName, M.text
FROM Messages M JOIN Users US ON US.ID = M.senderID
JOIN Users UR ON UR.ID = M.recipientID
Not sure if I understand your question correctly, or which db you are using but this is what I think you are looking for:
SELECT Messages.text, Users.name
FROM Messages
inner JOIN Users
ON Messages.sender_id = Users.id

SQL Server query. Where user exists in another table but not the user

I have a SQL server query which I am trying to achieve but am struggling to find the right words to describe it to look on MSDN. What I have is a table of data and a table of users. I want to create a loop that for each user in dbo.Users to count the records in dbo.Data where the user is the NOT current user in loop, but is a user listed in dbo.Users.
ie.
SELECT COUNT(Records)
FROM dbo.Data
WHERE User <> 'ajones' AND (User = 'jbloggs' OR User ='jdoe')
(Where ajones is the current user and jbloggs and jdoe are users listed in dbo.Users).
So the "OR" expression will have every single user other than the user it is searching for, that exists in dbo.Users
I can't do WHERE <> 'ajones' (the current user) because there are many users in the dbo.Data table, but only some those users appear in dbo.Users.
Does this make sense? Some pseudo code:
FOR EACH User in dbo.Users
SELECT COUNT(Records)
FROM dbo.Data
WHERE User <> CurrentUser AND User IN dbo.Users
Loop
What would be the best way to achieve this kind of query?
SELECT COUNT(Records)
FROM dbo.Data
WHERE User <> 'ajones' AND User in (select distinct user from dbo.Users)
SELECT COUNT(Records)
FROM dbo.Data D
INNER JOIN dbo.Users U
ON U.User = D.User
WHERE D.User <> 'ajones'

Doing a join with SOQL

Here is my SOQL problem.
Query 1:
Select
c.Date_Joined__c,
c.Email,
c.FirstName,
c.LastName,
c.regcode__c
from Contact c WHERE c.regcode__c ='XXXXXXXXX'
Query 2:
Select
p.Account__c,
p.Date__c,
p.Points__c,
p.Description__c,
p.Code__c
from Points__c p where p.Account__c ='YYYYYYYYYYYY' and (p.Points__c > 0)
Order by p.Date__c DESC
The relationship between the two queries is that c.regcode__c will have the same value as p.Code__c.
I want to combine Query1 and Query2, so c.regcode__c = p.Code__c
I'm stuck, I can't seem to get the syntax right for SOQL.
Is it even possible to do joins in the API?
You can't really create a join per se but you can do some filtering using a syntax similar to this:
SELECT Id FROM Contact WHERE c.RegCode__c IN (SELECT p.Code__c FROM Account)
As long as the subquery in the WHERE clause is only returning a single value and codes is a filterable field this should work. Also, this doesn't work if you were trying to filter by the same object (i.e. Account to Account). You can add more criteria to the account side to match your example queries.
Again, this isn't a true join so you can't put the account fields from your subquery. But you can at least filter down your contacts.

Resources