If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Running Query off the results of another query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-04, 15:07
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Running Query off the results of another query

Hi, I have gotten results from two queries. I am not very good at writing queries, so i was wondering if there is any way to combine the two queries and treat them as tables and run another query off them.
They can be stored as different queries, but I was wondering if the code could be cleaned up so it looks better.

The queries are below.
Any help will be appreciated.
Thanx in adv.
Amrit


Query One (This is saved as New OPDs in Access)

SELECT District.Description AS District, OPD.ForMonth, Sum(OPD.New) AS SumOfNew
FROM District INNER JOIN (Institute INNER JOIN OPD ON Institute.InstID = OPD.InstID) ON District.DistID = Institute.DistID
GROUP BY District.Description, OPD.ForMonth
HAVING (((OPD.ForMonth)="12/2003"));

Query two (This is saved as Cost of Medicines Issued in Access)

SELECT District.Description AS District, MedStock.ForMonth, Sum(MedStock.Issued*Medicines.Cost) AS CostIssued
FROM Medicines INNER JOIN ((District INNER JOIN Institute ON District.DistID = Institute.DistID) INNER JOIN MedStock ON Institute.InstID = MedStock.InstID) ON Medicines.MedID = MedStock.MedID
GROUP BY District.Description, MedStock.ForMonth
HAVING (((MedStock.ForMonth)="12/2003"));


The Final Query.


SELECT [New OPDs].District, [New OPDs].ForMonth, [Cost of Medicines Issued].CostIssued, [New OPDs].SumOfNew AS OPDs, [Cost of Medicines Issued].CostIssued/[New OPDs].SumOfNew AS CostPerOPD
FROM [Cost of Medicines Issued] INNER JOIN [New OPDs] ON ([Cost of Medicines Issued].District=[New OPDs].District) AND ([Cost of Medicines Issued].ForMonth=[New OPDs].ForMonth);
Reply With Quote
  #2 (permalink)  
Old 02-08-04, 15:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
cleaned up to look better? no, but they might run better if you moved the HAVING conditions to WHERE conditions
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-08-04, 22:46
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
If I move the HAVING statements to WHERE, will I still be able to group the data using GROUP BY?

Also, is there any utility that can clean up the SQL or any tutorial?

Tanshu
Reply With Quote
  #4 (permalink)  
Old 02-08-04, 22:55
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Select dept_id, count(*)
from table
group by dept_id
having count(*) > 10;

In the above example, the condition is applied to each individual group as opposed to the entire set.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 02-08-04 at 22:57.
Reply With Quote
  #5 (permalink)  
Old 02-08-04, 23:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally posted by tanshu
If I move the HAVING statements to WHERE, will I still be able to group the data using GROUP BY?
yes
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 02-09-04, 23:17
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
I just tried and the WHERE statement does work with GROPY BY. Silly me.
Thanx for the help.
Reply With Quote
  #7 (permalink)  
Old 02-09-04, 23:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
WHERE does work with GROUP BY

i do it all. the. time.

can i see your query?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-09-04, 23:30
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
This is not the same query, but this query does use WHERE along with GROUP BY.

SELECT District.Description AS District, MedStock.ForMonth, Sum(MedStock.Issued*Medicines.Cost) AS CostIssued
FROM Medicines INNER JOIN ((District INNER JOIN Institute ON District.DistID=Institute.DistID) INNER JOIN MedStock ON Institute.InstID=MedStock.InstID) ON Medicines.MedID=MedStock.MedID
WHERE (((Institute.InstType)<>'Q'))
GROUP BY District.Description, MedStock.ForMonth
HAVING (((MedStock.ForMonth)="12/2003"));
Reply With Quote
  #9 (permalink)  
Old 02-10-04, 00:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
change it to

WHERE Institute.InstType<>'Q'
AND MedStock.ForMonth="12/2003"
GROUP BY District.Description, MedStock.ForMonth
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 02-11-04, 01:35
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Thanx, I'll try that
Reply With Quote
  #11 (permalink)  
Old 02-18-04, 19:54
Bullschmidt Bullschmidt is offline
Guru
 
Join Date: Jun 2003
Location: USA
Posts: 1,032
And here's another subquery example if that might hopefully help at all:

Example of one query (QueryB) based on the results of another query (QueryA):

QueryA = "SELECT CustID FROM tblCUSTOMERS WHERE CustName = 'A%'"

QueryB = "SELECT CustID, CustName FROM tblCUSTOMERS WHERE CustID IN (" & QueryA & ")"

But the following is even faster and allows for more than one field to be returned in QueryA:

QueryB = "SELECT tblCUSTOMERS.CustID, CustName FROM (" & strSQLA & ") AS tblSQLA INNER JOIN tblCUSTOMERS ON tblSQLA.CustID = tblCUSTOMERS.CustID"

So QueryA would include all the CustID's for customers starting with A.

And QueryB would include more fields in the customers table (i.e. not just the CustID field) for the records returned in QueryA (which was the customers starting with A).

I suppose it wouldn't hurt to always use LEFT JOIN's in QueryB and build from the tblSQLA on the left to other tables that have fields you want to return.
__________________
J. Paul Schmidt, Freelance Web and Database Developer
www.Bullschmidt.com
Access Database Sample, Web Database Sample, ASP Design Tips
Reply With Quote
  #12 (permalink)  
Old 02-19-04, 14:25
tanshu tanshu is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Thanx a TON Bullschmidt, this is exactly wht I have been looking for.
Reply With Quote
  #13 (permalink)  
Old 02-19-04, 17:57
Bullschmidt Bullschmidt is offline
Guru
 
Join Date: Jun 2003
Location: USA
Posts: 1,032
You're welcome and glad it helped!
__________________
J. Paul Schmidt, Freelance Web and Database Developer
www.Bullschmidt.com
Access Database Sample, Web Database Sample, ASP Design Tips
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On