473,385 Members | 1,344 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,385 software developers and data experts.

Nested/Subquery assistance

Below is a query that I currently have. I need to produce a subquery
so that the top five of the CodeCount is returned. I appreciate your
help.

SELECT [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
FROM [Procedure Reference] INNER JOIN [Procedure (Px)] ON [Procedure
Reference].PX_CODE = [Procedure (Px)].PX_CODE
GROUP BY [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
[Procedure Reference].PX_TITLE
ORDER BY [Procedure (Px)].PX_SURGEON, Count([Procedure (Px)].PX_CODE)
DESC;
Nov 12 '05 #1
4 3805
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Not clear what you want. Do you want the top 5 rows of your query or
a result set with only the top 5 Px_Codes (how would you define the
top 5 PX_Codes - highest count)? Or, do you want the top 5 PX_Codes
for each surgeon? Might be a good idea if you gave an example of what
you expect the result set would look like.

If top 5 of your query just change the SELECT clause like this:

SELECT Top 5 [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
.... etc. ...

and change the ORDER BY clause like this:

ORDER BY 3 DESC

I prefer to use the column ordinal value in the ORDER BY clause rather
than "Count([Procedure (Px)].PX_CODE)," 'cuz it avoids confusion -
we're sorting by the values in column 3 rather than the results of the
function Count() - which really is the value of column 3. It's just
that I don't have to mentally parse the expression - all I have to do
is see that it will be sorted by the 3rd column.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5cK24echKqOuFEgEQL21gCePc/jf9pQfLQZnR49v7aL4METxk4AoJEc
UwOvWmPmhKOKJpJBUCP2+gKD
=/kro
-----END PGP SIGNATURE-----
Kenny G wrote:
Below is a query that I currently have. I need to produce a subquery
so that the top five of the CodeCount is returned. I appreciate your
help.

SELECT [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
FROM [Procedure Reference] INNER JOIN [Procedure (Px)] ON [Procedure
Reference].PX_CODE = [Procedure (Px)].PX_CODE
GROUP BY [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
[Procedure Reference].PX_TITLE
ORDER BY [Procedure (Px)].PX_SURGEON, Count([Procedure (Px)].PX_CODE)
DESC;


Nov 12 '05 #2
The current query gives me the total code count in desc order. I need
the top five codes for each surgeon with surgeon number in desc order.
It should look similar to this:

Surgeon PXCode CodeCount PXTitle
100 4013 3217 XXXXXXXX
100 3745 2866 XXXXXXXX
100 913 2313 XXXXXXX
100 1212 1819 XXXXXXX
100 7887 614 XXXXXXX

86 4550 5788 XXXXXX
86 377 3712 XXXXX
86 622 1712 XXXXXXXXX
86 1976 677 XXXXXXX
86 6650 76 XXXXX

I appreciate your help.

Kenny G

MGFoster <me@privacy.com> wrote in message news:<nV****************@newsread3.news.pas.earthl ink.net>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Not clear what you want. Do you want the top 5 rows of your query or
a result set with only the top 5 Px_Codes (how would you define the
top 5 PX_Codes - highest count)? Or, do you want the top 5 PX_Codes
for each surgeon? Might be a good idea if you gave an example of what
you expect the result set would look like.

If top 5 of your query just change the SELECT clause like this:

SELECT Top 5 [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
... etc. ...

and change the ORDER BY clause like this:

ORDER BY 3 DESC

I prefer to use the column ordinal value in the ORDER BY clause rather
than "Count([Procedure (Px)].PX_CODE)," 'cuz it avoids confusion -
we're sorting by the values in column 3 rather than the results of the
function Count() - which really is the value of column 3. It's just
that I don't have to mentally parse the expression - all I have to do
is see that it will be sorted by the 3rd column.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5cK24echKqOuFEgEQL21gCePc/jf9pQfLQZnR49v7aL4METxk4AoJEc
UwOvWmPmhKOKJpJBUCP2+gKD
=/kro
-----END PGP SIGNATURE-----
Kenny G wrote:
Below is a query that I currently have. I need to produce a subquery
so that the top five of the CodeCount is returned. I appreciate your
help.

SELECT [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
FROM [Procedure Reference] INNER JOIN [Procedure (Px)] ON [Procedure
Reference].PX_CODE = [Procedure (Px)].PX_CODE
GROUP BY [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
[Procedure Reference].PX_TITLE
ORDER BY [Procedure (Px)].PX_SURGEON, Count([Procedure (Px)].PX_CODE)
DESC;

Nov 12 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

That would be top 5 CodeCount per surgeon.

SELECT P.PX_SURGEON, P.PX_CODE, Count(*) AS CodeCount
FROM [Procedure (Px)] As P

WHERE PX_CODE In
( SELECT TOP 5 PX_CODE
FROM [Procedure (Px)]
WHERE PX_SURGEON = P.PX_SURGEON
GROUP BY PX_CODE
ORDER BY COUNT(*) DESC
)

GROUP BY P.PX_SURGEON, P.PX_CODE
ORDER BY 1, 3 DESC

The WHERE clause gets the top 5 PX_CODEs per surgeon. For the final
result the Count(*) As CodeCount in the main query re-counts the
PX_Codes found in the WHERE clause.

If there are ties in the WHERE clause TOP 5 the result set will show
more than 5 lines per surgeon. See the Access help article on the TOP
predicate.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5hXAoechKqOuFEgEQJUCwCg+Mrd2zQu344SzbRW4fLqBw nLxYEAn2G1
abHNQYkPtz+hgH0ZjuHe5p4N
=svBv
-----END PGP SIGNATURE-----
Kenny G wrote:
The current query gives me the total code count in desc order. I need
the top five codes for each surgeon with surgeon number in desc order.
It should look similar to this:

Surgeon PXCode CodeCount PXTitle
100 4013 3217 XXXXXXXX
100 3745 2866 XXXXXXXX
100 913 2313 XXXXXXX
100 1212 1819 XXXXXXX
100 7887 614 XXXXXXX

86 4550 5788 XXXXXX
86 377 3712 XXXXX
86 622 1712 XXXXXXXXX
86 1976 677 XXXXXXX
86 6650 76 XXXXX

I appreciate your help.

Kenny G

MGFoster <me@privacy.com> wrote in message news:<nV****************@newsread3.news.pas.earthl ink.net>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Not clear what you want. Do you want the top 5 rows of your query or
a result set with only the top 5 Px_Codes (how would you define the
top 5 PX_Codes - highest count)? Or, do you want the top 5 PX_Codes
for each surgeon? Might be a good idea if you gave an example of what
you expect the result set would look like.

If top 5 of your query just change the SELECT clause like this:

SELECT Top 5 [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
... etc. ...

and change the ORDER BY clause like this:

ORDER BY 3 DESC

I prefer to use the column ordinal value in the ORDER BY clause rather
than "Count([Procedure (Px)].PX_CODE)," 'cuz it avoids confusion -
we're sorting by the values in column 3 rather than the results of the
function Count() - which really is the value of column 3. It's just
that I don't have to mentally parse the expression - all I have to do
is see that it will be sorted by the 3rd column.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5cK24echKqOuFEgEQL21gCePc/jf9pQfLQZnR49v7aL4METxk4AoJEc
UwOvWmPmhKOKJpJBUCP2+gKD
=/kro
-----END PGP SIGNATURE-----
Kenny G wrote:

Below is a query that I currently have. I need to produce a subquery
so that the top five of the CodeCount is returned. I appreciate your
help.

SELECT [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
FROM [Procedure Reference] INNER JOIN [Procedure (Px)] ON [Procedure
Reference].PX_CODE = [Procedure (Px)].PX_CODE
GROUP BY [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
[Procedure Reference].PX_TITLE
ORDER BY [Procedure (Px)].PX_SURGEON, Count([Procedure (Px)].PX_CODE)
DESC;


Nov 12 '05 #4
Thanks - that did it. I appreciate your help.

Kenny G

MGFoster <me@privacy.com> wrote in message news:<XF*****************@newsread4.news.pas.earth link.net>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

That would be top 5 CodeCount per surgeon.

SELECT P.PX_SURGEON, P.PX_CODE, Count(*) AS CodeCount
FROM [Procedure (Px)] As P

WHERE PX_CODE In
( SELECT TOP 5 PX_CODE
FROM [Procedure (Px)]
WHERE PX_SURGEON = P.PX_SURGEON
GROUP BY PX_CODE
ORDER BY COUNT(*) DESC
)

GROUP BY P.PX_SURGEON, P.PX_CODE
ORDER BY 1, 3 DESC

The WHERE clause gets the top 5 PX_CODEs per surgeon. For the final
result the Count(*) As CodeCount in the main query re-counts the
PX_Codes found in the WHERE clause.

If there are ties in the WHERE clause TOP 5 the result set will show
more than 5 lines per surgeon. See the Access help article on the TOP
predicate.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5hXAoechKqOuFEgEQJUCwCg+Mrd2zQu344SzbRW4fLqBw nLxYEAn2G1
abHNQYkPtz+hgH0ZjuHe5p4N
=svBv
-----END PGP SIGNATURE-----
Kenny G wrote:
The current query gives me the total code count in desc order. I need
the top five codes for each surgeon with surgeon number in desc order.
It should look similar to this:

Surgeon PXCode CodeCount PXTitle
100 4013 3217 XXXXXXXX
100 3745 2866 XXXXXXXX
100 913 2313 XXXXXXX
100 1212 1819 XXXXXXX
100 7887 614 XXXXXXX

86 4550 5788 XXXXXX
86 377 3712 XXXXX
86 622 1712 XXXXXXXXX
86 1976 677 XXXXXXX
86 6650 76 XXXXX

I appreciate your help.

Kenny G

MGFoster <me@privacy.com> wrote in message news:<nV****************@newsread3.news.pas.earthl ink.net>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Not clear what you want. Do you want the top 5 rows of your query or
a result set with only the top 5 Px_Codes (how would you define the
top 5 PX_Codes - highest count)? Or, do you want the top 5 PX_Codes
for each surgeon? Might be a good idea if you gave an example of what
you expect the result set would look like.

If top 5 of your query just change the SELECT clause like this:

SELECT Top 5 [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
... etc. ...

and change the ORDER BY clause like this:

ORDER BY 3 DESC

I prefer to use the column ordinal value in the ORDER BY clause rather
than "Count([Procedure (Px)].PX_CODE)," 'cuz it avoids confusion -
we're sorting by the values in column 3 rather than the results of the
function Count() - which really is the value of column 3. It's just
that I don't have to mentally parse the expression - all I have to do
is see that it will be sorted by the 3rd column.

MGFoster:::mgf
Oakland, CA (USA)

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

iQA/AwUBP5cK24echKqOuFEgEQL21gCePc/jf9pQfLQZnR49v7aL4METxk4AoJEc
UwOvWmPmhKOKJpJBUCP2+gKD
=/kro
-----END PGP SIGNATURE-----
Kenny G wrote:
Below is a query that I currently have. I need to produce a subquery
so that the top five of the CodeCount is returned. I appreciate your
help.

SELECT [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
Count([Procedure (Px)].PX_CODE) AS CodeCount, [Procedure
Reference].PX_TITLE
FROM [Procedure Reference] INNER JOIN [Procedure (Px)] ON [Procedure
Reference].PX_CODE = [Procedure (Px)].PX_CODE
GROUP BY [Procedure (Px)].PX_SURGEON, [Procedure (Px)].PX_CODE,
[Procedure Reference].PX_TITLE
ORDER BY [Procedure (Px)].PX_SURGEON, Count([Procedure (Px)].PX_CODE)
DESC;

Nov 12 '05 #5

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

Similar topics

2
by: Jamie Townsend | last post by:
Hi. I'm having trouble filtering some nested selects. Can anyone tell me why these two statements (below) don't return the same results? The second example works as expected, but the first...
2
by: d2r2 | last post by:
Hi, I'm trying to run a nested (UNION) query against a MSAccessXP database (version 2002; build 10.6501.6714; SP3) In Access the SQL-statement executes just fine. When I run it in a asp-page I...
2
by: smauldin | last post by:
Why does the execution plan have a nested loop join for a simple select with an UDF in the where clause? Here is the query: select * from test_plan where vCol = my_udf('test') Here is the...
23
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the...
2
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
0
by: JT | last post by:
I'm using access 2000 and with assistance thought I had this problem licked. This query works: SELECT Q3.id, Q3.date, Max(Q3.Rank) AS Ninetieth FROM query3 AS Q3 WHERE...
2
by: ask | last post by:
Hi Newsgroup Hipe you can help me, I have a problem with a nested select. I have two tables; one with products and one with prices of these products: product - id, name price - pro_id, price,...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.