SQL query based on a column in parent table - Parent child relationship - sql

I have the following three TABLES(ACCOUNTS,CUSTOMER,EMPLOYEE) and I would like to join them based on the columns AGENT_CODE & AGENT_TYPE and achieve the below.
What should be the best way to join these tables when AGENT_CODE can be same in CUSTOMER & EMPLOYEE table?
I have this query which is giving me wrong results
SELECT ac.AGENT_CODE,
ac.WORKING_AREA,
ac.AGENT_TYPE,
CONCAT(c.FIRST_NAME,c.LASTNAME_NAME),
e.EMP_NAME
FROM ACCOUNTS ac,
CUSTOMER c,
EMPLOYEE e
WHERE ac.AGENT_CODE = e.AGENT_CODE
OR ac.AGENT_CODE = c.AGENT_CODE
GETTING_WRONG_RESULTS_WITH_THE_ABOVE_QUERY
+------------+--------------------+------------+--------------+--------------+
| AGENT_CODE | WORKING_AREA | AGENT_TYPE | CUSTOMER_NAME| EMP_NAME |
+------------+--------------------+------------+--------------+--------------+
| A007 | Bangalore | CUSTOMER |Walter Holmes |Walter Holmes |
| A007 | London | EMPLOYEE |Walter Holmes |Peter Sam |
| A008 | New York | CUSTOMER |Micheal Junior|Micheal Junior|
| A007 | Bangalore | EMPLOYEE |Walter Holmes |John Tyler |
| A010 | Chennai | CUSTOMER |Micheal |Micheal |
| A007 | San Jose | EMPLOYEE |Walter Holmes |Albert |
+------------+--------------------+------------+--------------+--------------+
Expecting Result
+------------+--------------------+------------+--------------+
| AGENT_CODE | WORKING_AREA | AGENT_TYPE | AGENT_NAME |
+------------+--------------------+------------+--------------+
| A007 | Bangalore | CUSTOMER |Walter Holmes |
| A003 | London | EMPLOYEE |Peter Sam |
| A008 | New York | CUSTOMER |Micheal Junior|
| A011 | Bangalore | EMPLOYEE |John Tyler |
| A010 | Chennai | CUSTOMER |Micheal |
| A012 | San Jose | EMPLOYEE |Albert |
+------------+--------------------+------------+--------------+
ACCOUNTS(AGENT_CODE -PrimaryKey)
+------------+--------------------+------------+
| AGENT_CODE | WORKING_AREA | AGENT_TYPE |
+------------+--------------------+------------+
| A007 | Bangalore | CUSTOMER |
| A003 | London | EMPLOYEE |
| A008 | New York | CUSTOMER |
| A011 | Bangalore | EMPLOYEE |
| A010 | Chennai | CUSTOMER |
| A012 | San Jose | EMPLOYEE |
| A005 | Brisban | EMPLOYEE |
+------------+--------------------+------------+
CUSTOMER(AGENT_CODE -ForeignKey)
+-----------+-------------+-------------+------------+
|CUST_CODE | FIRST_NAME | LAST_NAME | AGENT_CODE |
+-----------+-------------+-------------+------------+
| C00013 | Walter | Holmes | A007 |
| C00001 | Micheal | Junior | A008 |
| C00020 | Albert | Skyler | A010 |
+-----------+-------------+-------------+------------+
EMPLOYEES(AGENT_CODE -ForeignKey)
EMP_NAME EMP_CODE AGENT_CODE
---------- --------------- ----------
Peter Sam C00054 A003
John Tyler C00023 A011
White Bolt C00043 A012

If you want to combine the result, you may want to UNION your result.
SELECT a.AGENT_CODE, a.WORKING_AREA, a.AGENT_TYPE, c.FIRST_NAME || ' ' || c.LAST_NAME AS AGENT_NAME
FROM ACCOUNTS a
JOIN CUSTOMER c ON c.AGENT_CODE = a.AGENT_CODE
UNION
SELECT a.AGENT_CODE, a.WORKING_AREA, a.AGENT_TYPE, e.EMP_NAME
FROM ACCOUNTS a
JOIN CUSTOMER e ON e.AGENT_CODE = a.AGENT_CODE

Related

Get hierarchy of all different level of managers

I'm using pgAdmin4 and I have a SQL table with employee/manager HR data that looks like this:
| employee_id | email_address | full_name | band_lvl | manager_id |
| 5592 | jillr#ex.org | Jill Rhode | 20 | 6521 |
| 6421 | racheln#ex.org | Rachel Nam | 40 | 4251 |
| 2818 | todda#ex.org | Todd Alex | 25 | 6421 |
| 4251 | jalens#ex.org | Jalen Smith | 60 | 2199 |
| 6521 | tolun#ex.org | Tolu Nagoye | 30 | 2199 |
| 7831 | jina#ex.org | Ji Na | 80 | NULL |
| 2199 | zaynm#ex.org | Zayn Mate | 70 | 7831 |
Based on the first manager_id and employee_id, I'm seeking to return the following columns: Level1 Manager Name, Level1 Manager Email, Level1 Manager Band Lvl, Level1 Manager Manager's Id. I then want to do that for each manager that's a step above, until there are no higher managers.
The desired output should look like this:
| employee_id | email_address | full_name | band_lvl | manager_id | Lvl1 Mng Nm | Lvl1 Mng Email | Lvl1 Mng Band Lvl | Lvl1 Mng Mngs Id | Lvl2 Mng Nm | Lvl2 Mng Email | Lvl2 Mng Band Lvl | Lvl2 Mng Mngs Id |
| 5592 | jillr#ex.org | Jill Rhode | 20 | 6521 | Tolu Nagoye | tolun#ex.org | 30 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 |
| 6421 | racheln#ex.org | Rachel Nam | 40 | 4251 | Jalen Smith | jalens#ex.org | 60 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 |
| 2818 | todda#ex.org | Todd Alex | 25 | 6421 | Rachel Nam | racheln#ex.org | 40 | 4251 | Jalen Smith | jalens#ex.org | 60 | 2199 |
| 4251 | jalens#ex.org | Jalen Smith | 60 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL |
| 6521 | tolun#ex.org | Tolu Nagoye | 30 | 2199 | Zayn Mate | zaynm#ex.org | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL |
| 7831 | jina#ex.org | Ji Na | 80 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2199 | zaynm#ex.org | Zayn Mate | 70 | 7831 | Ji Na | jina#ex.org | 80 | NULL | NULL | NULL | NULL | NULL |
So far, this is what I've come up with, to get the first columns for the Level 1 Manager; however, I don't know where to go from here, as I'm very new to SQL:
SELECT B.employee_id,
B.email_address,
B.full_name,
B.band_lvl,
B.manager_id,
B1.full_name AS L1_mng_nm,
B1.email_address AS L1_mng_email,
B1.band_lvl AS L1_mng_band_lvl,
B1.manager_id AS L1_mgr_mgrs_id
FROM hrdata B
INNER JOIN hrdata B1 ON
B.manager_id = B1.employee_id;
Your query is close, but you would need to make a few changes to get to your desired output. To begin, I would recommend doing a LEFT JOIN as opposed to an INNER JOIN, as the INNER JOIN will not return null values and will instead drop records that it cannot find a match for in both tables (in this case, if it cannot find a match on manager_id to employee_id from the first use of hrdata to the second use of hrdata).
After that, your query should look similar to what you have already done, just with another self-join to get the second-level manager data:
SELECT B.employee_id,
B.email_address,
B.full_name,
B.band_lvl,
B.manager_id,
B1.full_name AS L1_mng_nm,
B1.email_address AS L1_mng_email,
B1.band_lvl AS L1_mng_band_lvl,
B1.manager_id AS L1_mgr_mgrs_id,
B2.full_name AS L2_mng_nm,
B2.email_address AS L2_mng_email,
B2.band_lvl AS L2_mng_band_lvl,
B2.manager_id AS L2_mgr_mgrs_id,
FROM hrdata B
LEFT JOIN hrdata B1
ON B1.employee_id = B.manager_id
LEFT JOIN hrdata B2
ON B2.employee_id = B1.manager_id

Display the sale id and sale date of sales made by salesmen working from London

I have three tables:
Salesman Table
+-----+---------+----------+
| SID | SNAME | LOCATION |
+-----+---------+----------+
| 1 | Peter | London |
| 2 | Michael | Paris |
| 3 | John | Mumbai |
| 4 | Harry | Chicago |
| 5 | Kevin | London |
| 6 | Alex | Chicago |
+-----+---------+----------+
Sale Table
+--------+-----+-----------+
| SALEID | SID | SLDATE |
+--------+-----+-----------+
| 1001 | 1 | 01-JAN-14 |
| 1002 | 5 | 02-JAN-14 |
| 1003 | 4 | 01-FEB-14 |
| 1004 | 1 | 01-MAR-14 |
| 1005 | 2 | 01-FEB-14 |
| 1006 | 1 | 01-JUN-15 |
+--------+-----+-----------+
Expected Result
+--------+-----------+
| SALEID | SLDATE |
+--------+-----------+
| 1001 | 01-JAN-14 |
| 1002 | 02-JAN-14 |
| 1004 | 01-MAR-14 |
| 1006 | 01-JUN-15 |
+--------+-----------+
I am using Oracle SQLDeveloper. I run the code below:
SELECT S.SALEID, S.SLDATE
FROM Salesman SA
INNER JOIN Sale S ON SA.SID = S.SID
WHERE SA.LOCATION = 'London';
but I get error:
Error: Your result did not match the Expected result.
If anyone can find the errors please answer.
OKAY, I got it, it was an database error, Refreshed the database and the same Query worked.
The query mentioned is correct tho.

How to execute this query to compare dates

Write a query to display the students who are older than 'Balakrishnan'. Sort the results based on firstname in ascending order.
The output should look like this
+--------+-----------+----------+-------------+------------+-----------+
| STUDID | FIRSTNAME | LASTNAME | STREET | CITY | DOB |
+--------+-----------+----------+-------------+------------+-----------+
| 3009 | Abdul | Rahman | HAL | Bangalore | 19-JAN-88 |
| 3002 | Anand | Kumar | Indiranagar | Bangalore | 19-JAN-88 |
| 3001 | Dileep | Kumar | Jai Nagar | Bangalore | 10-MAR-89 |
| 3004 | Gowri | Shankar | Gandhipuram | Coimbatore | 22-DEC-87 |
| 3008 | John | Dravid | Mylapore | Chennai | 15-SEP-87 |
| 3006 | Prem | Kumar | Ramnagar | Coimbatore | 17-MAY-87 |
| 3007 | Rahul | Dravid | KKNagar | Chennai | 08-OCT-87 |
+--------+-----------+----------+-------------+------------+-----------+
Try this:-
It may be help you.
SELECT * FROM TABLE_NAME
WHERE DOB < TO_DATE('DOB_of_Balakrishnan','DD-MM-YYYY')
ORDER BY FIRSTNAME;
I am using oracle 11g.
As I see that the DOB of Balakrishnan is not provided...
try using this:
SELECT *
FROM table_name
WHERE dob<(SELECT dob
FROM table_name
WHERE LOWER(firstname)='bala')
ORDER BY firstname;

how to use distinct in impala

HI I am trying to query the distinct localities in my table.
Here is my query.
select distinct city,locality, avg_sqft from real_estate.re_search where city = 'bangalore' AND locality != 'jayanagar';
Result
+-----------+--------------+----------+
| city | locality | avg_sqft |
+-----------+--------------+----------+
| bangalore | bannerghatta | 13500 |
| bangalore | kormangala | 18000 |
| bangalore | kodipur | 7000 |
| bangalore | kormangala | 16000 |
| bangalore | horamavu | 9000 |
| bangalore | bellandur | 15500 |
| bangalore | kodipur | 9000 |
| bangalore | madivala | 12000 |
| bangalore | varthur | 12000 |
| bangalore | kormangala | 13500 |
| bangalore | bellandur | 13000 |
| bangalore | kodipur | 11500 |
| bangalore | kormangala | 14000 |
the problem is I need to display the distinct locality in result.any help will be appreciated.
You should be able to get a list of distinct members of the locality column in your table, where the city is Bangalore by using the COUNT and GROUP BY operators:
SELECT city
,locality
,COUNT(locality)
FROM database.table
WHERE city = 'Bangalore'
GROUP BY city
,locality;

Unusual two tables join

I have table Persons:
----------------------------------------
id | name | phone | house_id |
----------------------------------------
1 | Sarah | 1234567 | 101 |
2 | Joseph | 7654321 | 102 |
3 | David | 1231231 | null |
Ans second table Houses:
----------------------------------------
id | street | number |
----------------------------------------
101 | Evergreen Terrace | 742 |
102 | Baker Street | 223B |
103 | Oxford Street | 23A |
I need such output table as following:
--------------------------------------------------------------------------------
id(person)| name | phone | house_id | id(house) | street | number |
--------------------------------------------------------------------------------
1 | Sarah | 1234567 | 101 | 101 | Evergreen T...| 742 |
2 | Joseph | 7654321 | 102 | 102 | Baker Street | 223B |
3 | David | 1231231 | null | null | null | null |
4 | null | null | null | 103 | Oxford Street | 23A |
What kind of join do I need to use to achieve such result?
SELECT
A.id AS 'Person',
A.name,
A.phone,
A.house_id,
B.id AS 'House',
B.street,
B.number
FROM
Persons AS A
FULL OUTER JOIN Houses AS B
ON A.house_id = B.id