SQlite how to display employee and manager names in this table? - sqlite

I am new to SQLite. I want to display the name of each employee and his/her manager's name. But I am a little confused about how to link the managerID with EmployeeID? Do I need to use subquery?
table: Employee
Columns: EmployeeID [pk], EmployeeName, ManagerID
Like:
EmployeeID EmployeeName ManagerID
1 Alice 2
2 Peter null
3 John 2
4 Mary 1
means Peter is Alice and John's manager and Alice is Mary's manager.
SELECT EmployeeName AS Employee, EmployeeName AS Manager
FROM Employee
WHERE

You need to join the table on itself with a LEFT OUTER join. Since the boss won't have a manager (null), use 'ifnull':
select A.EmployeeName as 'EmployeeName',
ifnull(B.EmployeeName, 'BOSS') as 'ManagerName'
from employee A left outer join employee B
on A.managerId = B.employeeId;

Related

SQLite, Filling in NULL with TEXT value

I have a table with 2 columns and 9 Rows, FirstName and LastName.
All rows of FirstName Column IS NOT NULL, but 1 row of LastName is NULL.
How would I put a value into NULL?
This is the Code I have:
SELECT Emp1.FirstName Employee, Emp2.FirstName Manager
FROM Employee Emp1
LEFT JOIN Employee Emp2 ON Emp2.EmployeeID = Emp1.ReportsTo;
Which produces:
I have tried IFNULL, but it didn't seem to work.
SELECT Emp1.FirstName Employee, Emp2.FirstName Manager, IFNULL('No Manager')
FROM Employee Emp1
LEFT JOIN Employee Emp2 ON Emp2.EmployeeID = Emp1.ReportsTo;
The syntax for ISNULL is slightly different. Try this:
SELECT Emp1.FirstName Employee, IFNULL(Emp2.FirstName, 'No Manager') as Manager
FROM Employee Emp1
LEFT JOIN Employee Emp2 ON Emp2.EmployeeID = Emp1.ReportsTo;
Please let me know if I can help you further.

How can I do join in hive with array datatype?

I have employee table which have employee id as int, employee name as string and department id as array of integer data type as the columns. Give below are the records for the Employee table.
employee5.empid employee5.empname employee5.deptid
1 Emp1 [2,3,1,4]
2 Emp2 [5,2,3,4]
3 Emp3 [1,4,2,3]
4 Emp4 [5,3,4,1]
5 Emp5 [1,2,3,4]
6 Emp6 [5,3,1,2]
7 Emp7 [3,2,4,5]
8 Emp8 [1,4,2,3]
9 Emp9 [5,2,3,1]
10 Emp10 [2,4,3,5]
I also have the department table which have department id as int, department name as string and department location as string columns. Give below are the records for the department table.
department.deptid department.deptname department.location
1 IT Delhi
2 Support Bangalore
3 HR Pune
4 Finance Mumbai
5 Call Center Mysore
As I am new to the hive, please help me in displaying the employee name and his corresponding department using the hive join.Thanks in advance.
I haven't tested however, this is might work.
First, you need to explode the array into multiple rows and then need the join, I don't think is a direct way to join.
select * from
(select empid, empname, dept_id from employee5 lateral view explode(deptid) d as dept_id) emp
join
department
on
department. deptid = emp.dept_id

select data from 2 table with where condition in asp.net

I want to check column status1 from 'studData' and status2 from 'studData1' where status1 &status2= 'Incomplete'.
I have 2 table #StudData , #StudData1
Studdata
userid fname lname mname status1
rswani suresh pqr lmn complete
root abc pqr incomplete
Studdata
userid occupation age hobbies status2
rswani student 22 reading complete
root service 21 Incomplete
I want to select fname,lname,mname from studdata where stauts1 & status 2 = 'incomplete'
I tried
cmd = new SqlCommand("select s.userid,s.fname,s.lname,s.last_exam_passed,s.course_name, s.status1, d.status2 from StudData s INNER JOIN StudData d ON s.status1= d.status2 " , con);
I am unable to select data which are incomplete from 2 table
You should join the tables with the relationship (PK & FK) and use WHERE clause to check the fields status and status1.
Here userid is the relationship between two tables, so try this:
SELECT s.userid,s.fname,s.lname,s.last_exam_passed,s.course_name, s.status1, d.status2
FROM StudData s INNER JOIN
StudData d ON s.userid= d.userid
WHERE s.status='incomplete'
AND d.status1='incomplete'

How to specify where clause with at least x values from nested select

Suppose I have these tables:
person
id name
-- ----
1 dude
2 john
3 doe
...etc
favourite_food
personid food
-------- ------
1 apples
5 apples
5 oranges
And I want to get a list of the names of people who like at least the foods that person 5 likes. Something like below:
SELECT p.name FROM person p
LEFT JOIN favourite_food ff ON ff.personid = p.id
WHERE ff.food = (SELECT food FROM favourite_food WHERE personid = 5)
AND ff.personid <> 5;
Except I have no idea how to specify the 'at least' part. Do I have to create a temporary table or so?
SQL essentially works with sets, so it often helps to reformulate your problem strictly in terms of set theory.
"At least" could be reformulated this way:
If we look only at foods that are favourite foods of person 5, which persons have the same number of favourite foods as person 5?
SELECT name
FROM person
WHERE id IN (SELECT personid
FROM favourite_food
WHERE food IN (SELECT food
FROM favourite_food
WHERE personid = 5)
GROUP BY personid
HAVING COUNT(food) = (SELECT COUNT(food)
FROM favourite_food
WHERE personid = 5)
)
Alternatively, use this reformulation:
We do not want persons who do not like a food that person 5 likes.
Therefore, find all persons for which no food exists that is liked by person 5 but not liked by that person:
SELECT name
FROM person
WHERE NOT EXISTS (SELECT 1
FROM favourite_food AS person5_food
WHERE personid = 5
AND NOT EXISTS (SELECT 1
FROM favourite_food
WHERE personid = person.id
AND food = person5_food.food)
)
(Actually, SQL is based on the relational algebra, and the operation you want is called division.)

delete values from dataset

i have an dataset with 3 tables in it they have a masterchild relationship,
lets say the dataset is like this
department table
deptID departmentname
1 IT
2 CS
3 EC
employee table
empID Empname DeptID
1 kiran 1
2 manu 2
3 kumar 3
4 ajay 3
now i have to delete value deptID =3 from department table
then all the employee who have DeptID ='3' have to be deleted from Employee table
how do we do in dataset , is there any builtin function
thanks
Prince5
You have to define a ForeignKeyConstraint and set the rule to cascade:
ForeignKeyConstraint fk = new ForeignKeyConstraint(dtDepartment.Columns["deptID"], dtEmployee.Columns["DeptID"]);
fk.DeleteRule = Rule.Cascade;
Now when you delete records in the parent it will delete them in the children as well.
You can add a constraint to you dataset and enable cascade deleting
http://msdn.microsoft.com/en-us/library/st1t2c35.aspx

Resources