473,401 Members | 2,125 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,401 software developers and data experts.

SUM and COUNT two different tables with one query

I would like to be able to sum data in one table and count data in another table with one query. Is this possible ? Below is an example of what I would like to accomplish.

tbl_class
classid | provinceID_fk | class_enroll | grade
1 ----------- 1 ---------------- 2 ----------------- 7
2 ----------- 1 ---------------- 3 ----------------- 8
3 ----------- 2 ---------------- 5 ----------------- 7
4 ----------- 2 --------------- 10 ---------------- 8
5 ----------- 2 ---------------- 5 ----------------- 7

tbl_survey
surveyID | classid_fk
1 --------------1
2 -------------- 2
3 -------------- 2
4 -------------- 2
5 -------------- 3
6 -------------- 3
7 -------------- 4
8 -------------- 4
9 -------------- 4
10 ------------- 4
11 ------------- 4
12 ------------- 4
13 ------------- 4
14 ------------- 5
15 ------------- 5

desired result
province | class_enroll | grade | survey_complete
1 -------------- 5 ------------- 7 ------------- 1
1 -------------- 5 ------------- 8 ------------- 3
2 ------------- 10 ------------- 7 ------------- 4
2 ------------- 10 ------------- 8 ------------- 7

The following will get the numbers independenly but I would like to accomplish this in one query.

SELECT SUM(class_enroll) as class_enroll
,provinceID_fk
,grade
FROM tbl_classs
GROUP BY provinceID_fk, grade

AND

SELECT COUNT(surveyID) as survey_complete
,provinceID_fk
,grade
FROM tbl_survey
GROUP BY provinceID_fk, grade

Any help would be appreciated. Thanks for your time.
Jan 28 '08 #1
3 12351
code green
1,726 Expert 1GB
You haven't explained why you want this.
But you could try UNION.
This goes where you have your AND in the post.
UNION must return equal number of fields for each query
Jan 28 '08 #2
What I want to do is have a complete report for my manager with one query. If I use the UNION as you suggest then my sum of enrollment will be in the same column as my count of completes. This would get quite confusing when I add in 10 provinces and 9 grades per province. I would also like to add a calculation statement that provides the response rate by dividing the two columns. Right now I am using two queries and then importing them into SAS and running a program to create the table that I want to provide to my manager. I was just hoping to do it in one step instead of two.
Jan 28 '08 #3
code green
1,726 Expert 1GB
Sorry about that.
A JOIN will return a common group of results
Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(class_enroll) as class_enroll,
  2. COUNT(surveyID) as survey_complete,
  3. provinceID_fk,grade
  4. FROM tbl_classs 
  5. JOIN tbl_survey ON classid = classid_fk
  6. GROUP BY provinceID_fk, grade
Not sure if this is what you mean
Jan 29 '08 #4

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

Similar topics

1
by: knoak | last post by:
Hi there, I have a mySQL DB: in it are 5000+ of products, each product has it's own categoryID. The category of a product can be 1 of 5 diferent categories. How do i count in 1 query:...
4
by: manning_news | last post by:
Using SQL2000. How do I format my select statement to choose one out of 24 different tables? Each table is slightly different and I was hoping I could use one select statement and format it...
10
by: Marizel | last post by:
I'm not sure there's an easy solution to this, but thought I'd ask. I often find myself with a query which I'd like to reuse, but with a different datasource. These datasources generally have...
2
by: Michael Hoffmann | last post by:
Hi, how can I read the count of tables and get there names from a database? cu Michael
4
by: satish | last post by:
Values of two columns in two different tables--presentation using select Hi Everyone, i have two tables in the database . One is called address table and one is adressPhone Table. Below...
2
by: sangita | last post by:
i don't know how to write module or query for the following problem in access i have two tables in one access database where id no &record no is same in two different tables, also fields a1, a2,...
1
by: empiresolutions | last post by:
Hello Fellow Programmers, I have an issue with a very large PHP page that calls over 20 different MySql Statements. This page is loading somewhat slow and i want to speed things up. I have looked...
0
by: apgoh | last post by:
Our system is on AIX5.3 ML5, DB2 v8.2 FP11, and we are running C applications. There are 2 scheduled cronjobs, which are actually triggering the same binary, but passing in different parameter....
1
by: plzansmyquery | last post by:
Hello, I am fresher to SQL Server. please help me with below query. does any one know how to get the total number of rows from two different tables in a single query. For ex: Table1 has 'x'...
6
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I have two different tables which havea different number of columns. One of the tables gets a new row every day and is populated with a st of 4 IDs from another table, along with today's date and an...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.