How to select distinct column once and blank to its similar id column - sql-server-2012

I want result like
Dept Name EmployeeCount
IT Ankit 4
Aman 4
Rakesh 4
Rohhit 4
HR Deepak 3
Manoj 3
Raj 3

You may use window function here -
SELECT CASE WHEN Dept = LAG(Dept) OVER() THEN NULL ELSE Dept END Dept
,Name
,COUNT(Name) OVER() EmployeeCount
FROM YOUR_TABLE;

Related

List records from a chosen employee to the ultimate manager

Given the following Table (In Postgres):
EmployeeID
EmployeeName
ManagerID
1
David
NULL
2
Jessica
NULL
3
Gregg
1
4
Joe
3
5
Brandon
NULL
6
Leslie
4
7
Harry
6
8
Paul
NULL
9
Frank
5
Starting from Employee ID 7 (Harry) - how do I list only the records, bottom-up, in the hierarchy until I get to the ultimate manager (which should be David, in this case)? Should I use recursive CTE? If so, how does that query look?
Expected Output:
EmployeeID
EmployeeName
ManagerID
1
David
NULL
3
Gregg
1
4
Joe
3
6
Leslie
4
7
Harry
6
In the common table expression, you can recognize:
the base step, where you select the record relatively to employee no 7
the recursive step, where you match managers with currently retrieved employees.
The recursion stops when the INNER JOIN does not return any more extra record.
WITH RECURSIVE cte AS (
SELECT * FROM tab WHERE EmployeeID = 7
UNION ALL
SELECT mgr.*
FROM cte emp
INNER JOIN tab mgr
ON emp.ManagerID = mgr.EmployeeID
)
SELECT *
FROM cte
Check the demo here.

Select query to return no records

I have Table 1 : EMP as below
ID NAME CITY AMT
-------------------------------------------
1 sajani Bangalore 20
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
Table 2: EMP1
ID NAME1 CITY1 AMT1
---------------------------------------------
1 Sajani Bangalore 10
1 Sajani Bangalore 10
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
ID is the Key and is common in both the files. I want a Select SQL statement which states table 1 = table 2 and once select query is executed, it should return 0 records.
Use MINUS operator
SELECT ID, NAME, CITY, AMT
FROM EMP
MINUS
SELECT ID, NAME1, CITY1, SUM(AMT1)
FROM EMP1
GROUP BY ID, NAME1, CITY1
Thanks, I figured out the solution
Select * from emp
where amt <>(
select sum(e1.amt1)
from emp1 e1
where e1.id =emp.id
)

SQL query to identify result using self join or any other way

Suppose I have a table like below:
ID Name ManagerID
1 Dave NULL
2 Scott 1
3 Mike 1
4 Nick 3
5 Jack 1
How Can I find out corresponding Manager Name of all the employee, something like below format?
ID Name ManagerID ManagerName
1 Dave NULL NULL
2 Scott 1 Dave
3 Mike 1 Dave
4 Nick 3 Mike
5 Jack 1 Dave
Try this!
select a.ID,a.Name,a.ManagerID,b.Name as ManagerName
from #t a left join #t b on a.ManagerID=b.ID
Demo
select ID, Name, ManagerID,
(case when managerid is null then null else (select Name from tab where id=managerid) end) as ManagerName
from tab
You can do this way too
Possible in several ways.
Such as:
SELECT
ID, Name, ManagerID,
(SELECT Name FROM table AS ManagersTable WHERE ManagersTable.ID=EmployeesTable.ManagerID) AS ManagerName
FROM table AS EmployeesTable

oracle duplicate rows based on a single column

How can I find out duplicate rows based on a single column. I have a table in oracle which has data as given below and it has duplicates. I'm trying to select and view all rows with duplicate employee ids as explained below
EMP table:
EmpId Fname Lname Mname Jobcode Status exp_date
1 Mike Jordan A IT W 12/2014
1 Mike Jordan A IT A 12/2014
2 Angela ruth C sales P 12/2015
2 Angela ruth C IT W 12/2015
3 Kelly Mike B sales W 12/2015
From the above table i want to select all rows which duplicate empids such as below
EmpId Fname Lname Mname Jobcode Status exp_date
1 Mike Jordan A IT W 12/2014
1 Mike Jordan A IT A 12/2014
2 Angela ruth C sales P 12/2015
2 Angela ruth C IT W 12/2015
How can I do this? thank you!
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT EmpID
FROM TableName
GROUP BY EmpID
HAVING COUNT(*) > 1
) b ON a.EmpID = b.EmpID
SQLFiddle Demo
Another way, although I prefer above, is to use IN
SELECT a.*
FROM TableName a
WHERE EmpId IN
(
SELECT EmpId
FROM TableName
GROUP BY EmpId
HAVING COUNT(*) > 1
)
SQLFiddle Demo
Here's another option using a subquery and COUNT OVER PARTITION BY since you're using Oracle 11:
SELECT *
FROM (
SELECT EmpId, Fname, Lname, Mname, Jobcode, Status, exp_date,
COUNT(EmpId) OVER (PARTITION BY EmpId) EmpCount
FROM TableName
) T
WHERE EmpCount > 1
SQL Fiddle Demo (Borrowed from JW)

Select unique random posting/recruitment places of employees within a list of places

I am trying to select unique random posting/recruitment places of employees within a list of places, all the employees are already posted at the places, i am trying to generate a new random posting place for them with "where" condition that "employee new random location will not be equal to their home place"
Employee table is :
EmpNo Empname CurrentPosting Home Designation RandomPosting
1 Satish Kumar Samastipur Gazi Manager
2 Anil Kumar Singh Vaishali Patna Manager
3 Rajdev Prasad Nawada Gaya PO
4 Rajesh Kumar Sheikhpura Muzaffarpur PO
5 Jitendra Kumar Banka Bhagalpur Clerk
And so on...
And Places table is
PlaceID PlaceName Manager PO Clerk
1 Araria 2 0 1
2 Arwal 1 1 1
3 Aurangabad 1 0 2
4 Banka 2 1 1
5 Begusarai 1 1 1
6 Bhagalpur 1 1 2
7 Bhojpur 0 2 0
and so on...
i tried with rand() and newid() like as below,
select Employee.*, Place.PlaceName As RandomPosting from Employee
inner join Place on Place.PlaceID=Employee.EmpNo order by newid()
But unable to select what is required... that is to assign each Employee a PlaceName(from Place) randomly which is not equal to CurrentPosting and Home(in Employee).
Thanks in advance.
WITH cteCrossJoin AS (
SELECT e.*, p.PlaceName,
ROW_NUMBER() OVER(PARTITION BY e.EmpNo ORDER BY NEWID()) AS RowNum
FROM Employee e
CROSS JOIN Place p
WHERE e.Home <> p.PlaceName
)
SELECT *
FROM cteCrossJoin
WHERE RowNum = 1;