SAP HANA: How to get distinct characters from a column data value containing ',' and decrement the count - hana

Example:
doc_details table
OUTPUT:
department_id department_name count_offices office_names
101,201,204,401 b,b,c,b 4 HQ,TCM,TCM,SCM
The above example is the information of doctor A1. The table contains the information of which all departments the doctor is working in which all branches with the department_id.
Doctor A1 is working in department b in all the 3 offices(HQ,TCM,SCM) with an exception of office TCM where he works in 2 departments(b,c). This is why the office count in count_offices column is having count as 4. Each of the offices are having department where the department name is same but the department_id assigned to each department based on branch is distinct.
How to write the sql query to get the following output
department_id department_name count_offices office_names
101,201,204,401 b,c 3 HQ,TCM,SCM
The department_id column should contain all the distinct department_id of all the departments the doctor is assigned with the above output. The count_offices column value has to be decremneted based on the distinct names in office_names column

Related

On Statement Self Join

I am having trouble wrapping my head around the on statement when doing a self-join. Let's say we have the following table:
employeeid
name
managerid
salary
1
Mike
3
35000
2
Rob
1
45000
3
Todd
NULL
25000
4
Ben
1
55000
5
Sam
1
65000
I want to perform a self join to return the employee name and their manager's name.
When I perform the following self join I get an incorrect result:
SELECT E.name as Employee,M.name as Manager
FROM tblEmployees E
LEFT JOIN tblEmployees M
ON E.Employeeid=M.managerid
However, when I reverse the columns on the on statement using the query below:
SELECT E.name as Employee,M.name as Manager
FROM tblEmployees E
LEFT JOIN tblEmployees M
ON E.managerid=M.Employeeid
I get the correct answer.
Why? How do I know which columns to select in an on statement?
Here's my explanation:
The table you have is structured with each row representing an employee in the company.
You are interested in determining who is each employee's manager.
You are able to find that by joining the table on itself where the lookup values are the manager ids (managerid) and the reference column are the employee ids (employeeid).
The first query is wrong because the employeeid column is being used for the lookup values and the managerid column is being used for reference.
To get the manager of each employee you need to look use the managerid column as the lookup column and the employeeid column as the reference column.
Hope that's not too confusing!

How I can generate unique employee id in SQL?

I need to generate unique employee id like first letter of first name + first letter of last name + 1 in SQL using select statement.
For example:
If employee name is Rahul Khandekar, then Employee Id will be RK1.
If new employee joins with name Rakesh Kumar, then Employee Id will be RK2.
If new employee joins with name Krishna Pawar, then Employee Id will be KP1.
I'm using T-SQL syntax - but you could do something like this. I'm assuming it's always 2 characters and then your number (however long)
SELECT 'RK' + CONVERT(varchar(10),ISNULL(MAX(SUBSTRING(EmployeeID,3,10)),0)+1)
FROM Employees
WHERE EmployeeID LIKE 'RK%'

What happens if one query contains duplicate joins?

I have an application filter which can generate duplicate SQL queries to the result SQL like:
select * from articles
inner join users on articles.users_id = users.id
inner join users on articles.users_id = users.id
where users.name like %xxx%
The question is if a database is able to handle these duplicates or not. What happened in the database if this query comes inside? If I should remove it from the result SQL or if I can leave it as is.
This is a self join.
A self join is a regular join, but the table is joined with itself.
Example
SELECT
A.Id,
A.FullName,
A.ManagerId,
B.FullName as ManagerName
FROM Employees A
JOIN Employees B
ON A.ManagerId = B.Id
A and B are different table aliases for the same table.
The self join, as its name implies, joins a table to itself. To use a self join, the table must contain a column (call it X) that acts as the primary key and a different column (call it Y) that stores values that can be matched up with the values in Column X. The values of Columns X and Y do not have to be the same for any given row, and the value in Column Y may even be null.
Let’s take a look at an example. Consider the table Employees:
Id
FullName
Salary
ManagerId
1
John Smith
10000
3
2
Jane Anderson
12000
3
3
Tom Lanon
15000
4
4
Anne Connor
20000
5
Jeremy York
9000
1
Each employee has his/her own Id, which is our “Column X.” For a given employee (i.e., row), the column ManagerId contains the Id of his or her manager; this is our “Column Y.” If we trace the employee-manager pairs in this table using these columns:
The manager of the employee John Smith is the employee with Id 3,
i.e., Tom Lanon.
The manager of the employee Jane Anderson is the employee with Id 3,
i.e., Tom Lanon.
The manager of the employee Tom Lanon is the employee with Id 4,
i.e., Anne Connor.
The employee Anne Connor does not have a manager; her ManagerId is
null.
The manager of the employee Jeremy York is the employee with Id 1,
i.e., John Smith.
This type of table structure is very common in hierarchies. Now, to show the name of the manager for each employee in the same row, we can run the following query:
SELECT
employee.Id,
employee.FullName,
employee.ManagerId,
manager.FullName as ManagerName
FROM Employees employee
JOIN Employees manager
ON employee.ManagerId = manager.Id
which returns the following result:
Id
FullName
ManagerId
ManagerName
1
John Smith
3
Tom Lanon
2
Jane Anderson
3
Tom Lanon
3
Tom Lanon
4
Anne Connor
5
Jeremy York
1
John Smith
The query selects the columns Id, FullName, and ManagerId from the table aliased employee. It also selects the FullName column of the table aliased manager and designates this column as ManagerName. As a result, every employee who has a manager is output along with his/her manager’s ID and name.
In this query, the Employees table is joined with itself and has two different roles:
Role 1: It stores the employee data (alias employee).
Role 2: It stores the manager data (alias manager).
By doing so, we are essentially considering the two copies of the Employees table as if they are two distinct tables, one for the employees and another for the managers.
You can find more about the concept of the self join in our article "An illustrated guide to the SQL self join".

Displaying the same fields AS different names from the same table -Access 2010

I have EmployeeName; it is from the Employee table. The Employee table holds ALL Employees in the organization and the Employee table references the primary key of the Position table, which holds the different position names. So this is how I'm differentiating between employees in the Employee table; each record in the Employee table has a PosNo which references the Position table(worker = Pos1, manager = Pos2, etc...) So for simplicity's sake, a record in the employee table would be similar to: EmployeeName, EmployeeAddress, DeptNo, PosNo
Here's the problem: Certain positions are under other positions. There are workers in the Employee table and there are managers in the Employee table. I'm wanting to make a table that lists all workers and their managers. For example, the table would have two fields: EmployeeName, ManagerName.
The Employee table is broken down into a generalization hierarchy. The Salary and Hourly tables branch out from Employee table. Then, from the Salary table, another table branches out called Manager(which I call ProgramSupervisor; it has a unique field). Workers are part of the Hourly table though. Managers(ProgramSupervisor) and Workers(Hourly) are related to each other through the ISL table. The Manager is the head of the ISL and therefore ISL has a ManagerNo as one of its fields. Workers(Hourly), however, work in the ISL and therefore have ISLNo as a field in their table, the Hourly table.
So, I'm trying to find a way to relate all of these table as make a table with two fields, workers and managers, in which the workers belong to managers through the ISL table. Would I use a nested query of some sort? I'll post my code so far, which is absolutely not correct (probably no even on the right track) and I'll post my ERD so you can get a better picture of how the tables relate.
SELECT EmpLastName + ', ' + EmpFirstName as ProgSupName,
EmpLastName + ', ' + EmpFirstName as EmpName
FROM Employee, Salary, ProgramSupervisor, ISL, Hourly
WHERE Employee.EmpNo = Salary.EmpNo
AND Salary.EmpNo = ProgramSupervisor.EmpNo
AND ProgramSupervisor.EmpNo = ISL.ProgramSupervisor_EmpNo
AND ISL.ISLNo = Hourly.ISLNo
AND Hourly.EmpNo = Employee.EmpNo
ERD
In its simplest form you can distill the Employee-Supervisor relationship down to three tables:
[Employee]
EmpNo EmpFirstName EmpLastName
----- ------------ -----------
1 Montgomery Burns
2 Homer Simpson
[Hourly]
EmpNo ISLNo
----- -----
2 1
[ISL]
ISLNo ProgramSupervisor_EmpNo ISLName
----- ----------------------- -------------------------
1 1 Springfield Nuclear Plant
If you put them together in a query that looks like this
it produces results like this:
Employee_LastName Employee_FirstName ISLName Supervisor_LastName Supervisor_FirstName
----------------- ------------------ ------------------------- ------------------- --------------------
Simpson Homer Springfield Nuclear Plant Burns Montgomery
"But wait a minute!" I hear you say, "There are four tables in that query. Where did the [Supervisor] table come from?"
That is just another instance of the [Employee] table that uses [Supervisor] as its alias. A table can appear in a query more than once provided that we use aliases to specify the instance to which we are referring when we talk about [EmpLastName], [EmpFirstName], etc..
The SQL for the above query shows the second instance Employee AS Supervisor on the second-last line:
SELECT
Employee.EmpLastName AS Employee_LastName,
Employee.EmpFirstName AS Employee_FirstName,
ISL.ISLName,
Supervisor.EmpLastName AS Supervisor_LastName,
Supervisor.EmpFirstName AS Supervisor_FirstName
FROM
(
Employee
INNER JOIN
(
Hourly
INNER JOIN
ISL
ON Hourly.ISLNo = ISL.ISLNo
)
ON Employee.EmpNo = Hourly.EmpNo
)
INNER JOIN
Employee AS Supervisor
ON ISL.ProgramSupervisor_EmpNo = Supervisor.EmpNo

Display all managers and employees without a manager

I have a table in SQL Server 2008 that for explanation purposes contains, ID, Employee and ManagerID.
eg:
ID Employee ManagerID
1 A NULL
2 B 2
3 C 2
I want to write a query that returns all non related ManagerID's and ID's where ManagerID is equal to the ID.
The result should look like this,
ID Employee ManagerID
1 A NULL
2 B 2
In essence no managers can be managers of managers.
At first I thought that it would be simple using a SELF Join and an EXCLUDE SQL statement however I cannot get this to work. I would prefer not to USE the EXCLUDE statement as my actual table has more columns and related data that I would like to return.
If you could help, I would be grateful.
select employee, managerid
from your_table
where managerid is null
or managerid = id