Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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
  #1  
Old March 27th, 2003, 11:07 AM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
calculations with queries

i am trying to calculate the sum of all records in one particular field. how do i do this using queries? do i need to create a new field in the query dedicated to this calculation? thanks

ameen

Reply With Quote
  #2  
Old March 27th, 2003, 02:54 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
no, just use the sum() function in the query statement along with a group by.

select sum(some_field) from some_table group by some_other_field.

Reply With Quote
  #3  
Old March 28th, 2003, 12:32 PM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
when i select the sum from the query i am trying to run nothing happens for the particular field. how do i get the sum for all of the data for only one field of my query?

Reply With Quote
  #4  
Old March 28th, 2003, 01:01 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
how about showing the query you are trying to run...

Reply With Quote
  #5  
Old March 28th, 2003, 02:27 PM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
well you see the query isn't very important. i am just trying to generate a value which is the sum of all the values across all records in a particular field.

i am building a clients tracking database and i am tracking quarterly information of each clients business. i have a one to many relationship between clients and year (the join table consisting of only client id, year id and year) and a one to many relationship between year and quarterly data. (one year has many (4) quarters per year of information). I can get the data of each client with no problem, it's creating totals and summaries that becomes a problem. so i am trying to do 2 things:

1)have the totals for each quarter (each record of the quarter table) of every field in the quarterly data. so i would be summing up information from quarters 1,2,3, and 4 into many fields that will hold this data. this will apply only to the same client record.

2)have the totals for ALL of the clients in each field of the quarterly data INCLUDING the totals (for each client) created in the previous step. this would display data in each quarter for all employees not just one.

i would like this total/summary information to be displayed in reports based on queries. can all of this summing be done in queries if i have to sum across many records?

please let me know if my explanation is confusing and i'll be more specific. thank you.

ameen

Reply With Quote
  #6  
Old March 28th, 2003, 03:04 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,733 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 20 h 11 m 38 sec
Reputation Power: 869
you have year id and year? a table for years? what does that table look like, i.e. what other columns are in there besides year id and year? and a quarter table? what does that look like?

i'm a little unclear what you mean by "summing up information from quarters 1,2,3, and 4 into many fields" and by "have the totals for ALL of the clients in each field of the quarterly data INCLUDING the totals (for each client) created in the previous step"

i'd want to see all your tables, not necessarily your query

the design of the tables will dictate what the query looks like

off the top of my head, i wouldn't need quarter and year tables, but rather just produce totals in the query by quarter and/or year


rudy

Reply With Quote
  #7  
Old March 28th, 2003, 03:09 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
well you see the query is important. it is easier to walk through fixing one statement then it is trying to teach proper syntax through only theory and no examples.

i understand the basis of what you are doing.

you can get the desired data through a combination of sum() and group by.
so basically, in your statements that you use to get the detailed data you want, take all of the fields except the one that you want summed out of the select but leave all of the where conditions intact.
the surround the field name with sum(), and possibly give it an alias for easy reference.
then before (if you have one) the order by clause, add a group by clause to the statement and add the field name that is for the quarters and/or users depending on how you want the data summed.

i.e.
lets take a sample table
-----------------------------------
Code:
--------------------------
| quarter | year | value |
+---------+------+-------+
|    1    | 2003 | 100   |
+---------+------+-------+
|    2    | 2003 | 100   |
+---------+------+-------+
|    3    | 2003 | 100   |
+---------+------+-------+
|    4    | 2003 | 100   |
+---------+------+-------+
|    1    | 2004 | 100   |
+---------+------+-------+
|    2    | 2004 | 100   |
+---------+------+-------+
|    3    | 2004 | 100   |
+---------+------+-------+
|    4    | 2004 | 100   |
+---------+------+-------+

ok, now, say we want to sum the data by year and show all years in the table
select year, sum(value) as total from sample group by year, order by year
This would output something like:
Code:
+------+-------+
| year | total |
+------+-------+
| 2003 |   400 |
| 2004 |   400 |
+------+-------+


now, lets say we wanted to get a sum on a quarters basis but for all years
select quarter, sum(value) as total from sample group by quarter, order by quarter
This would output something like:
Code:
+---------+-------+
| quarter | total |
+---------+-------+
|       1 |   200 |
|       2 |   200 |
|       3 |   200 |
|       4 |   200 |
+---------+-------+


now, lets say we carry the last one just a bit further and we only want to see the 1st quarter
select sum(value) as total from sample where quarter = 1 group by quarter
This would output something like:
Code:
+-------+
| total |
+-------+
|   200 |
+-------+

Reply With Quote
  #8  
Old March 31st, 2003, 10:52 AM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
ok. keeping the query simple works very well. i can actually pull quarterly data now. i have little more complex query that i am trying to execute now. i want to add the 1)sum of a field (for all for quarters) to 2)just the 4th quarter value of another field. how would i add these two values?

Reply With Quote
  #9  
Old March 31st, 2003, 10:57 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
i don't think you would do this in one query.
retrieve your two values from two seperate statements and add them together then.

Reply With Quote
  #10  
Old March 31st, 2003, 12:32 PM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
could i use a join query for that? from the previous explanation that you gave i have created a query that adds together all of the clients information for each quarter. so each field in the quarterly data is compiled for all the clients in the query. somehow, i would like to show these numbers as percentages of a total field that i have created. i am having trouble including a division expression within this query. is this possible? if not then where would i do the division? could i bring one query into another? thanks

ameen

Reply With Quote
  #11  
Old March 31st, 2003, 12:49 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
no, i don't think you could use a join query for that. the problem with grouping and summing is that the more complex you make it the more likely you are that you aren't getting the data you want.

the division part of it is easy enough to do in either a) the query or b) your program
as long as you have the value you need to divide it by in the resultset also. If you don't then you can only do this in the program, but i would recommend doing this in the program also. The reason behind this is because you need to keep in mind to check for 0 on the other value because dividing by zero is not a good thing and will cause errors.
You should be able to do this easily enough in your program though.

Reply With Quote
  #12  
Old March 31st, 2003, 04:29 PM
bogglebeats bogglebeats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 21 bogglebeats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
i have decided to base the division on the query. i created a new query based on the first query (the sum of each quarter query). now i just have to divide each number by a number that is already in the query. do i declare the new percent field as an expression in the 'group by' segment? right now i am just declaring the new field:[value1 / value2] and no numbers are showing up in the field. if i don't use a query for the calculation, what would i use? i really don't want to use a form for this for reporting purposes. what to do? thanks.

ameen

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > calculations with queries


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
On