Group By first 3 digits of 6 (1 Viewer)

KDH

New member
Local time
Today, 15:25
Joined
Jul 10, 2008
Messages
6
Hey all, I am trying to put together a query that takes a field that has 6 digits and Group By the first 3 digits. This information is then displayed in a Form.

Ex.
Dept ID: 500104 Balance: 20
Dept ID: 500108 Balance: 30
Dept ID: 500204 Balance: 50
Dept ID: 515221 Balance: 20

Group By: 500 Balance: 100
Group By: 515 Balance: 20

I was thinking to do the criteria by 500 * or something but having trouble. This is also a Number Type NOT Text based. Anything thoughts would be appreciated!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 15:25
Joined
Aug 30, 2003
Messages
36,130
Have you tried

Left(FieldName, 3)
 

MSAccessRookie

AWF VIP
Local time
Today, 18:25
Joined
May 2, 2008
Messages
3,428
I can think of two choices for the groupings (depending on whether you like strings or numbers better). Either one should do what you are asking

SELECT int([Dept ID]/1000), sum(field2) from table10
group by int([Dept ID]/1000);

SELECT Left(cstr([Dept ID]),3), sum(field2) from table10
group by Left(cstr([Dept ID]),3);
 

KKilfoil

Registered User.
Local time
Today, 18:25
Joined
Jul 19, 2001
Messages
336
Hey all, I am trying to put together a query that takes a field that has 6 digits and Group By the first 3 digits. This information is then displayed in a Form.

Ex.
Dept ID: 500104 Balance: 20
Dept ID: 500108 Balance: 30
Dept ID: 500204 Balance: 50
Dept ID: 515221 Balance: 20

Group By: 500 Balance: 100
Group By: 515 Balance: 20

I was thinking to do the criteria by 500 * or something but having trouble. This is also a Number Type NOT Text based. Anything thoughts would be appreciated!

If numeric, create a GroupBy field like
FirstThree: +int([Dept ID]/1000)
 

MSAccessRookie

AWF VIP
Local time
Today, 18:25
Joined
May 2, 2008
Messages
3,428
I can think of two possibilities. Which one you want to use depends on whether you need a string or a number as the interim value.

SELECT int([Dept ID]/1000), sum(Balance)
FROM <YourTableName>
GROUP BY int([Dept ID]/1000);

SELECT left(cstr(Dept ID]), 3), sum(Balance)
FROM <YourTableName>
GROUP BY left(cstr(Dept ID]), 3);
 

KDH

New member
Local time
Today, 15:25
Joined
Jul 10, 2008
Messages
6
Thanks for all of your help! I got it working just fine now thanks again ^_^.
 

KDH

New member
Local time
Today, 15:25
Joined
Jul 10, 2008
Messages
6
Another Query question, but a little different.

We recently recieved a data dump from an outside vendor and there are two fields that are always related to each other:

Account_ID AND Patient Name

It seems that Account_ID is always filled in, however, there are times when there is no Patient Name.

Ex
Account_ID Patient Name
12345 Bob
12345
78945 Jim
78945 Jim

The problem comes when i do a Group By and what shows up is:

12345 Bob
12345
78945 Jim

Is there a way to make the Group By Account_ID and display the Not Null value? And I cannot do a Criteria: Is Not Null since this will elimante information that is stored to that specific Account_ID. Thanks again for all of your help!
 

KDH

New member
Local time
Today, 15:25
Joined
Jul 10, 2008
Messages
6
Using the Sort By: MAX elimanites those records and i lose the information that is stored there. I worked around it just by removing the Patient Name and using a group by Account ID.
 

khawar

AWF VIP
Local time
Tomorrow, 02:25
Joined
Oct 28, 2006
Messages
870
SELECT Table1.Account_Id, Table1.Patient_Name
FROM Table1
GROUP BY Table1.Account_Id, Table1.Patient_Name
HAVING (((Table1.Patient_Name) Is Not Null));


Replace table1 in the above sql statement with your table name
 

Users who are viewing this thread

Top Bottom