Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
webby1396
Starting Member
3 Posts |
Posted - 2006-05-26 : 06:54:17
|
| Hi.I have two tables.Activity(ActivityID,Name,Price,NumberAttending)Student(StudentID,MorningActivityID,AfternoonActivityID)I want to update Activity.NumberAttending with the count of students going participating in that activity for both the morning and afternoon. ie i want the numberAttending field to be automatically updated for each activityID with the amount of students attending.I keep getting errors in Access when trying to do it.Also, is there a way of doing it so that all of them happen automatically and i dont have to pass a parameter into the query.TIA |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-05-26 : 07:23:11
|
| IS activity.activityid = student.studentid.....?ThanksKK |
 |
|
|
CSK
Constraint Violating Yak Guru
489 Posts |
Posted - 2006-05-26 : 07:23:51
|
| Otherwise give some sample data for both table..!ThanksKK |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-26 : 07:54:18
|
Hi all,webby - you probably shouldn't store NumberAttending in your data. That information is already available from the other data, so there's no need to store it.Instead, you can just write a query to get hold of it when you need (I'm guessing with the ActivityID mapping)...--datadeclare @Activity table (ActivityID int identity(1, 1), Name varchar(30), Price int)insert @Activity select 'some activity', 100union all select 'some other activity', 200union all select 'very expensive activity', 20000declare @Student table (StudentID int identity(1, 1), MorningActivityID int, AfternoonActivityID int)insert @Student select 1, 2union all select 1, 2union all select 1, 3union all select 2, 1union all select 2, 1union all select 1, 3--calculationselect *, (select count(*) from @Student where MorningActivityID = a.ActivityID) as NumberAttendingInMorning, (select count(*) from @Student where AfternoonActivityID = a.ActivityID) as NumberAttendingInAfternoon, (select count(*) from @Student where a.ActivityID in (MorningActivityID, AfternoonActivityID)) as NumberAttendingfrom @Activity a--resultsActivityID Name Price NumberAttendingInMorning NumberAttendingInAfternoon NumberAttending ----------- ------------------------------ ----------- ------------------------ -------------------------- --------------- 1 some activity 100 4 2 62 some other activity 200 2 2 43 very expensive activity 20000 0 2 2 But then again, you've said you're using Access, so maybe you're in the wrong forum...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
webby1396
Starting Member
3 Posts |
Posted - 2006-05-26 : 15:53:37
|
| Oops silly me... wrong forums indeed. what a n00b. ill post it in the access one. thanks |
 |
|
|
|
|
|
|
|