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 June 6th, 2003, 03:58 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 411 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 12 h 42 m 14 sec
Reputation Power: 6
Avg

Hi,

right now say I have data that looks like such

Name data1 data2 data3
a 1 2 3
b 3 5 2
c 1 7 9

what I would like to do is obtain the average from these queries, but with one slight condition

in all the data sets, while calculating the average, I do not want it to calculate the average if it encounters 3 in any of the columns.
the problem here is if I tell it to ignore 3 in 'data1', it will ignore row 2 in all of the other columns as well.

Reply With Quote
  #2  
Old June 6th, 2003, 07:04 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 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 21 h 15 m 47 sec
Reputation Power: 870
your question is a bit ambiguous, but i'll take a stab at it

select sum(iif(data1=3,0,data1)) / sum(iif(data1=3,0,1)) as avg1
, sum(iif(data2=3,0,data2)) / sum(iif(data2=3,0,1)) as avg2
, sum(iif(data3=3,0,data3)) / sum(iif(data3=3,0,1)) as avg3
from yourtable

this calculates the (column) averages, ignoring any 3s

rudy
http://r937.com/

Reply With Quote
  #3  
Old June 6th, 2003, 10:53 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 411 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 12 h 42 m 14 sec
Reputation Power: 6
thank you..

I will try that..
It seems that the SQL I learned doesn't seem to suffice my needs..
I went on www.sqlcourse.com and went through the whole basic and advanced queries tutorial.. but have never come across iif

where did u learn ur SQL ??

Reply With Quote
  #4  
Old June 7th, 2003, 12:12 AM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,323 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 8 m 45 sec
Reputation Power: 26
iif is not SQL, its Visual Basic code and will only work on MS Access or maybe SQL Server.

Reply With Quote
  #5  
Old June 7th, 2003, 04:57 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 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 21 h 15 m 47 sec
Reputation Power: 870
thanks, a.koepke, i knew that, but ecit12 is running access, as you can see in this other thread: http://forums.devshed.com/t64389/s.html

rudy

Reply With Quote
  #6  
Old June 7th, 2003, 12:40 PM
a.koepke's Avatar
a.koepke a.koepke is offline
Second highest poster :p
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Jul 2001
Posts: 7,323 a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level)a.koepke User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 8 m 45 sec
Reputation Power: 26
Yeah, I wasn't correcting or commenting on your solution, Just making sure that ecit12 knew it wasn't SQL, from his post it seems that he didn't

Reply With Quote
  #7  
Old June 7th, 2003, 01:33 PM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 411 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 12 h 42 m 14 sec
Reputation Power: 6
thanks guys for helping me out..

r937, the code u provided me works perfectly.
many thanks to u..

thanks a.koepke for letting me know that its not SQL..
for a minute there, I thought I have so much more SQL to learn..

Reply With Quote
  #8  
Old June 16th, 2003, 04:49 AM
ecit12's Avatar
ecit12 ecit12 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 411 ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level)ecit12 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 12 h 42 m 14 sec
Reputation Power: 6
ok... I'm encountering another problem here..

here is my main statement

Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),ServiceOutput.A1,0))/Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),1,0)) AS AvgOfA1


what that piece of code does is basically calculate the average the way r937 instructed me to.. and the method works well.. now, in some cases, the average of the column can't be calculated because all the data values fall outside the range

eg

Col1

99
99
99
99

etc..

so when it outputs a message, it gives be me an error message.

what I want to do is tell it that if the average is not between 1 and 10, the value of the average is 99 (a special value)

however, if I write it out.. my code increases by 300%
this is how I would write it out, but it doesn't seem like a good idea.

iif((
Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),ServiceOutput.A1,0))/Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),1,0)))>-1 AND (Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),ServiceOutput.A1,0))/Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),1,0)))<11), Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),ServiceOutput.A1,0))/Sum(IIf((ServiceOutput.A1>-1) And (ServiceOutput.A1<11),1,0)), 0)

or
iif(AvgCalculated>-1 AND AvgCalculated<11, AvgCalculated, 0)
any suggestions..??

Last edited by ecit12 : June 16th, 2003 at 04:58 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Avg


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
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway