2014-12-02 2 views
0

지난 한 달 동안 사용자가 특정 연도의 매주 데이터를 보유하는 범주 & 벤치 마크 항목을 추가 할 수있는 벤치 마크 시스템을 만들었습니다. 그 주에 대한 데이터가 없거나 1 년 동안 데이터가 없더라도 연중 모든 주 동안 결과가있는 각 benchmarkitem을 표시하는 mysql 문을 만드는 데 문제가 있습니다. 다음은 내가 테스트 한 쿼리와 테이블 열입니다. 나는 codeigniter를 사용하고 결과를 하단에 매핑 된 형식으로 표시하려고 계획하고 있습니다. 나는이 긴 설명을 사과하지만 나는 가능한 한 명확히하려고 노력하고있다. 어떤 도움이라도 대단히 감사하겠습니다.MYSQL- 주 종료 날짜를 반복하면서 데이터 표시

tblbenchmarkitem 
================= 
itemID | itemDescription | itemTarget | ItemFreq | FKcategoryID 

tblbenchmarkData 
================ 
dataID | FKitemID | resultDate | result | dateAdded | dateModified 

tblcategories 
=============== 
categoryID | categoryName | parentID | FKdeptID 

tblcalendardates - stores 52 weeks for year Saturday to Friday 
================ 
id | year | startDate | endDate 

다음은이 모델에 대한 나의 mysql 문입니다. 올해의 함수 param에 기본값을 설정하려고합니다.

public function get_data() { 
    $dates = $this->db->query("SELECT endDate FROM tblcalendardates WHERE year = '2014' ORDER BY endDate"); 
    $dates = $dates->result(); 
    foreach ($dates as $date) { 
     //$query = "SELECT tblcalendardates.enddate, tblbenchmarkitems.itemDescription, tblbenchmarkitems.itemTarget, tblbenchmarkitems.itemFrequency, tblbenchmarkdata.resultDate, IFNULL(tblbenchmarkdata.result,0) AS result, (SELECT strCategoryName FROM tblcategories WHERE tblbenchmarkitems.FKcategoryID = tblcategories.categoryID) AS category FROM tblbenchmarkdata LEFT JOIN tblbenchmarkitems ON tblbenchmarkdata.FKitemID = tblbenchmarkitems.itemID JOIN tblcalendardates WHERE DATE(tblbenchmarkdata.resultDate) BETWEEN (SELECT MIN(DATE(tblcalendardates.startdate)) FROM tblcalendardates WHERE tblcalendardates.year = " . date('Y') . ") AND (SELECT MAX(DATE(tblcalendardates.enddate)) FROM tblcalendardates WHERE tblcalendardates.year = " . date('Y') . ") GROUP BY category ORDER BY tblcalendardates.enddate"; 
     $query = 'SELECT c.*, i.itemDescription, i.itemTarget, IFNULL(d.result, 0) as result,"' . $date ->endDate . '" FROM tblcategories AS c JOIN tblbenchmarkitems AS i ON i.FKcategoryID = c.categoryID JOIN tblbenchmarkdata as d ON i.itemID = d.FKitemID JOIN tblcalendardates WHERE DATE(d.resultDate) BETWEEN (SELECT MIN(DATE(tblcalendardates.startdate)) FROM tblcalendardates WHERE tblcalendardates.year = "' . date('Y') . '") AND (SELECT MAX(DATE(tblcalendardates.enddate)) FROM tblcalendardates WHERE tblcalendardates.year = "' . date('Y') . '") GROUP BY strCategoryName'; 
     $query = $this->db->query($query); 

    return $query->result(); 
    } 
} 

이이 나는 테이블에 데이터를 표시 할 방법을 내보기

<table class="table table-hover data-list"> 
    <thead> 
     <tr> 
      <th class="th-set-width1">Benchmark Item</th> 
      <th class="th-set-width2">Target</th> 
     <?php foreach($weeks as $week): ?> 
      <th class="th-set-width2"><?php echo date('m-d-Y', strtotime($week->enddate)); ?></th> 
     <?php endforeach; ?> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach($entries as $entry) : ?> 
     <tr> 
      <td><?php echo $entry->itemDescription; ?></td> 
      <td><?php echo $entry->itemTarget; ?></td> 
      <?php for ($i = 0; $i < 52; $i++) : ?> 
      <?php if ($entry->result != NULL || $entry->result > 0): ?> 
       <td><?php echo $entry->result; ?></td> 
      <?php else: ?> 
       <td>0</td> 
      <?php endif; ?> 
      <?php endfor; ?> 
     </tr> 
     <?php endforeach; ?> 
    </tbody> 
</table> 

입니다.

----------------------------------------------------------------------------------- 
categoryName 
==================================================================================== 
itemDescription | itemTarget | 01-03-14 | 01-10-14 | 01-17-14 | 01-24-14 | 01-31-14 
===================================================================================== 
No. of Visits | 12.00 | NULL | NULL | 15.00 | NULL | 20.00 
No. of Calls | 17.00 | 12.00 | NULL | 17.00 | 22.00 | NULL 

답변

0

내가 찾고있는 응답이 거의 내 코드에 약간 변경되었습니다. 유일한 문제는 각 항목에 대해 하나의 결과가 표시된 두 개의 항목이 있다는 것입니다.

내 모델 :

public function get_data() { 
    $items = $this->db->query('SELECT * FROM tblbenchmarkitems'); 
    $items = $items->result(); 
    foreach ($items as $item) { 
     $query = 'SELECT c.endDate, IFNULL(d.result,0) as result FROM tblbenchmarkdata AS d RIGHT JOIN tblcalendardates as c ON (DATE(d.resultDate) = c.endDate) JOIN tblbenchmarkitems as i WHERE c.year = "2014" AND i.itemID = "' . $item->itemID .'" GROUP BY c.endDate'; 
     $query = $this->db->query($query); 
     $data[] = array(
      'itemID'   => $item->itemID, 
      'itemDescription' => $item->itemDescription, 
      'itemTarget'  => $item->itemTarget, 
      'dates'    => $query->result() 
     ); 
    } 
    return $data; 
} 

내보기 : 쿼리 결과에서

<table class="table table-hover data-list"> 
    <thead> 
    <tr> 
     <th class="th-set-width1">Benchmark Item</th> 
     <th class="th-set-width2">Target</th> 
    <?php foreach($weeks as $week): ?> 
     <th class="th-set-width2"><?php echo date('m-d-Y', strtotime($week->enddate)); ?></th> 
    <?php endforeach; ?> 
    </tr> 
    </thead> 

    <tbody> 

    <?php for($n = 0; $n < count($entries); $n++) : ?> 

    <tr> 
     <td><?php echo $entries[$n]['itemDescription']; ?></td> 
     <td><?php echo $entries[$n]['itemTarget']; ?></td> 
     <?php for($i = 0; $i < count($entries[$n]['dates']); $i++) :?> 
     <td><?php echo $entries[$n]['dates'][$i]->result; ?></td> 
     <?php endfor; ?> 
    </tr> 

    <?php endfor; ?> 

    </tbody> 

</table> 

표 형식 - 문제는 기록 하나는이 두 결과가 표시되고있는 기록이다. 기록 하나 11/7/14 기록이에 기록 된 결과를 가지고하는 것은 7/11/14에 기록 된 결과가 있습니다

+-----------------------------------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+ 
|Benchmark Item       | Target |01-03-2014|01-10-2014|01-17-2014|01-24-2014|01-31-2014|02-07-2014|02-14-2014|02-21-2014|02-28-2014|03-07-2014|03-14-2014|03-21-2014|03-28-2014|04-04-2014|04-11-2014|04-18-2014|04-25-2014|05-02-2014|05-09-2014|05-16-2014|05-23-2014|05-30-2014|06-06-2014|06-13-2014|06-20-2014|06-27-2014|07-04-2014|07-11-2014|07-18-2014|07-25-2014|08-01-2014|08-08-2014|08-15-2014|08-22-2014|08-29-2014|09-05-2014|09-12-2014|09-19-2014|09-26-2014|10-03-2014|10-10-2014|10-17-2014|10-24-2014|10-31-2014|11-07-2014|11-14-2014|11-21-2014|11-28-2014|12-05-2014|12-12-2014|12-19-2014|12-26-2014| 
+-----------------------------------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+ 
|Total No. of Calls Conducted    17.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 0.00  0.00  0.00  0.00  **2.00**  0.00  0.00  0.00  0.00 0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.00  0.00 **12.00**  0.00  0.00  0.00  0.00  0.00  0.00  0.00 | 
+-----------------------------------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+ 
|Total No. of Random Visits     29.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 0.00  0.00  0.00  0.00  **2.00**  0.00  0.00  0.00  0.00 0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.00  0.00 **12.00**  0.00  0.00  0.00  0.00  0.00  0.00  0.00 | 
+-----------------------------------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+ 
0

모델에서

해결을, 나는 쿼리 I 변경 tblbenchmarkitems 테이블에 대한 조인을 제거하고 where 문에 itemID을 지정해야하는 필요성을 제거했습니다. FKitemID 필드를 출력에 추가하여 결과가 평가되는 특정 항목에 해당하는지 확인합니다 (즉, 항목 ID가 1이고 배열에 제공된 결과가 ID 1 인 항목에 속한 경우 데이터를 표시 함). ID 01 만 해당)

벤치 마크 데이터를 가져 오는 기능입니다.

public function get_data() { 
    $items = $this->db->query('SELECT * FROM tblbenchmarkitems'); 
    $items = $items->result(); 
    foreach ($items as $item) { 
     $query = 'SELECT c.endDate as weekending, IFNULL(d.result,0) as result, d.FKitemID FROM tblbenchmarkdata AS d RIGHT JOIN tblcalendardates as c ON (DATE(d.resultDate) = c.endDate) WHERE c.year = "2014" GROUP BY weekending'; 
     $query = $this->db->query($query); 
     $data[] = array(
      'itemID'   => $item->itemID, 
      'itemDescription' => $item->itemDescription, 
      'itemTarget'  => $item->itemTarget, 
      'dates'    => $query->result() 
     ); 
    } 
    return $data; 
} 

보기는 :

<table class="table table-hover data-list"> 
    <thead> 
    <tr> 
     <th class="th-set-width1">Benchmark Item</th> 
     <th class="th-set-width2">Target</th> 

    <?php foreach($weeks as $week): ?> 
     <th class="th-set-width2"><?php echo date('m-d-Y', strtotime($week->enddate)); ?></th> 
    <?php endforeach; ?> 

    </tr> 
    </thead> 

    <tbody> 

    <?php for($n = 0; $n < count($entries); $n++) : ?> 
    <tr> 
     <td><?php echo $entries[$n]['itemDescription']; ?></td> 
     <td><?php echo $entries[$n]['itemTarget']; ?></td> 

    <?php for($i = 0; $i < count($entries[$n]['dates']); $i++) :?> 
     <?php if ($entries[$n]['itemID'] == $entries[$n]['dates'][$i]->FKitemID) : ?> 

     <td><?php echo $entries[$n]['dates'][$i]->result; ?></td> 

     <?php else: ?> 

     <td>0.00</td> 

     <?php endif; ?> 
    <?php endfor; ?> 

    </tr> 
    <?php endfor; ?> 

    </tbody> 
</table> 

마지막 뷰에서 I는를 추가 한 경우 루프 (몇 주에 걸쳐 데이터를 출력하는 것)에 대한 제 후 문. 출력중인 항목의 ID가있는 결과가 있는지 확인하고 행의 & 열에 만 표시하면 해당 항목이 표시됩니다. 그렇지 않으면 "0"을 표시하십시오.00 ".

관련 문제