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

Query/Subquery - Percentage

Hello everyone

i have 3 fields:

1. Code (Mon01, Mon02 etc)
2. Recycling (Check box, yes or no)
3. Tonnage

There are over 10,000 records in this table. I am trying to calculate in a query the percent for each code (Total approx 70), from the sum tonnage for records that are recycling (Check box ticked)

I have tried using a subquery in the tonnage field selecting records where recycling = yes. i get an error message saying the subquery will return 1 if any results.

Any help please!!!!
Sep 4 '08 #1
19 3237
NeoPa
32,556 Expert Mod 16PB
Start by designing a query which is GROUPed by [code] and WHERE [Recycling].

Next build a query from that where each record has the count divided by a DCount() of all the [Recycling] records in the table.

Does that make sense?
Sep 5 '08 #2
erm roughly.

How would you go about writing the Dcount/Count expression for the second part??
Sep 8 '08 #3
NeoPa
32,556 Expert Mod 16PB
Expand|Select|Wrap|Line Numbers
  1. DCount("*", "[YourTable]", "[Recycling]")
NB. The third parameter is not simply a field reference. It is a WHERE clause with the "WHERE " removed. As [Recycling] (as far as I can tell anyway) is a boolean field, the value is a TRUE/FALSE value, which is exactly what is required for a WHERE entry (no manipulation needed).

IE. The expression [Field] = 'A' actually resolves to a boolean value. A boolean field already IS a boolean value.

Is this clear and helpful?
Sep 8 '08 #4
hmm,

I do apologise for perhaps being a little slow. Im still confused. I undertand the DCount function now but i am still unsure how to make it all work.

Ive started by grouping by [code] and summing the total tonnage for each one.

Now i need to a field which sums how much of this tonnage (Kg) can be attributed to recycling.

From this i just need to divide the first summed tonnage by 100, then times this figure by the sum of tonnage which can be attributed to recycling.

I do apologise again but is there anyway you could perhaps spell it out more clearly and completely?
Sep 9 '08 #5
Update -

I have constructed a query that groups by [code]
I have summed the [tonnage] attributed to this code
I have now another field that divides the attributed code by 100

now i need another field that sums the [tonnage] attrbuted to the code which has the [recycling] check box ticked.

can i do this with "SELECT [Tonnage] From [Tonnage] WHERE [Recycling]-True"?
Sep 9 '08 #6
how about useing DSum

for example DSum ("[Tonnage]![Tonnage]", "[Tonnage]", "[Recycling] = True'")

(not worked by the way) but am i on the right lines?
Sep 9 '08 #7
NeoPa
32,556 Expert Mod 16PB
I've caught this very late so I need to look at it again tomorrow.

If I miss it after 24hrs, please bump the thread.
Sep 9 '08 #8
this is a reminder lol :)
Sep 11 '08 #9
NeoPa
32,556 Expert Mod 16PB
Well bumped.

I have lunch in 1/2 an hour, so I'll look at it then in more depth.
Sep 11 '08 #10
NeoPa
32,556 Expert Mod 16PB
Having read some of your later posts I now appreciate what your first post meant, more clearly.

Rather than the percentage [Tonnage] that each [ Code ] (Not a good name to use on this site as it treats it as meaning [ CODE ] tags) is, of the total [Tonnage] and completely ignoring items which are not [recycling], you mean the percentage, within each [ Code ], that the [Recycling] [Tonnage] is of the total [Tonnage] for that [ Code ].

This means that my earlier advice is inappropriate for this question.

I see, however, that you have made some progress on your own with the logic, so I will put down what I recommend and we can progress from there.
Sep 11 '08 #11
NeoPa
32,556 Expert Mod 16PB
To avoid posting problems on this site I will refer to your [ Code ] field as [TCode].

To avoid ambiguity and confusion I will refer to your table as [tblTonnage]. The table name of [Tonnage] - which I get from your post #6 - is unfortunate, as this also appears to be the name of one of the fields.

The SQL should be (relatively) straightforward with the new understanding of the question.
Expand|Select|Wrap|Line Numbers
  1. SELECT [TCode],
  2.        Sum(IIf([Recycling],[Tonnage],0)) AS RecycTonnage,
  3.        Sum([Tonnage]) AS TotTonnage,
  4.        (100*[RecycTonnage]/[TotTonnage]) AS RecycPC
  5.  
  6. FROM [tblTonnage]
  7.  
  8. GROUP BY [TCode]
Have a look at how it does it and see if you can A) Understand it; B) Confirm this should do the job you need.
Sep 11 '08 #12
Yea thats works great thank you.
The fields you were having difficulty with do actually have different names but i simplified them for what i thought would be easier for understanding.

A it works and B i understand it.

The only problem i am now having is that when i try and open the query, access asked me to state the value of [Recyctonnage] and [TotTonnage]. If i click Ok it still works but iw ouldnt mind getting rid of this minor problem. Also i need to sort the result so that [RecycPC] is displayed highest first etc. I have tried to do this by sorting the field in the design view of the query but to no avail.

Thank you once again
Sep 12 '08 #13
NeoPa
32,556 Expert Mod 16PB
Yea thats works great thank you.
The fields you were having difficulty with do actually have different names but i simplified them for what i thought would be easier for understanding.

A it works and B i understand it.

The only problem i am now having is that when i try and open the query, access asked me to state the value of [RecycTonnage] and [TotTonnage]. If i click Ok it still works but iw ouldnt mind getting rid of this minor problem. Also i need to sort the result so that [RecycPC] is displayed highest first etc. I have tried to do this by sorting the field in the design view of the query but to no avail.

Thank you once again
I suspect that this only arises after you have attempted to change it to do the sorting. Understandable enough.

The original SQL (that I posted) doesn't have this problem, but allowing Access to try to do the sort for you will cause this.

A revised version which does the sort too is more complex as the sorting (ORDER BY clause) is done before the SELECT results are available, so it is not possible to refer to them by their ALIASes.

Try this instead :
Expand|Select|Wrap|Line Numbers
  1. SELECT [TCode],
  2.        Sum(IIf([Recycling],[Tonnage],0)) AS RecycTonnage,
  3.        Sum([Tonnage]) AS TotTonnage,
  4.        (100*[RecycTonnage]/[TotTonnage]) AS RecycPC
  5.  
  6. FROM [tblTonnage]
  7.  
  8. GROUP BY [TCode]
  9.  
  10. ORDER BY (100*Sum(IIf([Recycling],[Tonnage],0))/Sum([Tonnage])) DESC
Sep 13 '08 #14
NeoPa
32,556 Expert Mod 16PB
A new question has been split off from this thread and can now be found at Cross-Tab Problem.
Sep 15 '08 #15
Hey

any way of putting a [Date] criteria into this? using date fields on the form.
Expand|Select|Wrap|Line Numbers
  1. SELECT Tonnage.SteveCode, Sum(IIf([Recycling],[Tonnage],0)) AS RecycTonnage, Sum(Tonnage.Tonnage) AS TotTonnage, (100*[RecycTonnage]/[TotTonnage]) AS RecycPC
  2. FROM Tonnage
  3. GROUP BY Tonnage.SteveCode
  4. ORDER BY (100*Sum(IIf([Recycling],[Tonnage],0))/Sum([Tonnage])) DESC;
i was thinking somthing along the lines of :
Expand|Select|Wrap|Line Numbers
  1. Sum(IIf([Date],(Sum(IIf([Recycling],[Tonnage],0)), (Between [Forms]![Recycling Rates]![From Date] And [Forms]![Recycling Rates]![To Date])
am i close?

by the way this is the SQL from this original thread not the split of thread.
Sep 15 '08 #16
NeoPa
32,556 Expert Mod 16PB
I have re-added a post which was mistakenly switched to the other thread. I can see your latest post is relevant to this thread too, but it might take me a while to work out what you're on about. The post is not well expressed and I'm just hoping I can work out what you mean if I process through it all in detail :(
Sep 15 '08 #17
NeoPa
32,556 Expert Mod 16PB
Luckily, although the form and the controls on it have never been mentioned before, I can work out from your suggested SQL, what it is you seem to be trying to do. It's much better to explain your situation in words, but at least it's here.

What you need is a WHERE clause. I have to assume, as you haven't included it in your question (one of the most fundamental parts of what should have been the question) that you have a field in [Tonnage] called [Date]. Like having a field called [Tonnage] in [Tonnage], having a field called [Date] is really not recommended. I should say these are highly recommended AGAINST. The problems these will cause you are perfectly avoidable. It seems a shame to suffer from them in the circumstances.

Anyway, try this :
Expand|Select|Wrap|Line Numbers
  1. SELECT [SteveCode],
  2.        Sum(IIf([Recycling],[Tonnage],0)) AS RecycTonnage,
  3.        Sum([Tonnage]) AS TotTonnage,
  4.        (100*[RecycTonnage]/[TotTonnage]) AS RecycPC
  5.  
  6. FROM [Tonnage]
  7.  
  8. WHERE [Date] Between [Forms]![Recycling Rates]![From Date]
  9.                  And [Forms]![Recycling Rates]![To Date]
  10.  
  11. GROUP BY [SteveCode]
  12.  
  13. ORDER BY (100*Sum(IIf([Recycling],[Tonnage],0))/Sum([Tonnage])) DESC
Sep 15 '08 #18
What you provided works beautifully, and i thank you.

I apologise for the amateurish mistakes but this is my first attempt at using Access and am i am completely self taught. I certainly have learned alot though and take your suggestions on board for next time.

Thank you once again.
Sep 16 '08 #19
NeoPa
32,556 Expert Mod 16PB
Two threads sorted in one session :) Great news!

I fixed the bug in the GROUP BY line by the way. I expect you simply discovered it and fixed it yourself in your code. The post (#18) has now been updated.
Sep 16 '08 #20

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

Similar topics

4
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
2
by: Yonatan Goraly | last post by:
I have a query that uses the same view 6 times. It seems that the database engine is calculating the view each time. The result is very poor performance. The same query takes 2 sec with MS SQL,...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
9
by: Christine | last post by:
It has come to my attention that sometimes, when I open a Query in SQL View, the SQL that I see is not exactly the same as the SQL in the Query's Querydef. The difference I see (only occasionally)...
2
by: phaddock4 | last post by:
Please help. GOAL: I hope to calculate a total amount in a TotalQuery (or field in the SubQuery?), based upon three field amounts in a SubQuery. STATUS: When I create an expression in the...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
3
by: Branco Medeiros | last post by:
Hi all, Using SQL Server 2000, SP4. I have a table of street names (Rua) whose ids (cod_rua) are foreign keys into a consumer table (Consumidor). It turns out that the "Rua" table has many...
4
gauravgmbhr
by: gauravgmbhr | last post by:
hi friends Well i am trying to create a query which uses a subquery in the from clause I want as soon as the subquery fetches a row the , the subquery should stop executing , beacuse the table used...
1
by: Aleck | last post by:
Hie. I have a trigger that monitors changes to my table fields but I get an error saying subquery returned more than one value.Below is the code for my trigger, hope you will figure out whats...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.