Data is missing from Query - mysql

I have this query to combine dates in a date field of sales.
Once the person has more then 3 purchase dates and we run the query, they do not show up at all. If they have two or three it's not a problem, as soon as the have four dates to combine it just skips the line all together.
<?php
set_time_limit(0);
$server = 'localhost';
$login = 'xxxx';
$password = 'xxxx';
$db = 'xxx';
$table = 'xxx';
$filename = '/fakepath/file.csv';
mysql_connect($server, $login, $password);
mysql_select_db($db);
$fp = fopen($filename, "w");
$query = 'SET GLOBAL group_concat_max_len=15000';
mysql_query($query);
$res = mysql_query("SELECT t1.employee_number, t1.first_name, t1.last_name
, GROUP_CONCAT( DATE_FORMAT( t1.dates_of_purchase, '%m-%d-%y' ) SEPARATOR ' & ' ) AS Purchase_Date
, t1.Dept, t1.Location
FROM XXX AS t1
LEFT JOIN XXX AS t2 ON ( t1.employee_number = t2.employee_number
AND t1.last_name = t2.last_name
AND t1.QTY != t2.QTY )
WHERE t1.dates_of_purchase >= '2011-01-01'
AND t1.QTY >=1
AND t2.employee_number IS NULL
GROUP BY t1.employee_number, t1.first_name, t1.last_name
ORDER BY t1.employee_number
");
mysql_data_seek($res, 0);
while($row = mysql_fetch_assoc($res)) {
$line = "";
$comma = "xxx,";
foreach($row as $value) {
$line .= $comma . '' . str_replace('', '', $value) . '';
$comma = ",";
}
$line .= "\n";
fputs($fp, $line);
}
fclose($fp);`
This is the code and it works for the most part.
Why does it not return any rows for more then 3 purchase dates?
Here is the data:
Account employee_number first_name last_name dates_of_purchase Dept Location QTY Vendor id
ONTH30 652526 G HARRIPERSAD 2011-02-16 PWU NUCLEAR SUPPLY CHAIN 1 MSS 1836
ONTH30 652561 AJJ FELS 2011-09-15 PWU NOSS 1 MSS 4319
ONTH30 652561 AJJ FELS 2011-06-23 PWU NOSS 1 MSS 8898
ONTH30 652561 AJJ FELS 2011-08-18 PWU NOSS 1 MSS 8938
ONTH30 652526 G HARRIPERSAD 2011-11-16 PWU PICKERING 1 MSS 7231
ONTH30 652561 AJJ FELS 2011-12-08 PWU NOSS -1 MSS 8141
ONTH30 652561 AJJ FELS 2011-10-13 PWU NOSS 1 MSS 9055
The data missing from the output is employee number 652561.
It does not get output to the CSV file it's skips it all together for some reason.
The output should look like this:
opg, 652561, AJJ, FELS, 10-13-2011 & 12-08-2011 & 08-18-2011 & 06-23-2011 & 09-15-2011 , ESR, PICKERING A

Related

How to pivot my table in mysql

I have table database like this :
i want to make on table view like this :
Thanks.
I assume you need it per year basis.
Select year, id_lesson,
Sum(case when term = 1 then exam_1 end) exam_1_term_1,
Sum(case when term = 1 then exam_2 end) exam_2_term_1,
Sum(case when term = 2 then exam_1 end) exam_1_term_2,
Sum(case when term = 2 then exam_2 end) exam_2_term_2,
Sum(case when term = 3 then exam_1 end) exam_1_term_3,
Sum(case when term = 3 then exam_2 end) exam_2_term_3
From your_table
Group by id_lesson, year;
Try this logic, Hope it is help.
$qdata = mysqli_query("SELECT * FROM table ORDER BY ID_Lesson");
$rows = array();
if (mysqli_num_rows($qdata) > 0) {
// Store all exam based on ID_Lesson ar array
while ($data = mysqli_fetch_array($qdata)) {
if (!isset($row[$data['ID_Lesson']])) {
$rows[$data['ID_Lesson']] = array($data['ID_Lesson'], $data['Exam1'], $data['Exam2']);
}
else {
$rows[$data['ID_Lesson']][] = $data['Exam1'];
$rows[$data['ID_Lesson']][] = $data['Exam2'];
}
}
$html = '';
// Print per element as <tr> row
foreach ($rows as $k => $row) {
$html .= "<tr><td>" . implode("</td><td>", $row) . "</td></tr>";
}
}

mysql query returning different dates

I am trying to add a new feature to my weather station, date of highest temperature and the date it rained the most.
Why do does this code give different dates in the result?
If I changed the first query to either T, R, P or H, the maximum for the row are shown but the date is always the same.
Once I understand that, I may be able to fix other problems with the code.
This is the output
$result = mysqli_query($con,"
SELECT DateTime,max(Tmax)
FROM alldata
WHERE YEAR(DateTime) = YEAR(NOW())
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate1 = $row['DateTime'];
$tempMax1 = $row['max(Tmax)'];
}
$result = mysqli_query($con,"
SELECT DateTime,Tmax
FROM alldata
WHERE Tmax=(select max(Tmax) from alldata)
AND YEAR(DateTime) = YEAR(NOW())
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate = $row['DateTime'];
$tempMax = $row['Tmax'];
}
$result = mysqli_query($con,"
SELECT DateTime,Tmax
FROM alldata
WHERE YEAR(DateTime) = YEAR(NOW())
ORDER BY Tmax DESC
LIMIT 1
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate = $row['DateTime'];
$tempMax = $row['Tmax'];
}

Laravel Join table and query

I have 3 table. Users, Accounts and Booking. I have try three table join and year and month wise query. [3 Table diagram][1]
But query booking calculation is show wrong. It's showing double quantity.
$january = DB::table('users')
->join('bookings', 'users.id', '=', 'bookings.user_id')
->join('accounts', 'users.id', '=', 'accounts.user_id')
->orderBy('users.room_id','asc')
->groupBy('users.id')
->whereYear('bookings.bookingdate','=', '2016')
->whereMonth('bookings.bookingdate','=','01')
->whereYear('accounts.accountdate','=', '2016')
->whereMonth('accounts.accountdate','=','01')
->select('users.*',
DB::raw("COUNT(case when bookings.breakfast='on' then 1 else null end) AS t_breakfast"),
DB::raw("COUNT(case when bookings.lunch='on' then 1 else null end) AS t_lunch"),
DB::raw("COUNT(case when bookings.dinner='on' then 1 else null end) AS t_dinner"),
DB::raw("SUM(accounts.amount) AS t_amount")
)
->get();
dd($january);
My Accounts table is:
Accounts Table
My Booking table is:
Bookings Table
When I'm run this query it's show:
+"t_breakfast": "2"
+"t_lunch": "2"
+"t_dinner": "2"
+"t_amount": "22.00"
But I need t_breakfast, t_lunch, t_dinner quantity is: 1. But It's showing double.
When I try one query I did not solved this problem. then I try two query and solved it.
$data = DB::select("SELECT users.id, users.name , users.picture, users.room_id,
SUM(CASE WHEN bookings.breakfast='on' THEN 1 ELSE 0 END) AS t_b ,
SUM(CASE WHEN bookings.lunch='on' THEN 1 ELSE 0 END) AS t_l ,
SUM(CASE WHEN bookings.dinner='on' THEN 1 ELSE 0 END) AS t_d FROM `users`
INNER JOIN
`bookings` ON users.id=bookings.user_id AND
MONTH(bookings.bookingdate) = $month AND
YEAR(bookings.bookingdate) = $year
GROUP BY users.id
ORDER BY users.room_id ASC");
$datas = [];
$data = json_decode(json_encode($data),true);
foreach ($data as $key => $value) {
$x = [];
$x = $value;
$x['exp'] = Account::where('user_id',$x['id'])->whereMonth('accountdate','=',$month)->whereYear('accountdate','=',$year)->sum('amount');
$datas[] = $x;
}
$total_t_b = $total_t_l = $total_t_d = $total_exp = 0;
foreach ($datas as $data) {
$total_t_b += $data['t_b'];
$total_t_l += $data['t_l'];
$total_t_d += $data['t_d'];
$total_exp += $data['exp'];
}
$g_total = $total_t_b + $total_t_l + $total_t_d;
if($g_total <= 0) {
$perbookingprice = 0;
} else {
$perbookingprice = $total_exp / $g_total;
}

Query mysql for post and comments system

I'm using codeigniter for make a post and comments system. I did all the querys but I don't know how get the comments in the table post_comment. This is my query for take all the post shared. Thank you for the help.
function get_post_profile($user_id,$limit) {
$this->db->select('post_user.user_id,post_user.id_post_shared,post_shared.post_text,users.name,users.surname,users.id');
$this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
$this->db->join('users','users.id = post_user.user_id');
$this->db->where('post_user.user_id',$user_id);
$this->db->order_by('post_user.id_post_shared','DESC');
$this->db->limit($limit);
$query = $this->db->get('post_user');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
users_table
id | name | surname |
1 jhon Smith
2 Sally Dunk
post_user table
id_post_shared | user_id |
1 1
post_share table
id_post | post_text
1 Hello guys
post_comment table
comment_text | id_post | id_user
Hello! 1 2
Try and reply:
$this->select('post_comment.comment_text, post_comment.id_post, post_comment.id_user')->join('post_share', 'post_share.id_post = post_comment.id_post')->join('users', 'users.id = post_comment.id_user')->get('post_comment');
EDIT
function get_comments(){
$data = array();
$posts = array();
$posts = $this->db->select('id_post as post_id, post_text', false)->order_by('id_post', 'desc')->get('post_share', 10)->result_array(); #get first 10 posts
if( is_array( $posts ) && count( $posts ) > 0 ){
foreach( $posts as $key=>$each ){
## gather the comments for the posts ###
$comments = array();
$comments = $this->db->select('comment_text, id_user')->where('id_post', $each['post_id'])->get('post_comment')->result_array();
if( is_array( $comments ) && count( $comments ) ){
$posts[$key]['comments'] = $comments;
}
}
}
return $posts;
}
EDIT 1
if( isset( $posts ) && is_array( $posts ) && count( $posts ) > 0 ){
foreach( $posts as $key=>$each ){
echo "Post id :".$each['post_id']." Post txt: ".$each['post_text']."<br>";
if( isset( $each['comments'] ) && is_array( $each['comments'] ) && count( $each['comments'] ) ){
foreach( $each['comments'] as $subKey=>$subEach ){
echo "Comment Txt :".$subEach['comment_text']."<br>";
}
}
}
}
Add LEFT JOIN to your post_comment
$this->db->select('post_user.user_id,post_user.id_post_shared,
post_shared.post_text,users.name,users.surname,users.id,post_comment.comment_text');
$this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
$this->db->join('users','users.id = post_user.user_id');
$this->db->join('post_comment ','users.id = post_comment .id_user','LEFT');
$this->db->where('post_user.user_id',$user_id);
$this->db->order_by('post_user.id_post_shared','DESC');
$this->db->limit($limit);
$query = $this->db->get('post_user');
third parameter in the call is to specify the type of join join('table','relation','join type')
Try this:
$sql = "SELECT a.*,b.id,b.name,b.surname,c.id_post_shared,c.user_id,d.id_post,d.post_text
FROM post_comment a, users_table b, post_user c,post_share d
WHERE b.id = c.user_id
AND b.id = $user_id
AND c.id_post_shared = d.id_post
AND d.id_post = a.id_post";
$result = $this->db->query($sql)->result_array();
echo $result[0]['comment_text'];

mySQL: How do I do multiple subqueries and group by for use with datatables?

I want to generate the following data set for use with DataTables. For the filtering and sorting to work properly, using JOINS instead of subqueries is difficult, if not impossible (as far as I can tell).
Applicant Name, Position, Interviewer, Interview 1 Score, Interview 2 Score, Demo. Score, Interview Date
Here is how the data is stored in mySQL (simplified version):
applicant_scores table
id applicant_id interviewer_id score_type score datetime_recorded
1 2 3 Interview 1 80 2013-04-23 09:35:48
2 2 45 Interview 1 70 2013-04-23 10:14:23
3 2 3 Interview 2 85 2013-04-23 09:35:48
4 2 45 Interview 2 77 2013-04-23 10:14:23
5 2 3 Demonstration 76 2013-04-23 09:35:48
6 3 45 Interview 1 79 2013-04-23 10:14:23
7 3 3 Interview 1 86 2013-04-23 09:35:48
8 3 45 Interview 2 92 2013-04-23 10:14:23
applicants table
id first_name last_name
2 John Doe
3 Jane Doe
interviewer table
id first_name last_name
3 Santa Claus
45 Fred Flintstone
applicant_positions table
position_id applicant_id
1 2
1 3
positions table
id title
1 Advanced mySQL Programmer
I've tried everything that I can think of, but I'm having trouble grouping scores by interviewer and applicant correctly. What I expect to see is:
Applicant Name Position Interviewer Interview 1 Score Interview 2 Score Demo. Score Interview Date
John Doe Advanced mySQL Programmer Santa Claus 80 85 76 2013-04-23
John Doe Advanced mySQL Programmer Fred Flintstone 70 77 2013-04-23
Jane Doe Advanced mySQL Programmer Santa Claus 86 2013-04-23
Jane Doe Advanced mySQL Programmer Fred Flintstone 79 92 2013-04-23
Here is my current query which gives zero results:
SELECT SQL_CALC_FOUND_ROWS
CONCAT( a.first_name, ' ', a.last_name),
p.title,
CONCAT( i.first_name, ' ', i.last_name),
(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Interview 1') as score_1,
(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Interview 2') as score_2,
(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Demonstration') as score_3,
(SELECT DATE_FORMAT(datetime_recorded, '%m-%d-%Y') FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id) as interview_date
FROM applicants a, positions p, interviewers i, applicant_scores s
WHERE s.applicant_id = a.id AND i.id = s.interviewer_id
GROUP BY i.id
Here is the server side ajax code that is being used to feed DataTables... Here is info on how to create server side script for DataTables - http://www.datatables.net/release-datatables/examples/data_sources/server_side.html
<?php
/* Database connection information */
$gaSql['user'] = "";
$gaSql['password'] = "";
$gaSql['db'] = "";
$gaSql['server'] = "";
$q1="'";
$q2='"';
$name = "CONCAT( ".$q2."<input type='hidden' id='name' value='".$q2.", LOWER(a.last_name), ' ', LOWER(a.first_name), ".$q2."'><input type='hidden' id='applicant_id' value='".$q2.", a.id, ".$q2."'><a href='applicant_details.php?id=".$q2.", a.id, ".$q2."'><img src='img/search.png' border='0'></a> ".$q2.", a.last_name, ', ', a.first_name )";
$interviewer = "CONCAT( ".$q2."<input type='hidden' id='name' value='".$q2.", LOWER(u.lastname), ' ', LOWER(u.firstname), ".$q2."'><input type='hidden' id='interviewer_id' value='".$q2.", i.id, ".$q2."'><a href='interviewer_details.php?id=".$q2.", i.id, ".$q2."'><img src='img/search.png' border='0'></a> ".$q2.", u.lastname, ', ', u.firstname )";
$int_1_score = "(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Interview 1' AND s.interviewer_id = i.id) as score_1";
$int_2_score = "(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Interview 2' AND s.interviewer_id = i.id) as score_2";
$demo_score = "(SELECT score FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id AND s.score_type = 'Demonstration' AND s.interviewer_id = i.id) as score_3";
$interview_date = "(SELECT DATE_FORMAT(datetime_recorded, '%m-%d-%Y') FROM applicant_scores s, applicants a WHERE s.applicant_id = a.id) as interview_date";
$aColumns = array($name, 'p.title', $interviewer, $int_1_score, $int_2_score, $demo_score, $interview_date);
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
$sIndexTable = "formtemplates";
/* DB table to use */
$sTable = "applicants a, positions p, interviewers i, applicant_scores s, users u";
$sWhere = " WHERE s.applicant_id = a.id AND i.id = s.interviewer_id AND u.id = i.user_id";
$sGroupBy = " GROUP BY i.id";
/*
* MySQL connection
*/
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = " ORDER BY ";
for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
if(stripos($aColumns[ intval( $_GET['iSortCol_'.$i] ) ], "c.name", 1) >= 1){
$sOrder .= "c.name ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ).", ";
}elseif(stripos($aColumns[ intval( $_GET['iSortCol_'.$i] ) ], "t.firstName", 1) >= 1){
$sOrder .= "t.firstName ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ).", ";
}elseif(stripos($aColumns[ intval( $_GET['iSortCol_'.$i] ) ], "d.shortName", 1) >= 1){
$sOrder .= "d.shortName ".mysql_real_escape_string( $_GET['sSortDir_'.$i] ).", ";
}else{
$sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
".mysql_real_escape_string( $_GET['sSortDir_'.$i] ) .", ";
}
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
//define columns to filter on
$aFcolumns = array( 'u.username', 'u.firstname', 'u.lastname', "REPLACE (g.status, 'OUT_FOR_SIGNATURE', 'WAITING')");
if ( $_GET['sSearch'] != "" )
{
//$sWhere .= "WHERE (";
$sWhere .= " AND (";
for ( $i=0 ; $i<count($aFcolumns) ; $i++ )
{
$sWhere .= $aFcolumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aFcolumns) ; $i++ )
{
if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= $aFcolumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sGroupBy
$sOrder
$sLimit
";
//echo $sQuery; die(); //for firebug debugging
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
/* Total data set length */
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sIndexTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] != ' ' )
{
/* General output */
$row[] = $aRow[$i];
}
}
//add another row for buttons
//$row[] = "<div style='float:right;'><span style='padding-right:2px;'><a href=''><img src='global_img\user_info.png' border='0'></a></span><span style='padding-right:2px;'><a href=''><img src='global_img\user_edit.png' border='0'></a></span><span style='padding-right:0px;'><a href=''><img src='global_img\user_delete.png' border='0'></a></span></div>";
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>
I modified the server side script to attempt to use the solution provided by bluefeet and the following is the generated query:
SELECT SQL_CALC_FOUND_ROWS concat(a.first_name, ' ', a.last_name), p.title, CONCAT(i1.first_name, ' ', i1.last_name), max(case when ac.score_type='Interview 1' then ac.score else '' end), max(case when ac.score_type='Interview 2' then ac.score else '' end), max(case when ac.score_type='Demonstration' then ac.score else '' end), DATE_FORMAT(ac.datetime_recorded, '%m-%d-%Y')
FROM applicants a
inner join applicant_positions ap on a.id = ap.applicant_id inner join positions p on ap.position_id = p.id inner join applicant_scores ac on a.id = ac.applicant_id inner join interviewers i1 on ac.interviewer_id = i1.id
GROUP BY concat(a.first_name, ' ', a.last_name), p.title, CONCAT(i1.first_name, ' ', i1.last_name), DATE_FORMAT(ac.datetime_recorded, '%m-%d-%Y')
ORDER BY concat(a.first_name, ' ', a.last_name)
asc
LIMIT 0, 500
This gives me an empty result set though. HELP?
You were definitely on the right track to get the result.
You are currently missing a few joins in your query. You need to join the applicants to the positions using the applicant_positions table. You can also add a join to the applicant_scores table which will let you use an aggregate function with a CASE to get the columns containing the Interview/Demo Scores:
select
concat(a.first_name, ' ', a.last_name) ApplicantName,
p.title Position,
CONCAT(i1.first_name, ' ', i1.last_name) Interviewer,
max(case when ac.score_type='Interview 1' then ac.score else '' end) Interview1Score,
max(case when ac.score_type='Interview 2' then ac.score else '' end) Interview2Score,
max(case when ac.score_type='Demonstration' then ac.score else '' end) DemoScore,
DATE_FORMAT(ac.datetime_recorded, '%m-%d-%Y') InterviewDate
from applicants a
inner join applicant_positions ap
on a.id = ap.applicant_id
inner join positions p
on ap.position_id = p.id
inner join applicant_scores ac
on a.id = ac.applicant_id
inner join interviewers i1
on ac.interviewer_id = i1.id
group by concat(a.first_name, ' ', a.last_name),
p.title,
CONCAT(i1.first_name, ' ', i1.last_name),
DATE_FORMAT(ac.datetime_recorded, '%m-%d-%Y');
See SQL Fiddle with Demo. This give the result:
| APPLICANTNAME | POSITION | INTERVIEWER | INTERVIEW1SCORE | INTERVIEW2SCORE | DEMOSCORE | INTERVIEWDATE |
-------------------------------------------------------------------------------------------------------------------------------
| Jane Doe | Advanced mySQL Programmer | Fred Flintstone | 79 | 92 | | 04-23-2013 |
| Jane Doe | Advanced mySQL Programmer | Santa Claus | 86 | | | 04-23-2013 |
| John Doe | Advanced mySQL Programmer | Fred Flintstone | 70 | 77 | | 04-23-2013 |
| John Doe | Advanced mySQL Programmer | Santa Claus | 80 | 85 | 76 | 04-23-2013 |
SELECT CONCAT(b.first_name, ' ', b.last_Name) Applicant_Name,
CONCAT(c.first_name, ' ', c.last_Name) Interviewer_Name,
e.Title,
MAX(CASE WHEN a.score_type = 'Interview 1' THEN a.score END) `Interview 1 Score`,
MAX(CASE WHEN a.score_type = 'Interview 2' THEN a.score END) `Interview 2 Score`,
MAX(CASE WHEN a.score_type = 'Demonstration' THEN a.score END) `Demonstration Score`,
MAX(DATE(a.datetime_recorded)) `Interview Date`
FROM applicant_scores a
INNER JOIN applicants b
ON a.applicant_ID = b.ID
INNER JOIN interviewer c
ON a.interviewer_ID = c.id
INNER JOIN applicant_positions d
ON a.applicant_ID = d.applicant_ID
INNER JOIN positions e
ON d.position_ID = e.ID
GROUP BY CONCAT(b.first_name, ' ', b.last_Name),
CONCAT(c.first_name, ' ', c.last_Name),
e.Title
SQLFiddle Demo