finding manager id from employee table - sql

I have a table data like in the below.
Emp_id Emp_name Dept_id
111 aaa 1
222 bbb 2
333 ccc 3
444 ddd 4
555 eee 5
Then i want to populate new column manager id as next emp_id from the employee table like in the below.
Emp_id Emp_name Dept_id Manager_id
111 aaa 1 222
222 bbb 2 333
333 ccc 3 444
444 ddd 4 555
555 eee 5 111
Thanks for your help in advance!

You can return the value as:
select t.*,
coalesce(lead(empid) over (order by empid),
min(empid) over ()
) as manager_id
from t;
Perhaps a select query is sufficient. Actually modifying the table is a bit tricky and the best syntax depends on the database.

Related

SQL to fetch least 2 salaries from a table department wise

How would be the SQL to fetch least 2 salaries from a table department wise ?
Sample Table:
empid salary Dept
---------------------
101 2000 aaa
102 1000 bbb
103 5000 bbb
104 8000 ccc
105 3000 aaa
106 4000 aaa
107 6000 ccc
108 7000 bbb
109 9000 ccc
Output should be like:
Dept empid salary
----------------------
aaa 101 2000
aaa 105 3000
bbb 102 1000
bbb 103 5000
ccc 104 6000
ccc 107 8000
SELECT
t.dept
,t.empid
,t.salary
FROM
#test t
WHERE
t.empid IN (
SELECT TOP 2
empid
FROM
#test
WHERE
dept = t.dept
ORDER BY
salary
)
ORDER BY
dept,empid
As was pointed out, there may be other people in dept ccc with a salary of 8000 that will be missed.
Following this blog post and supposing the table name is "samptab":
select dept, empid, salary
from samptab
where (
select count(*) from samptab as s
where s.dept = samptab.dept and s.salary <= samptab.salary
) <= 2 order by dept;
You can change the number "2" in the query row if you want more or less rows.

Oracle SQL- Fetch a particular column based on other two columns

Table 1
I_ID S_id E_id
1000 1234 123
1002 1235 12
1002 1235 13
1003 3456 234
1004 1256 236
1004 1257 236
1005 1239 236
Table2
Desc SS_id EE_id
aaaa 1234 125
bbbb 1235 13
cccc 2222 234
hhhh 4444 236
jjjj 1239 236
1.First I need to match S_id of table 1 with SS_id of Table 2 and pick the corresponding Desc
2.If the count of S_id in point 1 is greater than 1 then match S_id,E_ID of table 1 with SS_id,EE_ID of Table 2 and pick the corresponding Desc
3.When S_ID of Table 1 is not present in SS_ID of Table2 then match E_id of Table 1with EE_id of Table2 and pick the corresponding Desc
4.if count of E_id in the point 3 is greater than 1 then match S_id,E_ID of table 1 with SS_id ,EE_ID of Table 2 and pick the corresponding Desc
5.Else populate null
Output
I_ID Desc
1000 aaaa
1002 bbbb
1003 cccc
1004 null
1005 jjjj
can you help me write SQl query
looking to your result seem that you need only
select table1.I_ID
INNER JOIN table2 on table1.S_id = table2.SS_id
(otherwise try to post an example more appropriate)

Oracle SQL exclude specific type multiple rows select with exact two rows

I am trying to write oracle sql to select all emplids from table ABC
excluding the emplids with three specific roles. example is as follows -
TABLE1= ABC
EMPLID ROLE
______________________
111
Apple
111
Mango
111
Red_Apple
222
Apple
222
Orange
222
Red_Mango
222
Banana
333
Apple
333
Orange
444
Apple
444
Mango
444
Red_Mango
555
Grapes
666
Orange
666
Grapes
666
Blueberry
TABLE2 = DETAILS
EMPLID NAME EMAIL
__________________________________
111 John
info#email.com
222 Erica
info#email.com
and so on....
Basically, in above example since Apple, Mango, and Red% are the three roles
that needs to be excluded. The sql should return EMPLID and NAME for
222,333,555,and 666. It should exclude 111 and 444
I tried creating sub selects but still not working.`enter code here`. Any advice or help is
highly appreciated.
Use conditional aggregation:
SELECT t1.EMPLID,
t1.NAME,
t1.EMAIL
FROM DETAILS t1
INNER JOIN
(
SELECT EMPLID
FROM ABC
GROUP BY EMPLID
HAVING SUM(CASE WHEN ROLE = 'Apple' OR ROLE = 'Mango' OR ROLE LIKE 'Red%'
THEN 1 ELSE 0 END) < 3
) t2
ON t1.EMPLID = t2.EMPLID

how to write this query in sql-server?

This is my table structure in sql-server:
id Marks
---------- -----------
AAA 50
KKK 87
KKK 89
BBB 48
CCC 54
AAA 52
DDD 55
BBB 60
XXX 99
This is the desired output:
Name attempts Max Mark
------- ---------------- ------------
AAA 2 52
kkk 2 89
BBB 2 60
CCC 1 54
DDD 1 55
XXX 1 99
I've tried this but it seems incorrect:
SELECT
name,
count(*) as attempts,
max(marks)
FROM table_name
GROUP BY name, attempts, max_marks
Try this one:
SELECT
id AS Name,
count(id) AS attempts,
max(Marks) AS Max_Mark
FROM table_name
GROUP BY id
You were on the right track just added too much to grouping field,
SELECT id AS name
,COUNT(*) AS Attempts
,MAX(marks) as MaxMarks
FROM table_name
GROUP BY id
If your column is already part of aggregate function such as MAX or COUNT it does not need to be included in GROUP BY clause

Joining these tables

I have 3 tables as below :
Table Name :
------------
UserList
Column Name
-------------
DealerID DealerUserID
AAA 111
AAA 222
AAA 333
BBB 111
BBB 444
CCC 111
CCC 555
--
Table Name :
------------
UserInfo
Coulmns
--------
DealerUserID Name
111 John
222 James
333 Dany
444 Daniel
555 Romie
--
Table Name :
------------
CarPermitted
Coulmns
--------
DealerID DealerUserID
AAA 111
AAA 222
BBB 111
CCC 111
I want a result as below of a query which will take input as :
For DealerID = AAA
Name DealerUserID AllowedStatus
John 111 true
James 222 true
Dany 333 false
I have tried many joins as below, but could not get my desired result. Any suggestion how can I get it.
AllowedStatus is the value I need to fetch as :
If the combination of DealerID and DealerUserID from table UserList is present in CarPermitted///rest will be false..
Note : It will display all the dealeruserId belongs to one dealer
Here you go, you don't show your queries so I can say how you were going wrong
From the comments, Allowed status comes from if the record exists in the permitted table.
SELECT UF.Name, UF.DealerUserID,
CASE P.DealerID IS NULL THEN 'false' ELSE 'true' END AS AllowedStatus
FROM UserList UL
JOIN UserInfo UF ON UL.DealerUserID = UF.DealerUserID
LEFT JOIN CarPermitted P ON UL.DealerUserID = P.DealerUserID AND UL.DealerID = P.DealerID
WHERE UL.DealerID = 'AAA'