FreeTextTable across two related tables - sql-server

I have Client table and a Contact table. A Client can have multiple contacts, contact table has a FK to a Client PK.
I need a FreeTextTable query so I can search by Client Name or Contact Name.
I've been trying to UNION FreeTextTable results from each table without much luck.
Any ideas?

Soemthing like this should work for you:
SELECT *
FROM Client A
LEFT JOIN Contact B
ON A.ClientId = B.ClientId
WHERE A.ClientName LIKE '%somename%' OR B.ContactName LIKE '%somename%'

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

SQL: Building hierarchies and nesting queries on the same table

I am trying to build hierarchies by nesting queries on the same table in MS SQL Server 2014.
To give an example of what I am trying to achieve:
I have a table 'employees' whith the following columns:
[ID],[First Name],[Last Name],[ReportsTo]
{1},{John},{Doe},{2}
{2},{Mary},{Miller},{NULL}
I am trying to build a statement, where I join the employees table with itself and where I build a hierarchy with the boss on top.
Expected Result:
[Employee],[Boss]
{Miller,Mary},{NULL}
{Doe, John},{Miller,Mary}
I apologize, if this is a stupid question, but I fail to create a working nested query.
Could you please help me with that?
Thank you very much in advance
Based on the intended results, it looks like what you essentially want is a list of employees. So let's start with that:
SELECT LastName, FirstName, ReportsTo FROM Employees
This gives you the list, so you now have the objects you're looking for. But you need to fill out more data. You want to follow ReportsTo and show data from the record to which that points as well. This would be done exactly as it would if the foreign key pointed to a different table. (The only difference from being the same table is that you must use table aliases in the query, since you're including the same table twice.)
So let's start by joining the table:
SELECT e.LastName, e.FirstName, e.ReportsTo
FROM Employees e
LEFT OUTER JOIN Employees b on e.ReportsTo = b.ID
The results should still be the same, but now you have more data to select from. So you can add the new columns to the SELECT clause:
SELECT
e.LastName AS EmployeeLastName,
e.FirstName AS EmployeeFirstName,
b.LastName AS BossLastName,
b.FirstName AS BossFirstName
FROM Employees e
LEFT OUTER JOIN Employees b on e.ReportsTo = b.ID
It's a join like any other, it just happens to be a join to the same table.

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 Joining journal tables to user tables

I have a MSSQL database with 3 tables: Journals, Customers, and UserAccounts.
I'm trying to query Journals for transactions per account manager. This table has a customer ID column that links to Customers.
The Customers table has a ACC_Manager column that links to UserAccounts via UserID.
Inside the UserAcounts table are first and last name columns.
So it would be
Select
Journal.amount,
Customer.name,
UserAccounts.first
From
Tables
where
Journal.ACC_manager = 'Matt'
I'm having issues joining the tables so I can query using UserAccounts.first. Could anybody help? Thanks
Try the following. I didn't get exact column names so don't just use this code without modifying it a bit to suit your specific needs:
SELECT
j.amount,
c.name,
u.first
FROM
Journals j
JOIN Customers c ON
c.customerID = j.customerID -- Exact column names?
JOIN UserAccounts u ON
u.UserID = c.ACC_Manager
WHERE
u.first = 'Matt'
You may also need to use LEFT JOIN as opposed to JOIN. Read up on JOINs to be sure.

Need help on a sql query to get data from multiple tables

I have a couple of tables that have data in them that I am looking to get information from. Here is the rundown....In table 1 I have bunch of columns that I am pulling data from, one of the columns is a user ID (which is a number)that was the last userID to modify a record. In table 2 I want to pull in the name of that user based on the ID that is pulled from the other table (this table has both the userID and the username).
so my final query would have the columns in table 1 as well as the username from table 2 to show that was the user to last edit the record. I assume this has to be done in a nested select statement but for the life of me I cannot come up with the correct syntax.
Can anyone help me out?
Thanks
Jeff
Yes, you need a very basic join that link both tables together.
Select t1.UserID,
t2.UserName
FROM table1 t1 INNER JOIN
table2 t2 ON t1.userid=t2.userid
select t1.*, t2.{username} from table1 as t1
join table2 as t2 on t1.{userId}=t2.{userid};
change {username} with the actual column name of user
similarly {userId} with appropriate column name in tables.
Hope it helps you.
this is standard inner join query, to learn more consider reading: http://www.w3schools.com/sql/

Resources