Frage

I've searched for adding multiple AVG calculations and have found a few entries, however I'm having to join another table and the examples of that are scarce.

closest answer I can find is this but it deals with dates and no joins

here are my tables:

indicators:

StandardScore             IndicatorID      NID    DID
0.033333                  7                1      1
0.907723                  9                1      1
0.574739                  26               1      1
0.917391                  21               1      1
.....

indexindicators:

IndexID                   IndicatorID
1                         7
1                         26
2                         21
3                         7
4                         9
4                         21
4                         7
5                         9
.......

My goal is to get the average for each IndexID (indexindicators) related to NID/DID (indicators) combination

a query to retrieve a single value would be

SELECT AVG(StandardScore) FROM `indicators` INNER JOIN indexindicators ON indicators.IndicatorId=indexindicators.IndicatorId WHERE nid=1 AND did=1 AND indexindicators.IndexId=1

ultimately there will be 6 (indexID) averages which then have to be rounded then * by 100 (should I do that part with PHP?)

This seems like such a simple query, but i just can't seem wrap my mind around it.

Thanks in advance for your help!

War es hilfreich?

Lösung

SELECT nid, did, indexid, 100.0 * AVG(StandardScore) 
    FROM 'indicators'
    INNER JOIN 'indexindicators'
      ON indicators.IndicatorId=indexindicators.IndicatorId 
group by nid, did, indexid
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top