SQL self join sql query [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to solve below by self join
I have table like this:
Employee Name ReportTo
--------------------------------
Bob Peter
Chris Tim
ABC DEF
Peter Null
EFG Peter
One person have many employee under and there is hierarchy from top level to down. from director to employ. I am trying to create a report to fetch all employees who report to him. Is self join work here or there is any other easy way. I have another condition only top level people can see lower level.
Thanks in advance

Yes a self-join might be used
with Employees ( EmployeeName, ReportTo ) as
(
select 'Bob','Peter' union all
select 'Chris','Tim' union all
select 'ABC','DEF' union all
select 'Peter',Null union all
select 'EFG','Peter'
)
select e1.ReportTo, e1.EmployeeName
from Employees e1
left join Employees e2 on e2.EmployeeName =e1.ReportTo
where e1.ReportTo is not null
order by e1.ReportTo;
ReportTo EmployeeName
--------- ------------
DEF ABC
Peter EFG
Peter Bob
Tim Chris

Related

how to decide which can be taken as first table and second table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
This post was edited and submitted for review 4 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
To find the salesorders for all productsID along with productsID and name?
I wrote this cause the sales table also has productsID column.
select A.salesorderID, A.productsID, B.Name
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.proudctsID
But the answer I saw is
select A.productsID, A.Name, B.salesorderID
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.productsID
The decision is made based on how you want your output. The purpose of a left join is to join a table that has values you want to see to another table that MIGHT have values you want to see. The order is logical depending on which values you DON'T want to be left out of the query.
So, lets say I have a table of People with a relationship to a table of Pets.
People
ID
Name
1
John
2
Mike
3
Sue
Pets
ID
PersonID
Name
1
1
Foodo
2
2
Barrky
My organization is having a dog walking event and we want to invite all of our people. Simple, right? Done.
SELECT * FROM People;
ID
Name
1
John
2
Mike
3
Sue
But wait, its a dog walking event, so I want to include the name of these peoples Pets on the invite. No big deal.
SELECT People.*, Pets.Name AS PetName FROM People INNER JOIN Pets on Pets.PersonID = People.ID;
Result:
ID
Name
PetName
1
John
Foodo
2
Mike
Barrky
Bam, now I have invites for all of my people and their pets.
But wait! The director doesn't want to limit this to ONLY people with pets. Anyone is invited! Its a walk-a-thon and after all, not ONLY people with pets can walk and donate money to my cause. So now, I want to get ALL of my people along with their Pet's name if they have one. THIS is how you decide which order the left join should be.
SELECT People.*, Pet.Name
FROM People LEFT OUTER JOIN
Pets ON Pet.PersonID = People.ID;
Now my results are the below.
ID
Name
PetName
1
John
Foodo
2
Mike
Barrky
3
Sue
NULL
I'm guessing/assuming that B.Name is not the same as A.Name. A.Name is presumably the name of the Product (as it is coming from the Product table) whereas B.Name is the name of something in the sales order

Total salary and commission by RELATIONAL SET OPERATOR [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Employee (emp_no, emp_fname, emp_lname, emp_salary,emp_comm, job_title)
use RELATIONAL SET OPERATOR
-- Write a sql query that calculates the total salary for all employees
-- You need to add the salary and commission
-- Note that some employees don't get a commission (here commission is null)
-- You must need UNION
Please help me to find the answer.
Not sure why you need UNION to do this, Try this one
select sum(ifnull(emp_salary,0)+ifnull(emp_comm,0)) As Total
from yourtable
If you need UNION answer try this
SELECT SUM(salary)
from
(
select sum(emp_salary) As salary
from yourtable
WHERE emp_salary IS NOT NULL
UNION ALL
select sum(emp_comm)
from yourtable
WHERE emp_comm IS NOT NULL
)

I need help to write my query in SQL Server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table employee like :
name salary
jhon 5000
jaz 5000
raja 1234
rubi 1234
I need to get output like
name salary
jhon 5000
raja 1234
NAME should be anyone (jhon and jaz) OR (raja and rubi) for salary 5000,1234 respectively
There's no way to easily express "any" in SQL - you tend to have to give a rule, even if you don't care.
So,
select MIN(Name) as Name,Salary from employee group by salary
Will arbitrarily select the name that sorts earliest alphabetically.
this is one of the other way of getting result
with cte as
(
select *,ROW_NUMBER() over (partition by salary order by name)as rn from table)
select * from cte where rn=2
fiddle demo
You Question is quite incomplete including your criteria but see if this suffice your needs
select name, Salary from [Employee] where Salary = '5000'
will return
jhon 5000 and jaz 5000
select name, Salary from [Employee] where Salary = '1234'
will return
raja 1234 and rubi 1234

How to get desired result set by SQL query [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
select * from employee
UNIT_ID PARENT_UNIT TOT_EMP
------- ----------- -------
Oracle IT 10
SAP IT 20
IT PST 30
HRGA FA 5
FA PST 12
How to get output as:
IT 60
ORACLE 10
SAP 20
HRGA 5
FA 17
select a.name, sum(a.tot_emp)
from
(
select unit_id as name, sum(tot_emp) as tot_emp
from employee
group by unit_id
UNION
select parent_id as name, sum(tot_emp) as tot_emp
from employee
group by parent_id
) as a
where exists (select null
from employee e
where e.unit_id = a.name)
group by name;
But this is not a real recursive query (which seems to be what you need), it will work with your sample, but maybe not with real datas (but we don't know the deepness of your hierarchy).
i don't know some detalis. can be in the column UNIT_ID to times the same name?
if not, this is good query:
select e1.UNIT_ID, SUM(TOT_EMP) AS TOT_EMP
from emploee e1
innner join emploee e2
on e1.UNIT_ID = E2.PARENT_UNIT
group by e1.UNIT_ID
if can be same name in the column UNIT_ID, look in the answer of Raphaƫl Althaus. it's good answer.
This is Oracle query:
SELECT unit_id, SUM(total_emp) FROM stack_test
GROUP BY unit_id
/
UNIT_ID SUM(TOTAL_EMP)
-------- --------------
SAP 50
HRGA 15
ORACLE 30
It is always good idea to create tables and populate them with values in your posts...

Counting records in Access

Im having trouble coming up with a way of counting distinct record for a particular table and needing a little help with this. For example say have i have the following
FIRSTNAME LASTNAME STATE
WILL SMITH PA
JOHN DOE PA
BOB THOMAS OH
TOM JONES OH
MARK TIMMS CA
What I am looking for is a count of the distinct states in this table. Im looking for a count of 3.
Here what I have so far, but getting syntax error
SELECT COUNT(DISTINCT CONFIG) AS total
FROM TABLE_NAME
This should give you the correct count of 3:
SELECT Count(*) as Total
FROM
(
SELECT DISTINCT State
FROM yourTable
)
In MS Access you typically have to get the Distinct records first, then then the count of those records. See the below article for some tips on Distinct Counts.
Microsoft Access Tips & Tricks: Distinct Counts