get table join with column value - sql

How can i join tables using column value?
I have three tables as listed below:
messages_table
-----------------------------------------------------------------------------
msg_id | msg_sub | msg_to | to_user_type | msg_from | from_user_type
-----------------------------------------------------------------------------
001 | test | 88 | manager | 42 | admin
002 | test2 | 88 | manager | 94 | manager
admin_table
-------------------------—
admin_id | admin_name
-------------------------—
001 | Super Admin
manager_table
---------------------------
manager_id | manager_name
---------------------------
88 | Mandela
94 | Kristen
How can i get the desired output as shown below with SQL query. I.e.
Join tables with respect to column values when the following criteria is met:
If user_type = admin then it should join with admin_table.
If user_type = manager then it should join with manager_table.
Desired output:
-----------------------------------------------------
msg_id | msg_sub | msg_to_name | msg_from_name
-----------------------------------------------------
001 | test | Mandela | Super Admin
002 | test2 | Mandela | Kristen
I.e. Get the join sql query based on column value.
EDIT:
I want to fetch the datafrom sql query not form the serverside coding.
I tried this query from here, i.e. Winfred's Idea ( Answered )
However, I could not understand it.
msg_by_usertype is the column based, where the value manager then it should select manager_table and if it is admin the to admin_table

As far as I understood your question, you can try this:
SELECT msg_id,
msg_body,
usersBy.userName AS msg_by,
usersTo.userName AS msg_to,
msg_by_usertype
FROM messages
INNER JOIN
(SELECT admin_id As id, admin_name as userName
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName
FROM manager_table ) usersTo ON msg_to = usersTo.id
INNER JOIN
(SELECT admin_id As id, admin_name as userName
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName
FROM manager_table ) usersBy ON msg_by = usersBy.id
Here is an SQL Fiddle to see how it works. (It only works if you cant have an admin who has the same id like a manager. Id should be unique in both tables.)

Please use the below SQL
SELECT msg_id,
msg_body,
usersBy.userName AS msg_by,
usersTo.userName AS msg_to,
msg_by_usertype
FROM messages
INNER JOIN
(SELECT admin_id As id, admin_name as userName,'admin' as usertype
FROM admin_table
UNION
SELECT manager_id As id, manager_name as userName,'manager' as usertype
FROM manager_table ) usersTo
ON msg_to = usersTo.id and msg_by_usertype = usersTo.usertype

if I understand your question correctly, you want a result like this?
MSG_ID MSG_BODY MSG_TO BY MSG_BY_USERTYPE
---------- ---------- ---------- ----------- ---------------
001 test adm1 managone manager
002 sadff adm1 adm3? admin
If so, you could use this
SELECT MSG_ID, MSG_BODY, MSG_TO,
CASE
WHEN MSG_BY_USERTYPE = 'admin' THEN COALESCE(
(SELECT ADMIN_NAME FROM ADMIN_TABLE
WHERE MSG_BY = ADMIN_ID), RTRIM(MSG_BY) CONCAT '?')
WHEN MSG_BY_USERTYPE = 'manager' THEN COALESCE(
(SELECT MANAGER_NAME FROM MANAGER_TABLE
WHERE MSG_BY = MANAGER_ID), RTRIM(MSG_BY) CONCAT '?')
ELSE ' '
END AS BY,
MSG_BY_USERTYPE
FROM MESSAGES

Related

Update MyTable with values from AnotherTable (with self join)

I'm relatively new to SQL and currently making some practical tasks to gain experience and got struggled with an update of my custom overview table with values from another table that contains join.
I have an overview table MyTable with column EmployeeID. AnotherTable contains data of employees with EmployeeID and their ManagerID.
I am able to retrieve ManagerName using different join methods, including:
SELECT m.first_name
FROM AnotherTable.employees e LEFT JOIN
AnotherTable.employees m
on m.EmployeeID = e.ManagerID
But I am getting stuck updating MyTable, as I usually receive errors such as "single row query returns more than one row" or "SQL command not properly ended". I've read that Oracle doesnt support joins for updating tables. How can I overcome this issue? A sample data would be:
MyTable
------------------------------
EmployeeID | SomeOtherColumns| ..
1 | SomeData |
2 | SomeData |
3 | SomeData |
4 | SomeData |
5 | SomeData |
------------------------------
OtherTable
-------------------------------------
EmployeeID | Name | ManagerID |
1 | Steve | - |
2 | John | 1 |
3 | Peter | 1 |
4 | Bob | 2 |
5 | Patrick | 3 |
6 | Connor | 1 |
-------------------------------------
And the result would be then:
MyTable
-------------------------------------------
EmployeeID | SomeOtherColumns |ManagerName|
1 | SomeData | - |
2 | SomeData | Steve |
3 | SomeData | Steve |
4 | SomeData | John |
5 | SomeData | Peter |
6 | SomeData | Steve |
-------------------------------------------
As one of the options I tried to use is:
update MyTable
set MyTable.ManagerName = (
SELECT
(m.name) ManagerName
FROM
OtherTable.employees e
LEFT JOIN OtherTable.employees m ON
m.EmployeeID = e.ManagerID
)
But there I get "single row query returns more than one row" error. How is it possible to solve this?
You can use a hierarchical query:
UPDATE mytable m
SET managername = (SELECT name
FROM othertable
WHERE LEVEL = 2
START WITH employeeid = m.employeeid
CONNECT BY PRIOR managerid = employeeid);
or a self-join:
UPDATE mytable m
SET managername = (SELECT om.name
FROM othertable o
INNER JOIN othertable om
ON (o.managerid = om.employeeid)
WHERE o.employeeid = m.employeeid);
Which, for the sample data:
CREATE TABLE MyTable (EmployeeID, SomeOtherColumns, ManagerName) AS
SELECT LEVEL, 'SomeData', CAST(NULL AS VARCHAR2(20))
FROM DUAL
CONNECT BY LEVEL <= 5;
CREATE TABLE OtherTable(EmployeeID, Name, ManagerID) AS
SELECT 1, 'Alice', NULL FROM DUAL UNION ALL
SELECT 2, 'Beryl', 1 FROM DUAL UNION ALL
SELECT 3, 'Carol', 1 FROM DUAL UNION ALL
SELECT 4, 'Debra', 2 FROM DUAL UNION ALL
SELECT 5, 'Emily', 3 FROM DUAL UNION ALL
SELECT 6, 'Fiona', 1 FROM DUAL;
Then after either update, MyTable contains:
EMPLOYEEID
SOMEOTHERCOLUMNS
MANAGERNAME
1
SomeData
null
2
SomeData
Alice
3
SomeData
Alice
4
SomeData
Beryl
5
SomeData
Carol
Note: Keeping this data violates third-normal form; instead, you should keep the employee name in the table with the other employee data and then when you want to display the manager's name use SELECT ... FROM ... LEFT OUTER JOIN with a hierarchical query to include the result. What you do not want to do is duplicate the data as then it has the potential to become out-of-sync when something changes.
db<>fiddle here

SQL Lookup function

I have a Table that has employee info and another that has Manager assignments.
The manager info table references employee table by employee id in addition to managers.
I have been able to left join the table but I want to display the name rather than just employee ID.
SELECT CONCAT_WS(' ',[FirstName],[LastName]) AS FullName,
[EmployeeID]
,[Status]
,[LastName]
,[FirstName]
,[ManagersTbl].[ManagerIDf]
FROM [EmployeesTbl]
LEFT JOIN [ManagersTbl] on [EmployeesTbl].[EmployeeID] = [ManagersTbl].[EmployeeIDf]
WHERE Status = 'A'
Manager Table
_____________
| EmployeeIDf | ManagerIDf |
-----------------------------
001T | 005C
002J | 005C
_______________________________________________
Employee Table
______________
| EmployeeID | FirstName | LastName | Status |
-----------------------------------------------
001T | Tom | Spanks | A
002J | John | Doe | A
005C | Cruisin | Bruisin | A
_______________________________________________
End Result needed
_________________
|EmployeeID | StaffName | ManagerName |
------------------------------------------
001T | Tom Spanks | Crusin Bruisin
002J | John Doe | Crusin Bruisin
Please try this,
SELECT
EmployeeID
, CONCAT_WS(' ',A.[FirstName],A.[LastName]) AS StaffName
,CONCAT_WS(' ',B.[FirstName],B.[LastName]) AS ManagerName
FROM [EmployeesTbl] A
LEFT JOIN [ManagersTbl] M on A.[EmployeeID] = M.[EmployeeIDf]
LEFT JOIN [EmployeesTbl] B
ON M.[Manageridf]
=B.[EmployeeID]
WHERE Status = 'A'
There are numerous routes you can take, one possible solution is to join between your two tables to get your StaffName and then use an inline correlated subquery to get the ManagerName
select EmployeeId,
concat_ws(' ',FirstName,LastName) StaffName,
(select concat_ws(' ',FirstName,LastName) from EmployeeTable em where em.EmployeeID=m.ManagerIDf) ManagerName
from ManagerTable m join EmployeeTable e on e.EmployeeID=m.EmployeeIDf
where e.status='A'
you can use concatenation and join with two table by primary key employeeid.
concatenation function or + sign to concat in sql .

A better way to aggregate into a default value

For this example I have three tables (individual, business, and ind_to_business). Individual has information on people. Business has information on businesses. And ind_to_business has information on which people are linked to which business. Here are their DDL:
CREATE TABLE individual
(
ID INTEGER PRIMARY KEY,
NAME VARCHAR2(100) NOT NULL,
ENTERPRISE_ID VARCHAR2(25) NOT NULL UNIQUE
);
CREATE TABLE business
(
ID INTEGER PRIMARY KEY,
NAME VARCHAR2(100) NOT NULL,
ENTERPRISE_ID VARCHAR2(25) NOT NULL UNIQUE
);
CREATE TABLE ind_to_business
(
ID INTEGER PRIMARY KEY,
IND_ID REFERENCES individual(id),
BUS_ID REFERENCES business(id),
START_DT DATE NOT NULL,
END_DT DATE
);
I'm looking for the best way to display one row for each person. If they are linked to one business, I want to display the the business's ENTERPRISE_ID. If they are linked to more than one business, I want to display the default value 'Multiple'. They will always be linked to a business, so there is no LEFT JOIN necessary. They can also be linked to a business more than once (Leaving and coming back). Multiple records for the same business would be aggregated.
So for the following sample data:
Individual:
+----+------------+---------------+
| ID | NAME | ENTERPRISE_ID |
+----+------------+---------------+
| 1 | John Smith | 53a23B7 |
| 2 | Jane Doe | 63f2a35 |
+----+------------+---------------+
Business:
+----+----------+---------------+
| ID | NAME | ENTERPRISE_ID |
+----+----------+---------------+
| 3 | ABC Corp | 2a34d9b |
| 4 | XYZ Inc | 34bf21e |
+----+----------+---------------+
ind_to_business
+----+--------+--------+-------------+-------------+
| ID | IND_ID | BUS_ID | START_DT | END_DT |
+----+--------+--------+-------------+-------------+
| 5 | 1 | 3 | 01-JAN-2000 | 31-DEC-2002 |
| 6 | 1 | 3 | 01-JAN-2015 | |
| 7 | 2 | 3 | 01-JAN-2000 | |
| 8 | 2 | 4 | 01-MAR-2006 | 05-JUN-2010 |
| 9 | 2 | 4 | 15-DEC-2019 | |
+----+--------+--------+-------------+-------------+
I would expect the following output:
+---------+------------+------------+
| IND_ID | NAME | LINKED_BUS |
+---------+------------+------------+
| 53a23B7 | John Smith | 2a34d9b |
| 63f2a35 | Jane Doe | Multiple |
+---------+------------+------------+
Here is my current query:
SELECT DISTINCT
sub.ind_id,
sub.name,
DECODE(sub.bus_count, 1, sub.bus_id, 'Multiple') AS LINKED_BUS
FROM (SELECT i.enterprise_id AS IND_ID,
i.name,
b.enterprise_id AS BUS_ID,
COUNT(DISTINCT b.enterprise_id) OVER (PARTITION BY i.id) AS BUS_COUNT
FROM individual i
INNER JOIN ind_to_business i2b ON i.id = i2b.ind_id
INNER JOIN business b ON i2b.bus_id = b.id) sub;
My query works, but this is running on a large dataset and taking a long time to run. I'm wondering if anyone has any ideas on how improve this so that there isn't so much wasted processing (i.e Needing to do a DISTINCT on the final result or doing COUNT(DISTINCT) in the inline view only to use that value in the DECODE above).
I've also created a DBFiddle for this question. (Link)
Thanks in advance for any input.
You could try and use a correlated subquery. This removes the need for outer distinct:
SELECT
i.enterprise_id ind_id,
i.name,
(
SELECT DECODE(COUNT(DISTINCT b.enterprise_id), 1, MIN(bus_id), 'Multiple')
FROM ind_to_business i2b
INNER JOIN business b ON i2b.bus_id = b.id
WHERE i2b.ind_id = i.id
) linked_bus
FROM individual i
You can join with the aggregated ind_to_business per individual. One way to do this:
select i.id, i.name, coalesce(b.enterprise_id, 'Multiple')
from individual i
join
(
select
ind_id,
case when min(bus_id) = max(bus_id) then min(bus_id) else null end as bus_id
from ind_to_business
group by ind_id
) ib on ib.ind_id = i.id
left join business b on b.id = ib.bus_id
order by i.id;
First you should sub-query to get all needed dimensions and then do all your final aggregation using CASE statement.
select
ind_id,
name,
case
when count(*) > 1 then 'Multiple'
else ind_id
end as linked_bus
from
(
select
distinct i.enterprise_id as ind_id,
i.name,
b.enterprise_id as bus_id
from individual i
join ind_to_business i2b
on i.id = i2b.ind_id
join business b
on i2b.bus_id = b.id
) vals
group by
ind_id,
name
order by
ind_id
No need of using DISTINCT twice. You could use subquery factoring and put the in-line view in WITH clause, and make the data set DISTINCT in the subquery itself.
WITH data AS
(
SELECT distinct
i.enterprise_id AS IND_ID,
i.name,
b.enterprise_id AS BUS_ID
FROM individual i
JOIN ind_to_business i2b ON i.id = i2b.ind_id
JOIN business b ON i2b.bus_id = b.id
)
SELECT ind_id,
name,
case
when count(*) = 1 then MIN(bus_id)
else 'Multiple'
end AS LINKED_BUS
FROM data
GROUP BY ind_id, name;
IND_ID NAME LINKED_BUS
---------- ---------- -------------------------
53a23B7 John Smith 2a34d9b
63f2a35 Jane Doe Multiple

Sql query to solve the below scenario

I have a table like :
Employeeid personid reltnship status
111 125 owner active
111 252 prevown active
112 3 descsd active
I want to write a query to get all the details for the employees who have reltnship as combination of 'owner' and 'prevown'
Use Self JOIN
SELECT DISTINCT S.*
FROM Tbl S JOIN tbl T ON S.Employeeid = T.Employeeid
where S.reltnship = 'owner' AND T.reltnship = 'prevown'
Use a subquery that returns all the employeeids that satisfy the condition:
select * from tablename
where employeeid in (
select employeeid
from tablename
where reltnship in ('owner', 'prevown')
group by employeeid
having count(distinct reltnship) = 2
)
See the demo.
Results:
| Employeeid | personid | reltnship | status |
| ---------- | -------- | --------- | ------ |
| 111 | 125 | owner | active |
| 111 | 252 | prevown | active |
If you want all details, then one method uses exists:
select t.*
from t
where exists (select 1
from t t2
where t2.employeeid = t.employeeid and
t2.reltnship = 'owner'
) and
exists (select 1
from t t2
where t2.employeeid = t.employeeid and
t2.reltnship = 'prevown'
) ;
This is phrased to be index friendly. The index that you want is on (employeeid, reltnship).
If you only want the employees that meet this criteria, I would recommend aggregation:
select t.employeeid
from t
where reltnship in ('owner', 'prevown')
group by employeeid
having count(distinct reltnship) = 2;
You should study mysql query structure and what are clauses of queries.
Here is query:
select *
from tablename
where reltnship = 'owner'
or reltnship = 'prevown'

Select distinct where date is max

This feels really stupid to ask, but i can't do this selection in SQL Server Compact (CE)
If i have two tables like this:
Statuses Users
id | status | thedate id | name
------------------------- -----------------------
0 | Single | 2014-01-01 0 | Lisa
0 | Engaged | 2014-01-02 1 | John
1 | Single | 2014-01-03
0 | Divorced | 2014-01-04
How can i now select the latest status for each person in statuses?
the result should be:
Id | Name | Date | Status
--------------------------------
0 | Lisa | 2014-01-04 | Divorced
1 | John | 2014-01-03 | Single
that is, select distinct id:s where the date is the highest, and join the name. As bonus, sort the list so the latest record is on top.
In SQL Server CE, you can do this using a join:
select u.id, u.name, s.thedate, s.status
from users u join
statuses s
on u.id = s.id join
(select id, max(thedate) as mtd
from statuses
group by id
) as maxs
on s.id = maxs.id and s.thedate = maxs.mtd;
The subquery calculates the maximum date and uses that as a filter for the statuses table.
Use the following query:
SELECT U.Id AS Id, U.Name AS Name, S.thedate AS Date, S.status AS Status
FROM Statuses S
INNER JOIN Users U on S.id = U.id
WHERE S.thedate IN (
SELECT MAX(thedate)
FROM statuses
GROUP BY id);