It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Display doctorid, dname, total fees received by the doctor(s) who have treated more than one patient?
Display hospitalid, hname, htype of hospital(s) which has the highest number of doctors associated with them.
Tables
table 1 - patient
patientid
pname
address
amount
ptype
table 2 - hospital
hospitalid
hname
htype
table 3 - doctor
doctorid
dname
specialization
hospitalid
status
table 4 - billing
billingid
patientid
doctorid
fees
billdate
So far this is what I have:
select
billing.doctorid,
sum (fees) as totalfees,
doctor.dname
from
billing, doctor
where
doctor.doctorid = billing.doctorid
group by
billing.doctorid,
doctor.dname
having
min ( billing.patientid ) <> max ( billing.patientid )
I'll help you with your first question, and I'll leave to you the second.
Display doctorid, dname, total fees received by the doctor(s) who have treated more than one patient?
Let's split this problem in pieces:
So you need first to know which doctors have treated more than one patient. That information is in the table billing. So:
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
This query will return only the Ids of the doctors that have more than one patient. Notice that I'm using a subquery to deduplicate the doctor-patient tuple.
Now let's attack the other part of this question: The total fees of each doctor. Again, that info is in the table billing:
select doctorId, sum(fees) as totalFees
from billing
group by doctorId;
Finally, let's put it all together, and include the doctor's info, which is in the table doctor:
select
d.doctorId, d.doctorName, a.totalFees
from
doctor as d
inner join (
select doctorId, sum(fees) as totalFees
from billing
group by doctorId
) as a on d.doctorId = a.doctorId
inner join (
select doctorId, count(patientId) as patientCount
from (select distinct doctorId, patientId from billing) as a
group by doctorId
having count(patientId)>1;
) as b on d.doctorId = b.doctorId;
Hope this helps
Things you need to study and (or) keep in mind:
You need to understand how to relate data stored in different tables. Study how to use INNER JOIN (and also LEFT JOIN and RIGHT JOIN)
You need to understand how does GROUP BY works, and how to use aggregate functions (sum(), count(), etcetera).
You know how to write subqueries. Now try to use them not only for where conditions, but as data sources (including them in from statements)
Keep a copy of the reference manual of your RDBMS at hand. Also a good book on SQL can help you (go to a bookstore or library and find one you like).
Looks like you already got your answer, but since I wrote it up...
Select d.doctorID,
d.dName,
Sum(b.fees) [total fees received]
From doctor d
Join billing b
On d.doctorID = b.doctorID
Group By d.doctorID,
d.dName
Having Count(Distinct patientID) > 1
With CTE As
(
Select Rank() Over (Order By Count(d.doctorID) Desc) As priCount,
h.hospitalID,
h.hName,
h.hType,
Count(d.doctorID) As doctors
From hospital h
Join doctor d
On h.hospitalID = d.hospitalID
Group By h.hospitalID,
h.hName,
h.hType
)
Select hosptitalID,
hName,
hType
From CTE
Where priCount = 1
Related
i've just study SQL for a month and there is many things I'm still cannot get a hold of. Can anyone help me, plz provide the results with some explaination, I need to understand it for future uses. I only list things that I cannot understand.
List the ID of the managers and the total number of employees reporting to each of them. Display the result in descending order of the total number of employees
I can do:
SELECT employeeNumber
FROM employees
WHERE jobTitle LIKE '%Manager%'
UNION
SELECT employeeNumber, COUNT(*)
FROM employees
WHERE reportsTo 'WHICH CONDITION?'
ORDER BY COUNT(*) DESC
Can someone fill in after 'reportTo', I cant find a condition that's working TYT
Display all offices and their counts in each country
I think this mean showing a table with every country and count total number of offices in that country
I can do:
SELECT country, COUNT(*)
FROM offices
GROUP BY country
UNION
SELECT country, officeCode
FROM offices
But the results is not as expected
select
reportsTo,
COUNT(employeeNumber) as numberOfEmployees
from employees
group by reportsTo
Will give you a count of employeeNumbers that report to that reportsTo.
This will not give you the managers where nobody is reporting to, so to do that you would have to make a JOIN:
SELECT
a.employeeNumber AS managerNumber,
COUNT(b.employeeNumber) AS numberOfEmployees
FROM employees AS a
LEFT JOIN employees AS b on (b.reportsTo=a.employeeNumber)
WHERE a.jobTitle LIKE '%Manager%'
GROUP BY a.employeeNumber
I have a problem with a query.
I have one table named EMPLOYEE and there i have the fields NAME, YEAR and STATUS.
Every employee can have several years and several status but I need the status of the most recent year.
How can I get that data?
This should do it:
SELECT e.[Name],
e.[Year],
e.[Status]
FROM employee AS e
INNER JOIN
(
SELECT [Name],
MAX([Year])
FROM employee
GROUP BY [Name]
) AS m ON e.[Name] = m.[Name]
AND e.[Year] = m.[Year];
I have read answers to similar questions but I cannot find a solution to my particular problem.
I will use a simple example to demonstrate my question.
I have a table called 'Prizes' with two columns: Employees and Awards
The employee column lists the employee's ID and award shows a single award won by the employee. If an employee has won multiple awards their ID will be listed in multiple rows of the table along with each unique award.
The table would look as follows:
Employee AWARD
1 Best dressed
1 Most attractive
2 Biggest time waster
1 Most talkative
3 Hardest worker
4 Most shady
3 Most positive
3 Heaviest drinker
2 Most facebook friends
Using this table, how would I select the ID's of the employees who won the most awards?
The output should be:
Employee
1
3
For the example as both these employees won 3 awards
Currently, the query below outputs the employee ID along with the number of awards they have won in descending order:
SELECT employee,COUNT(*) AS num_awards
FROM prizes
GROUP BY employee
ORDER BY num_awards DESC;
Would output:
employee num_awards
1 3
3 3
2 2
4 1
How could I change my query to select the employee(s) with the most awards?
A simple way to express this is using rank() or dense_rank():
SELECT p.*
FROM (SELECT employee, COUNT(*) AS num_awards,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM prizes
GROUP BY employee
) p
WHERE seqnum = 1;
Being able to combine aggregation functions and analytic functions can make these queries much more concise.
You can use dense_rank to get all the rows with highest counts.
with cnts as (
SELECT employee, count(*) cnt
FROM prizes
GROUP BY employee)
, ranks as (select employee, cnt, dense_rank() over(order by cnt desc) rnk
from cnts)
select employee, cnt
from ranks where rnk = 1
This question already has answers here:
Sql query error: "From keyword missing" [closed]
(3 answers)
Closed 9 years ago.
The problem: Display hospitalid, hname, htype of hospital(s) which has the highest number of doctors associated with them.
The patient table:
patientid
pname
address
amount
ptype
The hospital table
hospitalid
hname
htype
The doctor table:
doctorid
dname
specialization
hospitalid
status
The billing table:
billingid
patientid
doctorid
fees
billdate
So far this is what I have:
select * from hospital where hospitalid =
(select hospitalid from doctor group by hospitalid having count ( doctorid ) =
(select max ( doctoramt ) from
(select count (doctorid) as doctoramt from doctor group by hospitalid) as tbltemp));
Try this but not tested
select * from hospital where hospitalid =
(select hospitalid from doctor group by hospitalid having count ( doctorid ) =
(select max ( doctoramt ) from
(select count (doctorid) as doctoramt from doctor group by hospitalid) as tbltemp)));
You can't use AS tbltemp to alias a table in Oracle. The AS keyword can only used to alias columns, not tables. You can either remove the AS keyword, or in this case since you don't refer to the alias, remove the whole AS tbltemp part. Here's an SQL Fiddle.
It looks like the parser initially tries to interpret AS as the alias name, and then doesn't know what the tbltemp is supposed to mean.
ZZa's approach is better anyway, but you could also use anayltic functions to avoid hitting the tables multiple times:
select hospitalid, hname, htype from (
select hospitalid, hname, htype, doc_count,
rank() over (order by doc_count desc) as rn
from (
select h.hospitalid, h.hname, h.htype,
count(d.doctorid) as doc_count
from hospital h
join doctor d on d.hospitalid = h.hospitalid
group by h.hospitalid, h.hname, h.htype
)
)
where rn = 1;
Another SQL Fiddle here. The innermost select does the counting, the next level ranks the grouped results in descending order of the number of doctors at each hospital, and the outermost restricts that to the highest-ranked.
Either way, if there's a tie - two hospitals with the same number of doctors - you'll get multiple rows back. If that's not what you want you'd need to decide how to break the tie.
Try this:
SELECT h.hospitalid, h.hname, h.htype
FROM hospital h, doctor d
WHERE h.hospitalid = d.hospitalid
GROUP BY h.hospitalid, h.hname, h.htype
HAVING COUNT(*) = (SELECT MAX(a) from (SELECT COUNT(*) a from doctor group by hospitalid));
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Customer Table columns:
CustomerID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax
Supplier table Columns:
SupplierID CompanyName ContactName ContactTitle Address City Region PostalCode Country Phone Fax HomePage
from these table how we find " Which country has the maximum number of suppliers and customers...?"
i need query for the above problem....?
Pls answer me any one....!
To get the maximum times a single country is included in both tables as a single value, union them, then group them:
select top 1 x.Country
from (
select Country from Customer
union all
select Country from Supplier) x
group by x.Country
order by count(1) desc
edit: alternatively, you could group the two tables separately, then full-outer-join them together, and add matching terms (remembering to handle the nulls from countries that are in only one of the two lists):
select top 1 ISNULL(x.Country, y.Country) as [Country]
from (
select Country, COUNT(1) as [Count] from Customers
group by Country) x
full outer join (
select Country, COUNT(1) as [Count] from Suppliers
group by Country) y
on x.Country = y.Country
order by ISNULL(x.[Count], 0) + ISNULL(y.[Count], 0) desc
SQL server syntax
maximum number of customers
select Country from (
(with cte as (select Country,COUNT(*) cnt
from Customer
group by Country)
select Country,Rank() over (order by cnt desc) row_num from cte)a
where a.row_num=1
maximum number of suppliers
select Country from (
(with cte as (select Country,COUNT(*) cnt
from suppliers
group by Country)
select Country,Rank() over (order by cnt desc) row_num from cte)a
where a.row_num=1
SELECT a.country AS MaxCustomers, b.country AS MaxSuppliers
FROM
(
SELECT TOP 1 country
FROM customers
GROUP BY country
ORDER BY COUNT(*) DESC
) a
CROSS JOIN
(
SELECT TOP 1 country
FROM suppliers
GROUP BY country
ORDER BY COUNT(*) DESC
) b
Should output something like:
MaxCustomers | MaxSuppliers
---------------------------------
USA | Japan