I have searched high an low for a solution, but I just can't get my head wrapped around how to set this up.
I have a table called "cals". It contains a column called "rep_code". Which contains a certain value identifying the cal, ie. "EFM","LM" etc. The "cals" list of records are linked to wells by "well_id" which is the indexer on another table called "wells". Now each record in the wells table is linked by "run_id" which is the indexer on another table called "runs".
Wells table:
Indexer | run_id
1 | 5
2 | 3
3 | 5
4 | 2
cals table:
Indexer | well_id | rep_code
1 | 3 | LM
2 | 4 | EFM
3 | 1 | LM
4 | 3 | EFM
Now when I view the list of runs, I want to display the total number reports in the run that have "EFM" under "rep_code", and total number of "LM" codes etc. So on the list of runs, the run with ID of 5 should say total of 2 "LM" reports and 1 "EFM". Run number 2 should just have 1 "EFM and 0 "LM"
Basically, what I think I need to do is " Join wells into cals adding run_id where well_id = $well_id from run list"...I think, every time I read this I get a little confused. I am new to "join" so I apologize.
I have managed to put together this bit of code to count each occurrence which works for one well, but cannot seem to get my head wrapped around putting it all into a functional statement for all wells in the run.
$sql = "SELECT
COUNT(CASE WHEN rep_code='LM' THEN 1 END) AS LM_tot,
COUNT(CASE WHEN rep_code='EFM' THEN 1 END) AS EFM_tot,
COUNT(CASE WHEN rep_code='GM' THEN 1 END) AS GM_tot
FROM cals WHERE well_id = '$well_id'";
$query = @mysql_query($sql);
$result = @mysql_fetch_array($query);
$LM_tot = $result['LM_tot'];
$EFM_tot = $result['EFM_tot'];
$GM_tot = $result['GM_tot'];
I also started this join, but I haven't gotten it to work yet.
sql = "SELECT
COUNT(CASE WHEN rep_code='LM' THEN 1 END) AS LM_tot,
COUNT(CASE WHEN rep_code='EFM' THEN 1 END) AS EFM_tot,
COUNT(CASE WHEN rep_code='GM' THEN 1 END) AS GM_tot
FROM cals, wells INNER JOIN wells ON cals.well_id = wells.indexer
WHERE run_id = '$run_id'";
I hope I have explained this clear enough.