How I can generate unique employee id in SQL? - 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%'

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 to use Max while taking other values from another column?

I am new in SQL and have problem picking the biggest value of a column for every manager_id and also other information in the same row.
Let me show the example - consider this table:
name
manager_id
sales
John
1
100
David
1
80
Selena
2
26
Leo
1
120
Frank
2
97
Sara
2
105
and the result I am expecting would be like this:
name
manager_id
top_sales
Leo
1
120
Sara
2
105
I tried using Max but the problem is that I must group it with manager_id and not being able to take name of the salesPerson.
select manager_id, max(sales) as top_sales
from table
group by manager_id ;
This is just an example and the actual query is very long and I am taking the information from different tables. I know that I can use the same table and join it again but the problem is as I mentioned this query is very long as I am extracting info from different tables with multiple conditions. And I don't want to make a temporary table to save it. It should be done in one single query and I actually did solve this but the query is super long due to the inner join that I used and made original table twice.
My question is that can I use Max and have the value in the name column or is there other method to solve this?
Appreciate all help
You can use row_number() with CTE to get the highest sales for each manager as below:
with MaxSales as (
select name, manager_id, sales,row_number() over (partition by manager_id order by sales desc) rownumber from table
)
select name , manager_id ,sales from MaxSales where rownumber=1

How to replace a column value for the matching records within a table in Oracle

I have a table which has some matching values of employees i.e. an employee could be in multiple departments.
I wanted to identify those records on the basis of their "name" and "dob". if it is same then replace the "id" as an increment of the decimal.
In below example: Mike is in 2 departments (IT, Finance) so I want his IT dept id (as an increment of the decimal) in the final outcome. Base id can identified on the basis department IT.
Please let me know how can I do this?
Let's take the min id and the row number divided by 10:
SELECT
MIN(id) OVER(PARTITION BY name, dob) + ((ROW_NUMBER() OVER(PARTITION BY name, dob ORDER BY id)-1)/10 as id,
name,
dob,
department
FROM
emp
I chose the min id for the employee as the base id. If you have a different strategy, like you want IT dept id to form the base value, instead of MIN(id) consider something like FIRST_VALUE(id) OVER(PARTITION BY ... ORDER BY case when department = 'it' then 0 else 1 end)
I agree with tim though; there seems a good deal of your question thatvis unclear poorly specified or not completely thought through. What if an emp is in 10 departments and an id conflict occurs? Generally we don't care about what an id number is so we don't change it or try to fill up gaps etc

Split a column in postgres in select query

I have a column with multiple employee id's with comma separation. Need to retrieve data with select query with where condition of that employee id's column but need to pass one id at a time. In the below employee id column have multiple id's.
SELECT lt.*
FROM leave as lt
WHERE true
and employee_id IN ($employee_id)
So, try something like this:
SELECT * FROM ... WHERE id = ANY (string_to_array(ids, ',')::integer[])

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