MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

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
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 25th, 2004, 12:27 PM
KillerKeller_jr KillerKeller_jr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 KillerKeller_jr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
MS query help

This is for a college project: I am trying to average all records of the same ID but also have it do the same for all of the IDs within the same query. Is their a way of doing this?

From Tables

Sow/Gilt

ID

Litter

NumberBornLive

Thanks
Chris

Reply With Quote
  #2  
Old April 25th, 2004, 02:01 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
what DBMS?

Did you set up the tables exactly as you have in your post??...

TablleName (Columns)
Sow/Gilt (ID)

Litter (NumberBornLive)

There's a fundamental flaw in the way you have these setup. As is, there is no way to relate fields from either table. You need to have a field/column that relates the two tables...a relationship. For example, put an ID field in the 'Litter' table that way we can relate what Sow/Gilt is tied to the specific number in the NumberBornLive field...

TablleName (Columns)
Sow/Gilt (ID)

Litter (SowGilt_ID, NumberBornLive)

...the Sow/Gilt.ID would then = the Litter.SowGilt_ID field. Until you do that, we really can't come up w/your solution.

Reply With Quote
  #3  
Old April 25th, 2004, 02:17 PM
KillerKeller_jr KillerKeller_jr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 KillerKeller_jr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am using MS Access 2000 and 2003.

Here is the structure of my DB:

Boar
BID(PK)
DateEnteredHerd
CulledDate
CulledReason
UID(FK)

Sow/Gilt
ID(PK)
DateEnteredHerd
Genetics
CulledDate
CulledReason
DiedDate
DiedReason
UID(FK)

Services
SID(PK)
ID(FK)
BID(FK)
DateBred
DateToFarrow
FailedToFarrow
BreedingComments
UID(FK)

Litter
LID(PK)
DateFarrowed
NumberBornLive
DateWeaned
NumberWeaned
Mastitis
Stillborn
Mummies
SID(FK)
UID(FK)

PreWeaningDeaths
PID(PK)
LID(FK)
DateDied
Cause
UID(FK)

UserID
UID(PK)
FirstName
LastName

Last edited by KillerKeller_jr : April 25th, 2004 at 02:26 PM.

Reply With Quote
  #4  
Old April 25th, 2004, 02:42 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Ok cool...so for each ID in the Sow/Gilt table, you want the AVG of NumberBornLive in the Litter table?

Last edited by Username=NULL : April 25th, 2004 at 02:52 PM.

Reply With Quote
  #5  
Old April 25th, 2004, 02:50 PM
KillerKeller_jr KillerKeller_jr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 KillerKeller_jr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Username=NULL
Ok cool...so for each ID in the Sow/Gilt table, you want the AVG of NumberBornLive in the Litter table?



Yes, correct although I am also going to do it for the NumberWeaned too. Thanks.

Reply With Quote
  #6  
Old April 25th, 2004, 02:53 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
OK, sorry, was editing as you were typing, I can get it...just have a question...

In the Services table you have 'ID' as a FK, what is it a FK of?...possibly the Sow/Gilt.ID?

Reply With Quote
  #7  
Old April 25th, 2004, 03:49 PM
KillerKeller_jr KillerKeller_jr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 KillerKeller_jr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Username=NULL
OK, sorry, was editing as you were typing, I can get it...just have a question...

In the Services table you have 'ID' as a FK, what is it a FK of?...possibly the Sow/Gilt.ID?


You are correct, it is a FK of Sow/Gilt.

Reply With Quote
  #8  
Old April 25th, 2004, 07:38 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Give this a try, see if it works...
Code:
Select  distinct SG.ID, AVG(L.NumberBornLive) as AvgBornLive
from    Sow/Gilt SG
inner   join Services S
        (inner join Litter L on S.SID = L.SID)
        on SG.ID = S.ID
group   by SG.ID

Last edited by Username=NULL : April 25th, 2004 at 07:41 PM.

Reply With Quote
  #9  
Old April 25th, 2004, 07:51 PM
KillerKeller_jr KillerKeller_jr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 11 KillerKeller_jr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It doesn't seem to like the / in SOW/GILT in the from clause. Should I change it or what. Must be MS Access that doesn't like it.

Reply With Quote
  #10  
Old April 25th, 2004, 09:13 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
You can get around that by using brackets around the table name. Give this a try...
Code:
Select  distinct SG.ID, AVG(L.NumberBornLive) as AvgBornLive
from    [Sow/Gilt] SG
inner   join Services S
        (inner join Litter L on S.SID = L.SID)
        on SG.ID = S.ID
group   by SG.ID

...but ya I would probably change that table name, I was actually gonna suggest it but if it works it works. I rarely user any special characters in my table or field names though...and I think most would agree it's a popular convention.

Last edited by Username=NULL : April 25th, 2004 at 10:57 PM. Reason: Double checked and yes, it is possible...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > MS query help


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


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





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