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 > What's wrong with my SQL Query??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-04, 15:21
norm801 norm801 is offline
Registered User
 
Join Date: Jan 2004
Posts: 100
What's wrong with my SQL Query??

I'm using the following code:

SELECT teRManager, count(*) as ItemCount FROM Time_Entry GROUP BY teRManager

and I seem to be getting numerous repeats when my goal is to get a total for each distinct instance of each item. It seems to work when I run it for other fields in the same table. Strange......

Here is what I get:

teRManager ItemCount
Bruce Amman 59
Jeff Mort 6275
James Monroe 2556
James Monroe;Jill Manners 38
Jill Manners 4
James Monroe;Jill Manners 17
Jill Manners 1698
Jill Manners;James Monroe 1
Jill Manners 28
Jill Manners;James Monroe 1
Jill Manners 63
Jill Manners;James Monroe 2
Jill Manners 90
Jill Manners;James Monroe 1

Here is what I'm looking for:

teRManager ItemCount
Bruce Amman 59
Jeff Mort 6275
James Monroe 2556
James Monroe;Jill Manners 55
Jill Manners;James Monroe 5
Jill Manners 1883

(I'm using Access to pull the info from a Lotus Notes database)

Thanks!

Norm
Reply With Quote
  #2 (permalink)  
Old 01-08-04, 16:00
acg_ray acg_ray is offline
Registered User
 
Join Date: Jan 2003
Location: Pittsburgh, PA
Posts: 86
Re: What's wrong with my SQL Query??

Your query looks correct. Do you have a primary or foreign key for the teRManager field? I ask, because is it possible that the values in the teRManager field are not all the same (though they look the same)? Perhaps some combinations of Jill Manners; James Monroe have extra spaces mixed into the text, or appended to the end of the names?

For example (assuming '_' is a space), 'Jill_Manners" is not the same as "Jill_Manners_".

Run a query of Select Distinct teRManager from Time_Entry and see if the name combinations show up more than once. If a combination shows up more than once, then there is some difference in how it was entered into the field.
Reply With Quote
  #3 (permalink)  
Old 01-08-04, 16:26
norm801 norm801 is offline
Registered User
 
Join Date: Jan 2004
Posts: 100
Re: What's wrong with my SQL Query??

Quote:
Originally posted by acg_ray
Your query looks correct. Do you have a primary or foreign key for the teRManager field? I ask, because is it possible that the values in the teRManager field are not all the same (though they look the same)? Perhaps some combinations of Jill Manners; James Monroe have extra spaces mixed into the text, or appended to the end of the names?

For example (assuming '_' is a space), 'Jill_Manners" is not the same as "Jill_Manners_".

Run a query of Select Distinct teRManager from Time_Entry and see if the name combinations show up more than once. If a combination shows up more than once, then there is some difference in how it was entered into the field.
I'm using a 4 field "combo" for my primary field. However, the teRManager is not one of them (nor is it a foreign key).

I also considered that perhaps there was extra spacing or some hidden characters in one of the names. The only way I knew to test for it was copy the info and set them equal to each other in Excel and see if it came back "true" (which it did).

Strangely, when I run your suggested query:

Select Distinct teRManager from Time_Entry

I don't get the repeated fields that I get during the first statement I used.

Any ideas?

Norm
Reply With Quote
  #4 (permalink)  
Old 01-09-04, 10:07
acg_ray acg_ray is offline
Registered User
 
Join Date: Jan 2003
Location: Pittsburgh, PA
Posts: 86
Re: What's wrong with my SQL Query??

What version of Access are you using?

The query looks right, so if your data is correct (which your SQL distinct query indicates), I'm at a loss for what else could be the issue.
Reply With Quote
  #5 (permalink)  
Old 01-09-04, 10:58
norm801 norm801 is offline
Registered User
 
Join Date: Jan 2004
Posts: 100
Re: What's wrong with my SQL Query??

Quote:
Originally posted by acg_ray
What version of Access are you using?

The query looks right, so if your data is correct (which your SQL distinct query indicates), I'm at a loss for what else could be the issue.
I was using Access 2002.

Thanks for the help.
Reply With Quote
  #6 (permalink)  
Old 01-15-04, 10:24
Nicjordan Nicjordan is offline
Registered User
 
Join Date: Jan 2004
Location: Somerset
Posts: 5
Re: What's wrong with my SQL Query??

Quote:
Originally posted by norm801
I'm using the following code:

SELECT teRManager, count(*) as ItemCount FROM Time_Entry GROUP BY teRManager

and I seem to be getting numerous repeats when my goal is to get a total for each distinct instance of each item. It seems to work when I run it for other fields in the same table. Strange......

Here is what I get:

teRManager ItemCount
Bruce Amman 59
Jeff Mort 6275
James Monroe 2556
James Monroe;Jill Manners 38
Jill Manners 4
James Monroe;Jill Manners 17
Jill Manners 1698
Jill Manners;James Monroe 1
Jill Manners 28
Jill Manners;James Monroe 1
Jill Manners 63
Jill Manners;James Monroe 2
Jill Manners 90
Jill Manners;James Monroe 1

Here is what I'm looking for:

teRManager ItemCount
Bruce Amman 59
Jeff Mort 6275
James Monroe 2556
James Monroe;Jill Manners 55
Jill Manners;James Monroe 5
Jill Manners 1883

(I'm using Access to pull the info from a Lotus Notes database)

Thanks!

Norm
I might be off base...but dont you have to do an ORDER BY - so the data is grouped together - otherwise everytime it hits a change in teRManager is spits out the total count to date.
Reply With Quote
  #7 (permalink)  
Old 01-15-04, 23:24
rnealejr rnealejr is offline
Registered User
 
Join Date: Feb 2002
Posts: 2,232
Can you post the mdb - I would like to test it.
Reply With Quote
  #8 (permalink)  
Old 01-16-04, 07:06
acg_ray acg_ray is offline
Registered User
 
Join Date: Jan 2003
Location: Pittsburgh, PA
Posts: 86
Re: What's wrong with my SQL Query??

In Access 2002, you should not have to use a ORDER BY statement in the grouping in order to get the proper count.

The GROUP BY statement will order the data accordingly.
Reply With Quote
  #9 (permalink)  
Old 01-16-04, 15:10
SimSoph SimSoph is offline
Registered User
 
Join Date: Jan 2004
Location: Ottawa, Canada
Posts: 53
Re: What's wrong with my SQL Query??

Try to count a specific field as opposed to all the fields (*).

For some reason I have had this work in the past.
Reply With Quote
  #10 (permalink)  
Old 01-16-04, 16:02
norm801 norm801 is offline
Registered User
 
Join Date: Jan 2004
Posts: 100
Re: What's wrong with my SQL Query??

Quote:
Originally posted by SimSoph
Try to count a specific field as opposed to all the fields (*).

For some reason I have had this work in the past.
Great! Thanks for all the help....the problem has been solved.

Norm
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