innitboy Posted January 20, 2007 Share Posted January 20, 2007 I have 3 columns of data 500 -- 150 -- 54500 -- 150 -- 32500 -- 148 -- 75500 -- 132 -- 98I would like it to display this1. 500 - 430The sum of the unique rows in 2nd column (150 + 148 + 132 = 430)Should I be looking to make 3 different tables from this data and join them or can i get the sum of the 2nd column filtered by the first column in this same table,Here is some code but its not workingThanks in advance for any help :)[code]$query = "SELECT id, 1stlevelkey, FROM keywords where 1stlevelkey = '".$stlevelkey."' group by 1stlevelkey "; $result = mysql_query($query);while ($row = mysql_fetch_assoc($result)) { $query = "SELECT id, 1stlevelkey, sum(2ndlevel) / count(2ndlevel) as count2ndFROM keywords where 1stlevelkey = '".$firstlevelkey."' group by 1stlevelkey "; $result = mysql_query($query);while ($row = mysql_fetch_assoc($result)) {print_r($row['1stlevelkey']);echo "-";print_r($row['count2nd']);echo "<br>"; }} [/code] Link to comment https://forums.phpfreaks.com/topic/34979-sum-unique-values-of-a-column/ Share on other sites More sharing options...
innitboy Posted January 20, 2007 Author Share Posted January 20, 2007 I can get it to return the list grouped by the 2nd column with the code belowlike this500 - 150500 -- 148500 -- 132however i cannot get it to do this500 - 430[code]$query = "SELECT id, 1stlevelkey, sum(distinct 2ndlevel) / count(distinct 2ndlevel) as count2nd FROM keywords group by 2ndlevelkey "; $result = mysql_query($query);while ($row = mysql_fetch_assoc($result)) {print_r($row['1stlevelkey']);echo "-";print_r($row['count2nd']);echo "<br>"; } [/code] Link to comment https://forums.phpfreaks.com/topic/34979-sum-unique-values-of-a-column/#findComment-164983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.