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
Related
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
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 7 years ago.
Improve this question
select
author.name,
max(count(paper.pid))
from
paper,
author
where
paper.aid=author.aid
group by
author,.name
Max of count does not make sense. Do you mean this:
select top 1 name, paper_count
from
(
select author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
)
order by paper_count desc
*this answer assumes sql server.
or (as Giorgos Betsos points out:)
select top 1 author.name,count(paper.pid) as paper_count
from paper
join author on paper.aid=author.aid
group by author.name
order by paper_count desc
http://sqlfiddle.com/#!6/7ed37/3
(Some SQL platforms won't let you order by the results of an agg function but SQL server does.)
It looks like you have a comma before "name" in your group by statement. Also, max(count()) doesn't really work or make sense. Try the below to get the author with the greatest number of papers:
select *
from(
select author.name,count(paper.pid)
from paper,author
where paper.aid=author.aid
group by author.name
order by count(paper.pid) desc
)
where rownum=1
And here is the sqlfiddle showing this in action: fiddle. Jane has 2 papers and John has 3. John shows in the results.
Alternatively you can do rownum <= x where you want the top X values.
This is an Oracle 11g solution. If using some other brands of sql you'll probably want to use Top 1/Top x I believe.
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
)
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 called teacher_student_course with teacher_id, student_id, and course_id how would I return a course_id for a course where the student count is lets say above 50?
please help my mind is shot its midnight!
You can group by the course_id and get all group having more than 50 records like this.
SELECT course_id
FROM teacher_student_course
GROUP BY course_id
HAVING COUNT(*) > 50
If you want to check if a course has more than 50 students or not, you need to use a similar query but with a JOIN as shown below.
SELECT tsc.course_id
FROM teacher_student_course tsc
INNER JOIN course ON course.id = tsc.course_id
WHERE course = 'course name'
GROUP BY tsc.course_id
HAVING COUNT(tsc.course_id)>50;
Demo for count greater than 4
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 8 years ago.
Improve this question
I have a table consisting of the following columns:
billid, patientid, doctorid, fees
How do i display the doctors who treat more than one patient?
TRIED THE FOLLOWING CODE and got it.
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
Thanks :)
SELECT doctorID
FROM YourTable
GROUP BY doctorID
HAVING COUNT (DISTINCT patientid) > 1
These are basic SQL queries. If you have trouble with something like this, you should really get to some SQL tutorial or book first.
select doctorid, count(patientid) from table1 group by doctorid having COUNT (DISTINCT patientid) > 1 ;
This will show you the doctor list having more than 1 distinct patient
on the given information if you will just select doctorid who is treating more than one patient with this query
select doctorid from tableName GROUP BY doctorId HAVING COUNT (DISTINCT patientid) > 1
and then you can use that doctorid in rest of your operations