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

Subquery and joined tables

I'm working on a SQL query in Access 2003 to find the average value for a given "Procedure Name."

tblPickList has the list of procedures. tblPickListItem lists each supply and quantity used in the procedure, while tblSupplies has the cost for each item.

The following code is supposed to list the procedure and average cost for the procedure. For the SubTotal column, I sum the cost of all items being used and divide by the number of procedures with an identical name. I have verified that replacing "SAMPLE NAME" with any given procedure will give me the correct number to get the average, but I can't seem to find a variable that I can use to change the number of procedures for each new record in the query.

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPickList.[Procedure Name], (Sum([Number Open]*[Cost]+[Number PRN]*[Cost]))/(SELECT Count(Physician) as PhysicianCount
  2. FROM tblPickList GROUP BY [Procedure Name] HAVING [Procedure Name] = "SAMPLE NAME" ) AS SubTotal
  3. FROM tblSupplies INNER JOIN (tblPickList INNER JOIN tblPickListItem ON tblPickList.PickListID = tblPickListItem.PickListID) ON tblSupplies.[Supply name] = tblPickListItem.[Supply name]
  4. GROUP BY tblPickList.[Procedure Name];
(I would format this correctly, but I have no idea what the normal formatting conventions for SQL are).
Apr 30 '08 #1
2 1143
Atli
5,058 Expert 4TB
Hi.

I have moved your thread from the MySQL forums over to the Access forums.
Please try to post your threads in the appropriate forum.

Thank you :)
Apr 30 '08 #2
I figured it out! I guess this shows how little experience I have at SQL. I used the following as a query:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPickList.[Procedure Name], tblPickList.Physician, tblPickList.Location, Sum([Number Open]*[Cost]+[Number PRN]*[Cost]) AS SubTotal
  2. FROM tblSupplies INNER JOIN (tblPickList INNER JOIN tblPickListItem ON tblPickList.PickListID = tblPickListItem.PickListID) ON tblSupplies.[Supply name] = tblPickListItem.[Supply name]
  3. GROUP BY tblPickList.[Procedure Name], tblPickList.Physician, tblPickList.Location;
This lists each PickList and the total for each PickList. Then I built the next query off of the above query:

Expand|Select|Wrap|Line Numbers
  1. SELECT qryPickListCost.[Procedure Name], Avg(qryPickListCost.SubTotal) AS AvgOfSubTotal
  2. FROM qryPickListCost
  3. GROUP BY qryPickListCost.[Procedure Name];
This query groups the results by Procedure, averaging the cost of each line just like I wanted.
May 1 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Haisoo Shin | last post by:
Hello.. I am working on a PHP/PEAR web page that shows statistics of how many people read a certain article during given period of time. I have, say, two tables called 'books' and 'logs'. The...
2
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
8
by: Venkata C | last post by:
Hi! Does anyone here know of a way to goad DB2 into converting a correlated subquery to a non-correlated one? Does DB2 ever do such a conversion? We have a query of the form SELECT .. FROM A...
2
by: Gellert, Andre | last post by:
Hello, I have a problem with this simple query : explain select * from ref_artikel a where a.artnr in ( 351275 , 351346 , 293082 ) LIMIT 20 OFFSET 0 ; QUERY PLAN ...
3
by: Evil Bert | last post by:
I hope someone can help with this. If so then I'd really appreciate it. I have this sql statement: SELECT * FROM affiliates_referrals, applications WHERE affiliates_referrals.application_id...
5
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
3
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: jghouse | last post by:
Everyone, I am just starting my work with subqueries and I am a little lost on how to handle the syntax on one part. I have a query that will contain some subqueries for the purpose of counting...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.