473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help me with this query.

Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo
Nov 12 '05 #1
5 1872
MLH
Because I am seeking the help of this forum, I feel obliged to try
to lend a hand. I don't think a simple suggestion relating to using
TopValues property setting is gonna do it here. I think the difficulty
of what you're trying to accomplish warrants the skills of someone
like John Winterbottom. John's an absolute whiz at this stuff.

I will offer this strategy... sometimes, you can develop a series of
one or more queries (using the QBE design grid tool) that meet
your objective, then, displaying each of them in SQL view, you
can cut 'n paste the SQL for each of them into a single query &
accomplish what you want. I don't know if that will work for you.
You may have already tried it. Its worth fooling around with a bit
just to see if it works for you.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx

On 10 Oct 2003 11:46:24 -0700, pa*****@hispavista.com (Pablo) wrote:
Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo


Nov 12 '05 #2
pa*****@hispavista.com (Pablo) wrote in news:e9**************************@posting.google.c om:
Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo


Assuming you don't mind that the members be the first alphabetically in the list:

SELECT aa.PCPID, aa.PCPName, count(bb.PCPID) as SampleNum
FROM tblPCP as aa, tblPCP as bb
WHERE aa.PCPID = bb.PCPID and aa.PCPName >= bb.PCPName
GROUP BY aa.PCPID, aa.PCPName
HAVING count(bb.PCPID) <= 10

On a member table with 7800 rows and with indexes on PCPID and PCPName, this shouldn't take more
than 5 sec to run.

--
Ross Presser -- rpresser AT imtek DOT com
.... seeking a new quote ...
Nov 12 '05 #3
Ross Presser <rp******@NOSPAM.imtek.com.invalid> wrote in message news:<Xn**********************@129.250.170.82>...
pa*****@hispavista.com (Pablo) wrote in news:e9**************************@posting.google.c om:
Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo


Assuming you don't mind that the members be the first alphabetically in the list:

SELECT aa.PCPID, aa.PCPName, count(bb.PCPID) as SampleNum
FROM tblPCP as aa, tblPCP as bb
WHERE aa.PCPID = bb.PCPID and aa.PCPName >= bb.PCPName
GROUP BY aa.PCPID, aa.PCPName
HAVING count(bb.PCPID) <= 10

On a member table with 7800 rows and with indexes on PCPID and PCPName, this shouldn't take more
than 5 sec to run.

Thanks for reply. I am thinking a join query(maybe nested join query)
between tblPCP and tblMembers would do the work. But the join between
tblPCP itself would return the tblPCP itself.
BTW, I want to correct myself: tblMembers has around 78000 members
instead of 7800 members.
Thanks again for all of you.
Pablo
Nov 12 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'd try something like this - I haven't tested this:

SELECT P.PCPName, M.MemberName
FROM tblPCP As P INNER JOIN tblMembers As M
ON P.PCPID = M.PCPID
WHERE M.MemberName In (SELECT TOP 10 MemberName FROM tblMembers WHERE
PCPID = P.PCPID)
ORDER BY 1, 2

The part that gets the 10 Members per doctor is in the WHERE clause.
It "says" "Use the first 10 Member records you find for the current
doctor ID (PCPID = P.PCPID)." If you had other criteria to determine
which Members should be choosen, it would go in the subquery's WHERE
clause:

....WHERE PCPID = P.PCPID AND DateOfBirth Between #1/1/1951# And
#12/31/1951#)

It would probably be best if you used a unique MemberID instead of the
MemberName for the main query WHERE clause - 'cuz some MemberNames may
be the same. E.g.

SELECT P.PCPName, M.MemberName
FROM tblPCP As P INNER JOIN tblMembers As M
ON P.PCPID = M.PCPID
WHERE M.MemberID In (SELECT TOP 10 MemberID FROM tblMembers WHERE
PCPID = P.PCPID)
ORDER BY 1, 2

- --
MGFoster:::mgf
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP4rwhoechKqOuFEgEQI1mQCdHduZBRPqPahqGO1jRtZ9ac DuMTsAn0y3
a3lwlb3EWxcUbxHkt0teOiVg
=ikuw
-----END PGP SIGNATURE-----
Pablo wrote:
Ross Presser <rp******@NOSPAM.imtek.com.invalid> wrote in message news:<Xn**********************@129.250.170.82>...
pa*****@hispavista.com (Pablo) wrote in news:e9**************************@posting.google.c om:

Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo


Assuming you don't mind that the members be the first alphabetically in the list:

SELECT aa.PCPID, aa.PCPName, count(bb.PCPID) as SampleNum
FROM tblPCP as aa, tblPCP as bb
WHERE aa.PCPID = bb.PCPID and aa.PCPName >= bb.PCPName
GROUP BY aa.PCPID, aa.PCPName
HAVING count(bb.PCPID) <= 10

On a member table with 7800 rows and with indexes on PCPID and PCPName, this shouldn't take more
than 5 sec to run.


Thanks for reply. I am thinking a join query(maybe nested join query)
between tblPCP and tblMembers would do the work. But the join between
tblPCP itself would return the tblPCP itself.
BTW, I want to correct myself: tblMembers has around 78000 members
instead of 7800 members.
Thanks again for all of you.
Pablo


Nov 12 '05 #5
MGFoster <me@privacy.com> wrote in message news:<dg*****************@newsread4.news.pas.earth link.net>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'd try something like this - I haven't tested this:

SELECT P.PCPName, M.MemberName
FROM tblPCP As P INNER JOIN tblMembers As M
ON P.PCPID = M.PCPID
WHERE M.MemberName In (SELECT TOP 10 MemberName FROM tblMembers WHERE
PCPID = P.PCPID)
ORDER BY 1, 2

The part that gets the 10 Members per doctor is in the WHERE clause.
It "says" "Use the first 10 Member records you find for the current
doctor ID (PCPID = P.PCPID)." If you had other criteria to determine
which Members should be choosen, it would go in the subquery's WHERE
clause:

Thank you very much. It works perfectly. You saved me a lot of time.
Have a good day.
Pablo ...WHERE PCPID = P.PCPID AND DateOfBirth Between #1/1/1951# And
#12/31/1951#)

It would probably be best if you used a unique MemberID instead of the
MemberName for the main query WHERE clause - 'cuz some MemberNames may
be the same. E.g.

SELECT P.PCPName, M.MemberName
FROM tblPCP As P INNER JOIN tblMembers As M
ON P.PCPID = M.PCPID
WHERE M.MemberID In (SELECT TOP 10 MemberID FROM tblMembers WHERE
PCPID = P.PCPID)
ORDER BY 1, 2

- --
MGFoster:::mgf
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP4rwhoechKqOuFEgEQI1mQCdHduZBRPqPahqGO1jRtZ9ac DuMTsAn0y3
a3lwlb3EWxcUbxHkt0teOiVg
=ikuw
-----END PGP SIGNATURE-----
Pablo wrote:
Ross Presser <rp******@NOSPAM.imtek.com.invalid> wrote in message news:<Xn**********************@129.250.170.82>...
pa*****@hispavista.com (Pablo) wrote in news:e9**************************@posting.google.c om:
Hello, there,
I have two tables:
tblPCP: it has 108 doctors name and affiliation number.
tblMembers: it has 7800 members name and PCP affiliation number.
Let's say tblPCP has two fields: PCPName, PCPID
tblMembers has two fields: MemberName, PCPID

Now I want to get 10 members associated with each PCP. Say DoctorA has
seen 98 members, but I only want to get 10 sample members; DoctorB has
seen 456 members, and I only want 10 sampel members...
The result should have 108*10=1080 records.
Does anyone know how to get the result in ONE query?
Thanks in advance.
Pablo

Assuming you don't mind that the members be the first alphabetically in the list:

SELECT aa.PCPID, aa.PCPName, count(bb.PCPID) as SampleNum
FROM tblPCP as aa, tblPCP as bb
WHERE aa.PCPID = bb.PCPID and aa.PCPName >= bb.PCPName
GROUP BY aa.PCPID, aa.PCPName
HAVING count(bb.PCPID) <= 10

On a member table with 7800 rows and with indexes on PCPID and PCPName, this shouldn't take more
than 5 sec to run.


Thanks for reply. I am thinking a join query(maybe nested join query)
between tblPCP and tblMembers would do the work. But the join between
tblPCP itself would return the tblPCP itself.
BTW, I want to correct myself: tblMembers has around 78000 members
instead of 7800 members.
Thanks again for all of you.
Pablo

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
17
by: Liam.M | last post by:
Hey guys, Forgive me if my question my be alittle silly, but I would very much appreciate and assistance that could be given! My situation is as follows: I have created a Button, and set...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.