473,506 Members | 16,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find percentages in Access query

107 New Member
Hello,
can I write a query that will give percentages of records in one field?
table example:
Expand|Select|Wrap|Line Numbers
  1. table1
  2. IdNumber    model    condition
  3. 01              045         good
  4. 01              045         bad
  5. 02              046         new
  6. 03              047         damaged
  7. 02              046         new
  8. 02              046         old
  9. 03              047         new
  10. 03              047         new
  11. 01              045         damaged
I have 4 things the field conditon can be good, bad, new, and damaged.
Is there any way to show them in percent values?
thank you
Jan 22 '09 #1
7 6073
DonRayner
489 Recognized Expert Contributor
Based on your table data above you could do it like this

Expand|Select|Wrap|Line Numbers
  1. SELECT ((Sum(IIf([Table1]![condition]="new",1,0)))*100)/Sum(IIf([Table1]![condition] Like "*",1,0)) AS NewCount, ((Sum(IIf([Table1]![condition]="old",1,0)))*100)/Sum(IIf([Table1]![condition] Like "*",1,0)) AS OldCount, ((Sum(IIf([Table1]![condition]="good",1,0)))*100)/Sum(IIf([Table1]![condition] Like "*",1,0)) AS GoodCount, ((Sum(IIf([Table1]![condition]="bad",1,0)))*100)/Sum(IIf([Table1]![condition] Like "*",1,0)) AS BadCount, ((Sum(IIf([Table1]![condition]="damaged",1,0)))*100)/Sum(IIf([Table1]![condition] Like "*",1,0)) AS DamagedCount
  2. FROM Table1;
  3.  
Just change all the "Table1" to whatever you table name is
Jan 22 '09 #2
buddyr
107 New Member
thank you- that works
Jan 23 '09 #3
ADezii
8,834 Recognized Expert Expert
@buddyr
Another Option (change to your Table Name)
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT ServiceRec.Condition, Format(DCount("*","ServiceRec","[Condition] = '" & 
  2. [Condition] & "'")/DCount("*","ServiceRec"),"Percent") AS [Percent]
  3. FROM ServiceRec
  4. ORDER BY ServiceRec.Condition;
Expand|Select|Wrap|Line Numbers
  1.  
  2. Condition            Percent
  3. bad                   10.00%
  4. damaged               20.00%
  5. good                  10.00%
  6. new                   40.00%
  7. old                   20.00%
  8.  
Jan 23 '09 #4
buddyr
107 New Member
now your making it look easy- thank you
Jan 23 '09 #5
NeoPa
32,557 Recognized Expert Moderator MVP
To reduce the number of function calls, I've started with ADezii's SQL and doctored it a little. I've also allowed the formatting to be controlled by whatever is using the query. Format() returns a string which may not always suit your requirements.
Expand|Select|Wrap|Line Numbers
  1. SELECT [Condition],
  2.        Count(*)/DCount('*','[ServiceRec]') AS [Percent]
  3.  
  4. FROM [ServiceRec]
  5.  
  6. GROUP BY [Condition]
  7.  
  8. ORDER BY [Condition]
Jan 23 '09 #6
ADezii
8,834 Recognized Expert Expert
@NeoPa
Nah, not as pretty! (LOL)!
Expand|Select|Wrap|Line Numbers
  1.  
  2. Condition            Percent 
  3. bad                   0.1 
  4. damaged               0.2 
  5. good                  0.1 
  6. new                   0.4 
  7. old                   0.2 
  8.  
Jan 24 '09 #7
NeoPa
32,557 Recognized Expert Moderator MVP
That's cos you missed a bit A - XD.
The formatting is done in whatever uses the SQL (QueryDef, Report, etc).
That way it shows how you want it when displayed, and is also usable as a value in the code ;)
Expand|Select|Wrap|Line Numbers
  1. Condition            Percent 
  2. Bad                   10%
  3. Damaged               20%
  4. Good                  10%
  5. New                   40%
  6. Old                   20%
Jan 24 '09 #8

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

Similar topics

0
1161
by: Avinash Dhoot | last post by:
Hi, I have a 5 row table with the following values. key value 1 5 2 10 3 15 4 15 5 5
2
14885
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
26
2205
by: Mike Barnard | last post by:
Hi all. I am playing with html and css. I don't (yet) have a working site, I'm just trying to build a working, basic template I can use for a couple of ideas I have. I recall reading a...
1
1681
by: Bob Alston | last post by:
I need to produce a report like this Color: # % ------ --- --- White 10 20 Black 25 50 other 15 30 ---- ---- Total 50 100
9
27794
by: AZKing | last post by:
Hi all, I would like to know how do you go about calculating percentages in Access. For example, in a form I have 3 combo boxes with drop down menus where a user can select "Yes" or "No" and a...
3
7114
by: HBH | last post by:
I am using Access 2003. I made a pie chart from a query for the sum in each catagory i am using. I want the chart to show the sum and also the percentage of the total.
5
2968
by: eliana82 | last post by:
I have problems calculating score percentages within groups. I have created a boat program in access where the information provided is name, team, boat and score. The first query I've done is...
1
4264
by: marcnz | last post by:
I have been charged of creating a coldfusion web site for our company. Our database has a ms sql 2005 backend and ms access frontend. Almost all tables are linked tables with the SQL database,...
3
2063
by: martin DH | last post by:
Access 2003 I have a table (TASKS) filled with data from an external source. The table lists several tasks for employees to complete and has a text field (STATUS) that identifies where in the...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7370
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
5614
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,...
1
5035
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...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.