SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 24th, 2004, 04:09 PM
dbaralo dbaralo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Maryland, USA
Posts: 3 dbaralo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question SQL Subquery Problem

I am trying to extract data from two tables in the same database and simply display a COUNT total. The code is as follows:

SELECT tbl_personnel.Assignment, COUNT(tbl_personnel.User_Name) AS Troopers_Number, (SELECT COUNT(*) FROM tbl_twelve_hours GROUP BY tbl_personnel.assignment) AS Completed_Surveys FROM tbl_personnel WHERE tbl_personnel.Region='II' AND (tbl_personnel.Rank='Trooper' OR tbl_personnel.Rank='TFC' OR tbl_personnel.Rank='Corporal' OR tbl_personnel.Rank='Sergeant') GROUP BY tbl_personnel.Assignment
I am using an ASP movenext loop to display the results. The problem is that when I run it, the subquery does not loop, it displays the same results (shown below in the "Completed Surveys" column) for all the groups. If I change the subquery GROUP BY statement to tbl_twelve_hours.assignment, I receive an error message that it can only return one record. I have been fooling with this for a few hours and am out of ideas. Any suggestions? Thanks in advance.

BARRACK ELIGIBLE TROOPERS COMPLETED SURVEYS Barrack A Waterloo 29 26 Barrack D Bel Air 50 26 Barrack F North East 47 26 Barrack L Forestville 33 26 Barrack M JFK Memorial Highway 46 26 Barrack Q College Park 29 26 Barrack R Golden Ring 40 26 STATEWIDE TOTALS 274 26

Reply With Quote
  #2  
Old May 24th, 2004, 04:43 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
1) What fields from what tables are you wanting to count?
2) What are the relationships between the two tables?
3) What database are you using?

give pseudocode for what you want the query to do and what you expect the return results to be.

Reply With Quote
  #3  
Old May 26th, 2004, 10:09 AM
dbaralo dbaralo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Maryland, USA
Posts: 3 dbaralo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Details

I am trying to count:

1 - the number of personnel assigned to each area who meet the inclusionary criteria from one table and
2 - the number of people who entered data into a second table

The two tables both contain the person's ID Number and assignment. So I need the query to pull all the people out of the first table who match the rank and assignment criteria (count #1) and then pull the number of people who have entered data into the second table (count #2 - they can only enter data once). This will allow me to determine how many people still need to complete the assignment. The results I am hoping to retrieve would look something like this (dots added since spaces are removed when I post):

Assignment..........Eligible..............Complete d
#1........................25...................... 12
#2........................48...................... 22
#3........................18...................... 18
etc.
The database is Access 2002.

Reply With Quote
  #4  
Old May 26th, 2004, 06:13 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
SELECT T.Assignment, COUNT(T.User_Name) AS Troopers_Number, (SELECT COUNT(*) FROM tbl_twelve_hours Where tbl_personnel.assignment = T.Assignment) AS Completed_Surveys FROM tbl_personnel AS T WHERE T.Region='II' AND (T.Rank='Trooper' OR T.Rank='TFC' OR T.Rank='Corporal' OR T.Rank='Sergeant') GROUP BY T.Assignment

Queries DON'T Loop
S-

Reply With Quote
  #5  
Old May 27th, 2004, 09:16 AM
dbaralo dbaralo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Maryland, USA
Posts: 3 dbaralo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Success - Thank You!!!

It worked with one minor adjustment. I had to change
"WHERE tbl_personnel.Assignment=T.Assignment...."
to
"WHERE Assignment=T.Assignment...." as shown below.

strSQL2 = "SELECT T.Assignment, COUNT(T.User_Name) AS Troopers_Number, (SELECT
COUNT(*) FROM tbl_twelve_hours WHERE assignment = T.Assignment) AS

Completed_Surveys FROM tbl_personnel AS T WHERE T.Region='II' AND

(T.Rank='Trooper' OR T.Rank='TFC' OR T.Rank='Corporal' OR T.Rank='Sergeant') GROUP

BY T.Assignment"



I greatly appreciate your time and assistance.....Doug

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > SQL Subquery Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT