How To Get Column Description - sql

For example, in a [emp] table, the columns are :
emp_id emp_name emp_role
If the values being inserted in emp_role column values can be 0 (for Administrator), 1 (for Management), 2 (for Employees).
Now is there any way to get those details of a column emp_role (like, 0 for Administrator) along with the table concerned (i.e, [emp]) in SQL server database ?
Thanks.

If you have dictionary table with role definitions it will be something similar to:
select e.emp_id, e.emp_name, r.name
from emp e
inner join role r on e.emp_role = r.id
if not, but you know role names it will be something similar to:
select emp_id, emp_name,
case emp_role when 0 then 'Administrator' when 1 then 'Management' when 2 then 'Employees' end as RoleName
from emp

Related

Recursive SQL query for finding matches

I have 5 SQL Tables with the following columns:
tbl_department:
department_id, parent_id
tbl_employee
employee_id, department_id
tbl_department_manager
department_id, employee_manager_id
tbl_request_regular_employee
request_id, employee_id
tbl_request_special_employee
request_id, employee_id
As input data I have employee_id and request_id.
I need to figure out whether the employee has access to the request (whether he's a manager or not)
We cannot use ORM here since app's responsiveness is our priority and the script might be called a lot.
Here's the logic I want to implement:
First we query to tbl_department_manager based on employee_id to check whether the current employee is a manager or not (also the employee can be a manager in a few departments). If so, we get a list of department_id (if nothing is found, just return false)
If we got at least one id in tbl_department_manager we query to tbl_request_regular_employee AND tbl_request_special_employee based on request_id and get employee_id from both tables (they are the same)
Based on employee_id collected above we query to tbl_employee to get a unique list of department_id that the employee belongs to.
Finally have a list of unique department_id from p.3 which we can compare to the one (ones) that we got in p.1.
The catch is, however, that in tbl_department there might be departments which inherit from the one (ones) that we got from p.1 (so we might need to find it recursively based on parent_id until we find at least one match with one element from p.1). If there's at least one match between one element in p.1 and one element in p.3 return true. So there's a need to look for it recursively.
Could someone give a clue how to implement it in MSSQL? Any help would be greatly appreciated.
declare #employee_id int, #request_id int;
with reqEmployees as (
select regular_employee_id as employee_id
from tbl_request_regular_employee
where request_id = #request_id
union all --concatenate the two tables
select special_employee_id
from tbl_request_special_employee
where request_id = #request_id
),
cte as (
select e.department_id, null as parent_id
from reqEmployees r
join tbl_employee e on e.employee_id = r.employee_id -- get these employees' departments
union all
select d.department_id, d.parent_id
from cte -- recurse the cte
join tbl_department d on d.department_id = cte.parent_id -- and get parent departments
)
-- we only want to know if there is any manager row, so exists is enough
select case when exists (select 1
from cte --join on managers
join tbl_department_manager dm on dm.department_id = cte.department_id
where dm.employee_manager_id = #employee_id)
then 1 else 0 end;

Optional condition in Oracle 10g

For a dummy test, I want to show a list of employees in a web form.
There is a drop down on the web form that contains a short list of departments, like this:
All Depts
Sales Dept
Marketing Dept
Communication Dept
HR Dept
Finance Dept
IT Dept
The drop down item of All Depts has a value of 0.
The following fiddle shows you what I am trying to do:
http://sqlfiddle.com/#!4/59d1f/2
I know I can do this:
IF (deptid = 0) THEN
select firstname, lastname from employees;
ELSE
select firstname, lastname from employees where deptid = :p_deptid
END IF;
But my real situation has a much more convoluted select query that involves joins of multiple tables. So, I don't wanna clutter up my script with repetitive codes.
Can I achieve my goal using CASE WHEN? Or do I have to use dynamic SQL?
Thanks.
SELECT firstname, lastname
FROM employees
WHERE 0 = :p_deptid
OR dept_id = :p_deptid

SQL: Check if entry exists in one table to another and remove duplicates

The same question actually as this one:
sql: check if entry in table A exists in table B
Here are my tables, Employee and User
Employee User
EmpNo EmpNo
PositionCode
I just want to check if the EmpNo in table:Employee already exists in table:User
here's the code that ive used:
SELECT Employee.EmpNo, PositionCode
FROM Employee
WHERE NOT EXISTS (SELECT 1
FROM User
WHERE User.EmpNo= Employee.EmpNo)
Now with that query it displays the result,
But it also displays duplicate rows of PositionCode.
eg:
EmpNo PositionCode
E098 ER1
E712 ER1
E990 ER1
So yeah, I just need to get the PositionCode(in this ex its ER1) so I can display it to a dropdownList, and you dont want to display same items in a dropdownlist right?
I am still not sure what you want. But from my understanding you want distinct PositionCode which have EmpNo related to them. If that's what you want you can just select Distinct PositionCode as below.
SELECT DISTINCT PositionCode
FROM Employee
WHERE NOT EXISTS (SELECT 1
FROM User
WHERE User.EmpNo= Employee.EmpNo)
Let me know if this is not what you are looking for.
SELECT distinct PositionCode
FROM Employee
WHERE NOT EXISTS (SELECT 1
FROM User
WHERE User.EmpNo= Employee.EmpNo)

How to get records from both tables using ms access query

I have 2 Tables in Ms Access
tbl_Master_Employess
tbl_Emp_Salary
I want to show all the employees in the employee table linked with employee salary table
to link both table the id is coluqEmpID in both table
In the second table, I have a date column. I need a query which should fetch records from both tables using a particular date
I tried the following query:
select coluqEID as EmployeeID , colEName as EmployeeName,"" as Type, "" as Amt
from tbl_Master_Employee
union Select b.coluqEID as EmployeeID, b.colEName as EmployeeName, colType as Type, colAmount as Amt
from tbl_Emp_Salary a, tbl_Master_Employee b
where a.coluqEID = b.coluqEID and a.colDate = #12/09/2013#
However, it shows duplicates.
Query4
EmployeeID EmployeeName Type Amt
1 LAKSHMANAN
1 LAKSHMANAN Advance 100
2 PONRAJ
2 PONRAJ Advance 200
3 VIJAYAN
4 THIRUPATHI
5 VIJAYAKUMAR
6 GOVINDAN
7 TAMILMANI
8 SELVAM
9 ANAMALAI
10 KUMARAN
How would I rewrite my query to avoid duplicates, or what would be a different way to not show duplicates?
The problem with your query is that you are using union when what you want is a join. The union is first going to list all employees with the first part:
select coluqEID as EmployeeID , colEName as EmployeeName,"" as Type, "" as Amt
from tbl_Master_Employee
and then adds to that list all employee records where they have a salary with a certain date.
Select b.coluqEID as EmployeeID, b.colEName as EmployeeName, colType as Type,
colAmount as Amt
from tbl_Emp_Salary a, tbl_Master_Employee b
where a.coluqEID = b.coluqEID and a.colDate = #12/09/2013#
Is your goal to get a list of all employees and only display salary information for those who have a certain date? Some sample data would be useful. Assuming the data here: SQL Fiddle this query should create what you want.
Select a.coluqEID as EmployeeID, colEName as EmployeeName,
b.colType as Type, b.colAmount as Amt
FROM tbl_Master_Employees as a
LEFT JOIN (select coluqEID, colType, colAmount FROM tbl_EMP_Salary
where colDate = '20130912') as b ON a.coluqEID = b.coluqEID;
The first step is to create a select that will get you just the salaries that you want by date. You can then perform a join on this as if you were performing a separate query. You use a LEFT JOIN because you want all of the records from one side, the employees, and only the records that match your criteria from the second side, your salaries.
I believe you will need a join, however as to your question on Unique names.
select **DISTINCT** coluqEID as EmployeeID
Adding the distinct operator would give only uniquely returned results.

group by for Manager and Reps in Employee table

I have a table Employees with Managers and Reps in it. I would want a stored procedure that can get Manager 1 (alphabetic order) and then all Reps under that Manager1 (in alphabetic order) and then Manager 2 and all Reps under the Manager2.
There is a Column 'Manager' for all Employees with EmployeeID of Manager in that column for each Rep and null for Managers. And there is also a column ismanager which will be 1 for Managers and 0 for reps.
I tried doing it but the logic is somewhere incorrect. this is in SQL Server 2005.
Thank you in advance!!
Select * from Employees
groupby IsManager, EmployeeID
What you want to do is order by the manager id and then order by ismanager desc.
Since the manager does not have his own id in the manager column you have use a case statement to fix that. Here are two examples of how to do it in SQL:
WITH fixup AS
(
SELECT CASE MANAGER = 0 THEN EMPLOYEEID ELSE MANAGER END as ManGroup, *
FROM Employees
)
SELECT * from fixup
ORDER BY ManGroup, ismanager DESC
or
SELECT *
FROM Employees
ORDER BY CASE MANAGER = 0 THEN EMPLOYEEID ELSE MANAGER END, ismanager DESC
nb -- not tested might have typos.
select case when isManager=1 then man.name else 'Unmanaged' end as managerName,
emp.name
from employees emp left join employees man on emp.manager = man.employeeId
order by case when isManager=1 then 0 else 1 end,
case when isManager=1 then man.name else 'Unmanaged' end,
emp.name