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
Related
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 4 years ago.
Improve this question
Ok, basically I've got a lot of temp tables I've created and I'm trying to create Validation for the ProvDiff table.
DROP TABLE #ProvDiff;
IF OBJECT_ID ('temp.dbo.#ProvDiff') IS NOT NULL
DROP TABLE #ProvDiff;
SELECT *
INTO #ProvDiff
FROM
(SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]) ProvDiff;
SELECT DISTINCT COUNT(DISTINCT ???) AS 'Unique EI NPIS'
FROM #ProvDiff
In my head it seems like the differences should be able to produce a result and I should be able to do a count on that. But for the life of me I can't figure out how to do that. If I do a count on rendering or pay to then those numbers wouldn't necessarily reflect the value for what are above. I know how many of each are produced for above validation.
Any help would be greatly appreciated
Is this what you want?
SELECT COUNT(*)
FROM (SELECT DISTINCT *
FROM #finalclaimswithflags f
WHERE f.[Pay-To Prov NPI] <> f.[Rendering Prov NPI]
) ProvDiff;
I don't see why a temporary table would be used for this.
For better or worse, SQL Server does not support select count(distinct *), so you pretty much need a subquery.
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 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 8 years ago.
Improve this question
I have 2 tables
Players
Pnr
Pname
Padress
Pcity
Tickets
Tnr
Pnr
Date
Costs
I want to get the name of the player with the highest Ticket Cost from the database with Select. And I want to know who has got a Ticket in May. I also want to know who has never got a Ticket.
How do I get each of these?
Pnr is the primary key of Players and is connected with Pnr from Tickets
I've tried
SELECT MAX(Costs) from Players, Tickets
Where max()
I hope i understand correctly. Try the following query. I think it will still have some errors, but you can comment them here and I will try to correct them.
select top 1 Pname from Players
inner join Tickets on Players.Pnr = Tickets.Pnr
where Date > 1.05.2014 and Date <31.05.2014
order desc by Tickets.Costs
For highest ticket cost:
SELECT P.*,T.Tnr,T.Date,T.cost
FROM Players P JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Cost= SELECT MAX(Cost) from Tickets
For player who never got a ticket:
SELECT P.*
FROM Players P LEFT JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Tnr IS NULL
AND T.Pnr IS NULL
AND T.Date IS NULL
AND T.Costs IS 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 9 years ago.
Improve this question
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER