Several lines of information to a single line - sql

I have information about customer's email address and phonenumber, but the data is stored in different tables which results in several rows per customer.
I have my main table with customerId etc. which I need to join with email and phonenumber. To do so I have a "translation" table with a communication_id which connects the tables.
For example
Main table:
CustomerID var1 var2 ...
123 1 7
The translation table which I need to use to connect the main table with the tables that includes email and phone look like this
CustomerID CommID
123 780
123 781
123 782
And the table with the email could look like this
commID email
780 a#a.com
and the table with the phone number could look like this
commID phone
781 88888
what I achieve if I left join the above 3 tables to my main table is
CustomerID var1 var2 email phone
123 1 7 a#a.com ?
123 1 7 ? 88888
123 1 7 ? ?
I understand why I get 3 rows, but what I want to achieve is a single row like this
CustomerID var1 var2 email phone
123 1 7 a#a.com 88888
Thank you
EDIT:
The join syntax is
sel * from maintable
left join Communication on maintable.CustomerID=Communication.CustomerID
left join email on email.commID=Communication.CommID
left join phone on phone.commID=Communication.CommID

To combine multiple rows into one you usually do a GROUP BY:
SELECT maintable.CustomerID, MAX(email), MAX(phone)
FROM maintable
LEFT JOIN Communication ON maintable.CustomerID=Communication.CustomerID
LEFT JOIN email ON email.commID=Communication.CommID
LEFT JOIN phone ON phone.commID=Communication.CommID
GROUP BY maintable.CustomerID

Related

SQL join To Fetch Mutiple Records

I need to find the firstName and Email address from of all teachers and parents in a single query
Here is the structure of the table :
// Patients Table
ID guid parentID PatientName
1 234 1258 John
2 xyz 111 Paul
// Patient_teacher table
ID PatiendGuid teacherid
1 122 132
2 xyz 1424
3 245 1545
4 xyz 1222
// Members table
ID guid email fname
22 123 hello#xyz.com hello
111 xyz parentEmail#xyz.com parentName
1424 343 teacherEmail#xyz.com teacherName
1222 546 teacher2EMail#xyz.com teacher2Name
And Here is the required Result:
//Required Result
fname Email
parentName parentEmail#xyz.com
techerName teacherEmail#xyz.com
teacher2Name teacher2Email#xyz.com
The problem is when I tried to search using join I found a single row that contains parentID and TeacherID
Here is what I tried:
select Members.email,Members.fname
from Members
join Patients on Members.guid = Patients.guid
join Patient_Teacher on Patient_Teacher.patientguid = Patients.guid
where patients.guid = 'xyz'
Here is the Solution :
select Members.id, Members.email, Members.fname
from Patients
join Patient_Teacher on Patient_Teacher.patientguid = Patients.guid
join Members on (Patient_Teacher.teacherid = Members.id
or Patients.parent = Members.id)
where patients.guid = 'xyz'
Have you checked the following reasons?
1- You have a table named 'Patients_teacher' but in your solution, you're referring to it as 'Patient_teacher'.
2- In 'Patients_teacher' table, you have a column named 'patiendguid' but in your solution you're referring to it as 'patientguid'.

Presto left join SQL statement to return first match only

I know this question has been asked many times before, but for the life of me I am unable to figure this out.
I'm essentially trying to do a Left Join statement that is matching a Purchase Order number in table 1 with a Purchase order number in table 2. The issue is, since PO data is updated daily, it will return numerous rows from table 2 because there are different scenarios of a PO not being paid, canceled, etc.
Where I am running into issues is, my left join statement is returning multiple rows when joining against table 2.
Table 1 = T1
POnum
Description
12345
I need help
54321
I need help
78910
I need help
Table 2 = T2
POnum
Date
Vendor
12345
1/2/21
ABC
12345
1/2/21
ABC
12345
1/2/21
ABC
54321
1/1/21
CBD
54321
1/1/21
CBD
54321
1/1/21
CBD
78910
1/5/21
GED
78910
1/5/21
GED
78910
1/5/21
GED
Here is the code that I am using:
Select
t1.POnum, T2.Vendor
From
Table 1 as T1
Left Join
Table 2 as T2 On T1.POnum = T2.POnum
As you will note, in Table 2 the PO number 12345 has 3 rows with the same exact date.
The ending result should essentially look like the following:
T1.POnum
T2.Vendor
12345
ABC
54321
CBD
78910
GED
where in query you are using the date from Table T2 ?
you have not used any where clause also in query, it means no condition is required.
The result you need can be achieved directly without any join.
SELECT DISTINCT PONUM, VENDOR FROM Table2;
Let me know in case anything is missing here.

SQL Query: Join (or select) 2 columns from 1 table with 1 column from another table for a view without extra join columns

This is my very first Stackoverflow post, so I apologize if I am not formatting my question correctly. I'm pounding my head against the wall with what I'm sure is a simple problem. I have a table with a bunch of event information, about 10 columns as so:
Table: event_info
date location_id lead_user_id colead_user_id attendees start end <and a few more...>
------------------------------------------------------------------------------------------------
2020-10-10 1 3 1 26 2100 2200 .
2020-10-11 3 2 4 18 0600 0700
2020-10-12 2 5 6 6 0800 0900
And another table with user information:
Table: users
user_id user_name display_name email phone city
----------------------------------------------------------------------
1 Joe S goofball ...
2 John T schmoofball ...
3 Jack U aloofball ...
4 Jim V poofball ...
5 Joy W tootball ...
6 George A boring ...
I want to create a view that has only a subset of the information, not full table joins. The event table lead_user_id and colead_user_id columns both refer to the user_id column in the users table.
I want to create a view like this:
date Location Lead Name CoLead Name attendees
---------------------------------------------------------------------
2020-10-10 1 Jack U Joe S 26
2020-10-11 3 John T Jim V 18
2020-10-12 2 Joy W George A 6
I have tried the following and several iterations like it to no avail...
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
U2.display_name AS CoLead Name.
E.attendees
FROM
users U1, event_info E
INNER JOIN
event_info E ON U1.user_id = E.lead_user_id
INNER JOIN
users U2 ON U2.user_id = E.colead_user_id
And I get the dreaded
You have an error in your SQL Syntax
message. I'm not surprised, as I've really only ever used joins on single columns or nested select statements... this two columns pointing to one is throwing me for a loop. Help!
correct query for this matter
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
(select display_name from users where user_id=E.colead_user_id) AS CoLead Name,
E.attendees
FROM
event_info E
INNER JOIN
users U1 ON U1.user_id = E.lead_user_id

How to inner join same table with different conditions?

I need to get contact information where employee id is null and not null. How do I join the same table with these different conditions. I need the information to populate a report with both employee information and person accompanied them to a event. Here is the query I have so far.
select events.id, (persons.firstname+' '+ persons.lastname) as employee
from events
inner join eventscontacts on events.id = eventcontacts.events_id
inner join contacts on eventcontacts.contact_id = contacts.id
inner join persons on contacts.person_id = person.id
Eventcontacts table
Id ContactType_id contact_id event_id
1 1 1 300
2 2 3 300
Contact type is 1 for employee and 2 for non emplopyees
contacts table
Id person_id employee_id
1 100 200
2 101 201
3 102 NULL
4 103 202
5 104 203
Person table
Id firstname lastname
100 John Stewart
101 Greg Larry
102 Kim Hans
103 Gloria June
104 Dan Duke
Result table
ID employee accompany
300 John Stewart Kim Hans
right now, I have information of all the employees for the event. I want the people who accompanied these person for the events. Their employee id is null in the contacts table. How do I join the contacts table again here?
An inner join will return only the rows that exist in both tables, where it seems like you want all the rows from the contacts table including the rows that don't match due to them lacking an employee id.
If you use an outer join, it will return rows that exist in contacts AND in events like an inner join but ALSO rows that ONLY exist in events and rows that ONLY exist in contacts.
In the case that I am explaining this poorly, I recommend you read this to help explain:
https://mode.com/sql-tutorial/sql-outer-joins/
If you can successfully use an outer join you will get all the visitors in one table regardless of having an id or not.

ORACLE SQL Multi Join

I want to join fields from different tables into one doing a query with joins. From now I've only joined two tables but I have difficulties merging others more. Can you help me? These are my tables:
Table Departments
------------------------------------
Department_ID Department_Name
------------------------------------
1 Sales
2 Marketing
3 Warehouse
Table Roles
---------------------------------
Role_ID
---------------------------------
1
2
3
4
Table Departments_Roles
--------------------------------------------------------------------
Dep_Role_ID Department_ID Role_ID Role_Name
--------------------------------------------------------------------
1 1 1 Admin
2 1 2 Client Attention
3 2 1 Admin
4 3 2 Client Attention
Table Employers
---------------------------------
Employer_Id Employer_Name
---------------------------------
1 John
2 Jess
3 Tom
4 George
5 David
What I want to see is:
Table Merged
-------------------------------------------------
Department_Name Employer_Name Role_Name
-------------------------------------------------
xxxxx yyyyy zzzz
This are just some example tables. Dont look for the sense of it.
I've tried using join, but I've never make something so complex.
Can you give me some advice?
Joining multiple tables is the same as joining one table, just repeated.
SELECT D.Department_Name, E.Employer_Name, DR.Role_Name
FROM Employers E --this is your base table
INNER JOIN magicalEmployerToDepartmentRoleLinkTable EDR --this connects our base table to the linking table
ON E.Employer_Id = EDR.Employer_Id
INNER JOIN Department_Roles DR --now we can pull any column in the Department_Roles table that is related back to our base table
ON ED.Department_Id = DR.Department_Id
INNER JOIN Department D --now we can pull any column in the Department table that is related back to the Department_Roles table
ON DR.Department_ID = D.Department_Id