0

Ok, so I have a Student table that has 6 fields, (StudentID, HasBamboo, HasFlower, HasAloe, HasFern, HasCactus) the "HasPlant" fields are boolean, so 1 for having the plant, 0 for not having the plant.

I want to find the average number of plants that a student has. There are hundreds of students in the table. I know this could involve transposing of some sort and of course counting the boolean values and getting an average. I did look at this question SQL to transpose row pairs to columns in MS ACCESS database for information on Transposing (never done it before), but I'm thinking there would be too many columns perhaps.

My first thought was using a for loop, but I'm not sure those exist in SQL in Access. Maybe a SELECT/FROM/WHERE/IN type structure?

Just hints on the logic and some possible reading material would be greatly appreciated.

Community
  • 1
  • 1
bov25
  • 121
  • 3
  • 12

1 Answers1

0

you could just get individual totals per category:

SELECT COUNT(*) FROM STUDENTS WHERE HasBamboo

add them all up, and divide by

SELECT COUNT(*) FROM STUDENTS

It's not a great database design though... Better normalized would be:

Table Students; fields StudentID, StudentName
Table Plants; fields PlantID, PlantName
Table OwnedPlants; fields StudentID,PlantID

The last table then stores records for each student that owns a particular plant; but you could easily add different information at the right place (appartment number to Students; Latin name to Plants; date aquired to OwnedPlants) without completely redesigning table structure and add lots of fields. (DatAquiredBamboo, DateAquiredFlower, etc etc)

Jur
  • 520
  • 2
  • 18
  • Oh and your average plant problem could simply be solved by total count of OwnedPlants divided by total count of Students... – Jur Dec 10 '15 at 14:49
  • Thanks! I ended up making a long list of left joins to get the total count before just stopping. I'll look more into your solution. I agree that the design isn't pretty or great haha. – bov25 Dec 10 '15 at 15:12