I have a working script that, when executed, runs the following SQL query which is then written to a CSV. All of this works perfectly.
However, I'd like to adjust the formatting somewhat. It currently writes results by employee and orders by last night. I'd like to have a row at the end of all agent's records that said something like "Agent's Name: Totals" and then sums up the totals of their records.
For example, each row has their name, phone extension, and then several metrics which are either blank or have an 'x', and lastly a number representing time on the phone. So I'd like to total the x's in the appropriate fields, add the duration of time on the phone, and lastly a count of total calls (which would be a count of that employee's records).
I don't know if this should be done in the SQL, in the CSV code block or if there's a better way to store the metrics with PHP and do this programmatically.
I'm a rookie here, normally just relying on queries in MySQL workbench, so any help is much appreciated.
Here's the script:
$result = mysqli_query($conn2,
"SELECT
firstn
, lastn
, extension
, Recieved
, RecievedKnown
, Outbound
, outboundKnown
, Missed
, MissedKnown
, CallingNumber
, CalledNumber
, starttime
, endtime
, duration
, HOLDTIMESECS
, TERMINATIONREASONCODE
FROM (
SELECT
u.firstn
, u.lastn
, c.extension
, CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
, CASE WHEN LEGTYPE1 = 2 AND answered = 1 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS RecievedKnown
, CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 THEN 'x' ELSE '' END AS Outbound
, CASE WHEN LEGTYPE1 = 1 AND FINALLYCALLEDPARTYNO = k.phone_number THEN 'x' ELSE '' END AS outboundKnown
, CASE WHEN Answered = 0 THEN 'x' ELSE '' END AS Missed
, CASE WHEN ANSWERED = 0 AND CALLINGPARTYNO = k.phone_number THEN 'x' ELSE '' END AS MissedKnown
, a.CALLINGPARTYNO AS CallingNumber
, a.FINALLYCALLEDPARTYNO AS CalledNumber
, b.starttime AS starttime
, b.endtime AS endtime
, b.duration
, a.holdtimesecs
, a.terminationreasoncode
FROM ambition.session a
INNER JOIN ambition.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
INNER JOIN ambition.mxuser c ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
INNER JOIN jackson_id.users u ON c.extension = u.extension
LEFT JOIN ambition.known_numbers k ON a.callingpartyno = k.phone_number
WHERE date(b.ts) >= curdate()
AND LEGTYPE1 <> 12 -- This keeps the report from having blank spaces due to the 12 legtype.
AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
) x
ORDER BY lastn") or die(mysqli_error( $conn2));
$userDetails = array();
while($row = $result->fetch_array(MYSQLI_NUM)){
/* Let me take the extension as the key to store its respective counts */
$extension = $row['extension'];
/* Now in your while loop do all the calculations for the user */
if(!isset($userDetails[$extension])){
/* Since this is the first time for the extension your doin the calculations */
$userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
}else{
$userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
}
}
foreach($userDetails as $userDetail){
/* In the following line dump the respective userdetails to csv which will show summary */
fputcsv($fp, array_values($userDetails));
}
And a screenshot of the current CSV results in excel (omitted names/numbers, but you still see the idea):
So for extension 7312, under the first two rows would be a new row with something like:
7312's Totals | 0 | 0 | 0 | 0 | 2 | 2 | - | - | - | - | 76 | 0
Your looping into the sql query to output the same to CSV file, at that time keep the records in generic array and dump once the loop gets over.
For Eg:
$userDetails = array();
while($row = $result->fetch_array(MYSQLI_NUM)){
/* Let me take the extension as the key to store its respective counts */
$extension = $row['extension'];
/* Now in your while loop do all the calculations for the user */
if(!isset($userDetails[$extension])){
/* Since this is the first time for the extension your doin the calculations */
$userDetails[$extension]['missedCallCounts'] = 1; /* First time count */
}else{
$userDetails[$extension]['missedCallCounts'] += 1; /* Sum up the count */
}
}
Now loop into the $userDetails and dump the same to CSV
foreach($userDetails as $userDetail){
/* In the following line dump the respective userdetails to csv which will show summary */
fputcsv($fp, array_values($userDetails));
}
Related
I've been banging my head hard over this problem for the last 2-3 days trying to see the problem from as many different angles as possible but to no avail. I'm turning to the SO community for extra perspectives. Below is the code I have which prints all 9 product plans. I'm wanting to find and print the plan with pricing equals or closest to a given user input. How can I do this?
//arrays of productnames
$productnames=array(1=>"Beginner","Advanced","Expert");
//arrays of productlevels
$productlevels=array(1=>"Bronze","Silver","Gold");
//Get The Length of Product Name Array
$planname_array_length=count($productnames);
//Get The Length of Product Level Array
$planlevel_array_length=count($productlevels);
for ($prn=1; $prn <= $planname_array_length; $prn++) {//loop to create plan name indicators
for ($prl=1; $prl <= $planlevel_array_length; $prl++) {//loop to create plan level indicators
$getpoductsql = " SELECT name, level,productNameId,productLevelId,finalProductPrice
FROM (
SELECT wspn.productName AS name, wspl.productLevel AS level, wsp.productNameId AS productNameId, wsp.productPlanLevel AS productLevelId,
ROUND(SUM(`Price`) * 1.12) AS finalProductPrice,
FROM `products` ws
left join product_plan wsp on wsp.productId = ws.wsid
left join product_plan_level wspl on wsp.productPlanLevel = wspl.wsplid
left join product_plan_name wspn on wspn.wspnid = wsp.productNameId
WHERE wspn.productName = '$planname_array_length[$pn]' AND wspl.productLevel = '$planlevel_array_length[$pl]'
)
AS x ORDER BY ABS(finalProductPrice - $compareprice)"
$resultproducts = $conn->query($getpoductsql);
$prodArray = mysqli_fetch_array($resultproducts);
//print array of each plan
$resultArr = array('planNameID' => $prodArray['planNameId'],
'planName' => $prodArray['name'],
'planLevelID' => $prodArray['planLevelId'],
'planLevelName' => $prodArray['level'],
'planPrice' => $prodArray['finalProductPrice'];
//print arrays of products
echo json_encode($resultArr);
}
}
This will output 9 plans as follow :
{"planNameID":"1","productName":"Beginner","productLevelID":"1","productLevelName":"Bronze","productPrice":"15"}
Rather than performing a separate query for each product name and product level, do them all in one query, and let MySQL find the one with the closest price.
$getpoductsql = " SELECT name, level,productNameId,productLevelId,finalProductPrice
FROM (
SELECT wspn.productName AS name, wspl.productLevel AS level, wsp.productNameId AS productNameId, wsp.productPlanLevel AS productLevelId,
ROUND(SUM(`Price`) * 1.12) AS finalProductPrice,
FROM `products` ws
left join product_plan wsp on wsp.productId = ws.wsid
left join product_plan_level wspl on wsp.productPlanLevel = wspl.wsplid
left join product_plan_name wspn on wspn.wspnid = wsp.productNameId
WHERE wspn.productName IN ('Beginner', 'Advanced', 'Expert') AND wspl.productLevel IN ('Bronze', 'Silver', 'Gold')
GROUP BY productNameId, productLevelId
)
AS x ORDER BY ABS(finalProductPrice - $compareprice)"
forgive my formatting, I'm on mobile
Like Amr Berag said above, your result should be the first row returned from your query.
If you have a table like this:
ID value
---- ------
A 7
B 12
C 23
...
You can then SELECT from this table to find the closest to some value, like so:
(Assume your desired value is $VALUE)
SELECT id, value, ABS(value - $VALUE) AS diff
FROM your_table
ORDER BY diff ASC
This will return something like this (say $VALUE is 10):
id value diff
-- ------ ----
B 12 2
A 7 3
C 23 13
...
You can just pick the first row.
You may also be able to add a WHERE clause to only select the row with the least difference using the MIN function:
SELECT id, value, ABS(value - $VALUE) AS diff
FROM your_table
WHERE diff = MIN(diff)
The way you are doing it will produce invalid json, do it like this:
$result=array();
for ($prn=1; $prn <= $planname_array_length; $prn++) {
for ($prl=1; $prl <= $planlevel_array_length; $prl++) {
. . . // the other code
//print array of each plan
$resultArr = array('planNameID' => $prodArray['planNameId'],
'planName' => $prodArray['name'], 'planLevelID' => $prodArray['planLevelId'],
'planLevelName' => $prodArray['level'],
'planPrice' => $prodArray['finalProductPrice'];
//print arrays of products
$resul[]=$resultArr;
}//loop1
}//loop2
echo json_encode($result);
you should also add the limit 1 and do the rest in JS in the front end
I have a php script successfully running right now that writes results of a sql query into a CSV file, eventually mailing as an attachment. So far so good, but I need to add another similar query that just does some summing basically, however, I want it to run right after the first one and have them both Written into the CSV.
Hopefully/ideally, write the results of query 1, skip a few blank rows, and write the results of query 2. It would be nice If I could put a heading for the 2nd query that said "CSR Totals" But I'm not banking on that.
Anyway, I'm not sure how to go about that exactly. I would imagine using mysqli_multiple_queries? I don't want to refactor too much of what I have due to it working well, but I realize I'll have to change a few things. This just isn't my area of expertise so I'm hoping for some guided suggestions.
Here's the code: (notice I've concatenated the 2nd query but just to show my concept, I'm not sure if the syntax would be right.)
$result = mysqli_query($conn2,
"SELECT
firstn
, lastn
, extension
, Recieved
, RecievedKnown
, Outbound
, outboundKnown
FROM (
SELECT
firstn
, lastn
, c.extension
,CASE WHEN LEGTYPE1 = 2 AND ANSWERED = 1 THEN 'x' ELSE '' END AS Recieved
, case when LEGTYPE1 = 2 and answered = 1 and CALLINGPARTYNO = k.phone_number then 'x' ELSE '' end as RecievedKnown
, CASE WHEN ANSWERED = 1 AND LEGTYPE1 = 1 then 'x' ELSE '' end AS Outbound
, case when LEGTYPE1 = 1 and FINALLYCALLEDPARTYNO = k.phone_number then 'x' ELSE '' end as outboundKnown
FROM test.session a
INNER JOIN test.callsummary b ON a.NOTABLECALLID = b.NOTABLECALLID
right join jackson_id.users c on a.callingpartyno = c.extension or a.finallycalledpartyno = c.extension
LEFT JOIN test.known_numbers k ON a.callingpartyno = k.phone_number
WHERE a.ts >= curdate()
and(a.CALLINGPARTYNO in (select extension from test.test_users) OR a.finallycalledpartyno IN (select extension from test.test_users))
) x
ORDER BY extension;") or die(mysqli_error( $conn2));
$result .= mysqli_query($conn2,
"//2nd query in here") or die(mysqli_error( $conn2));
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysqli_num_fields($result);
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
$headers[] = $fieldinfo->name;
}
$fp = fopen('test.csv', 'w');
if ($fp && $result) {
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
fclose($fp);
}
$file = "test.csv";
I have a Part Management system I've created in PHP with MySQL. What I'm trying to create is something that will generate the next Part Number for me. All part numbers start with a 3 letter prefix (which is determined by the product family/category) followed by their number.
For example 'ABC001'
What I have below is something that I'd like to use to determine what the next number is having already 'ABC001', 'ABC002' & 'ABC003' so I would like it to recognize what the next number is by querying until the query comes back false because that product number doesn't exist yet.
$abc_query = "SELECT * FROM products WHERE id LIKE 'ABC%'";
$abc_result = $mysqli2->query($abc_query);
while($row = $abc_result->fetch_assoc()) {
$rowid = $row["id"];
$pnumber = substr($rowid, 3, 3);
echo $pnumber. '<br/>';
$int = (int)$pnumber;
$abc_query2 = "SELECT * FROM products WHERE id 'ABC" . sprintf('%03s', $int);
for ($abc_query2 = true; $abc_query2 = false; $int++){
echo $int;
}$abc_nextnumber = $int +1;
}
$abc_newnumber = 'ABC' . sprintf('%03s', $abc_nextnumber);
echo $abc_newnumber;
The result I get is
001
002
003
005
ABC006
However the result should be..
001
002
003
ABC004
code update I've updated the code but it doesn't seem to stop at ABC004 if I have an 005. It will go to 006.
You should have the db do this instead of your app:
select t.id_prfx, max(t.id_num) as latest_num from
(select substring(id, 1, 3) as id_prfx,
cast(substring(id,4) as integer) as id_num) t
group by id_prfx
This will give you a result table where you get the highest part number for each prefix.
If you really really only want prefixes of 'ABC' then:
select max(cast(substring(id,4) as integer)) as max_num from table
where id LIKE 'ABC%'
Could you try this query?
SELECT MAX(SUBSTR(id, 4)) as last_id FROM products WHERE SUBSTR(id, 1, 3)='ABC'
EDİT:
products TABLE
==============
ABC001
ABC002
ABC003
ABC005
==============
We want to find 4 in products table.
SELECT SUBSTR(t1.id, 4) + 1 as POSSIBLE_MIN_ID
FROM products t1
WHERE NOT EXISTS (
SELECT *
FROM products t2
WHERE SUBSTR(id, 1, 3)='ABC' AND SUBSTR(t2.id, 4) = SUBSTR(t1.id, 4) + 1
) LIMIT 1
RESULT:
POSSIBLE_MIN_ID : 4
If anyone knows how I can have it add automatic zeros to the into the query (as it will be different amount of 0s once it gets to 'ABC011') instead of typing them in that would also be very helpful.
Here's how to automatically handle the prepended zeroes.
$sql3 = "SELECT * FROM products WHERE id 'ABC" . sprintf('%03s', $int);
I have an SQL query that fetches posts from a database. Everything works fine, but now I need to order the results by the number of comments each post has. The comments are on a separate table and they have a post_id column that links it to the post. I need to order the posts by the count of the comments table based on a shard ID? I have tried everything but every time I try to add something to my query it stops running completely and leaves my page blank. I need help to know where to put the other JOIN statement. This is my query:
$union = "UNION ALL
(
SELECT DISTINCT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
LEFT JOIN walllikes_track
ON wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id."
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) ";
The comments table is called wallcomments and it has a column called post_id. I know I need to use JOIN and COUNT but I don't know where to put it within my current code.
Try this query, I didn't run but i updated it.
SELECT wallposts.p_id,wallposts.is_profile_notes,wallposts.times_viewed,wallposts.columnTimesShared,
wallposts.marked,wallposts.secure_id,wallposts.reshared,wallposts.group_id,
wallposts.totaluploads,wallposts.WallUploadID,wallposts.type,
wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,
wallposts.privacy,wallposts.tagedpersons,wallposts.with_friends_tagged,wallposts.emotion_head,wallposts.selected_emotion,wallposts.title,
wallposts.url,wallposts.description,wallposts.cur_image,
wallposts.uip,wallposts.likes,wallposts.userid,
wallposts.posted_by,wallposts.post as postdata,wallusers.*,
UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
PosterTable.mem_pic as posterPic, PosterTable.gender as posterGender,PosterTable.oauth_uid as poster_oauth_uid, PosterTable.username as posterUsername,
PosterTable.mem_fname as posterFname,PosterTable.work as posterWork,
PosterTable.mem_lname as posterLname,walllikes_track.id as PostLikeFound,wallposts.date_created
FROM
wallusers,wallusers as PosterTable, wallposts
WHERE
wallusers.active = 1
AND
PosterTable.active = 1
AND
wallposts.group_id IN (".$groups.")
AND
wallposts.group_id != 0
AND
PosterTable.mem_id = wallposts.posted_by
AND
wallposts.marked < ".$this->flagNumber."
AND
wallusers.mem_id = wallposts.posted_by ) " AND wallposts.p_id = walllikes_track.post_id AND walllikes_track.member_id = ".$user_id.";
A more readable query might look like this...
At least then we'd have a chance.
SELECT DISTINCT p.p_id
, p.is_profile_notes
, p.times_viewed
, p.columnTimesShared
, p.marked
, p.secure_id
, p.media...
FROM wallposts p...
I have a PHP script which runs 2 queries. The first query is
"SELECT PID , EXPERIENCE , LASTLOGIN , WON , LOST , TIED , PRODUCT1_COUNT , PRODUCT2_COUNT , PRODUCT3_COUNT , AD_FREE , CODENAME , FB_UID , WSBALANCE , BEST_GAME_SCORE , BEST_WORD_SCORE , BEST_MOVE_SCORE , MW_PLAYED
FROM user
WHERE (PID > 9 AND (('. $serverTime .'- LASTLOGIN < 1209600) OR (AD_FREE + PRODUCT1_COUNT + PRODUCT2_COUNT + PRODUCT3_COUNT + PRODUCT4_COUNT ) > 0 ))"
Then the next step in the script is to iterate through each row of this query to build an html table. In the course of that I am querying a second table (games1) to get a count of the number of active games the first query returns.
$id = $row['PID'];
$pSQL = "SELECT COUNT(SID) AS c
FROM games1
WHERE ACTIVE = 1 AND (P1_UID = $id OR P2_UID = $id OR P3_UID = $id OR P4_UID = $id)";
I've been trying to wrap my brain around how to use join to combine these two queries, but I'm just not getting it. I'd greatly appreciate the help as this combination will save our script a bit of time.
You can use join,below query will give the data from user table and the count from game table per user
SELECT
u.PID,
u.EXPERIENCE,
u.LASTLOGIN,
u.WON,
u.LOST,
u.TIED,
u.PRODUCT1_COUNT,
u.PRODUCT2_COUNT,
u.PRODUCT3_COUNT,
u.AD_FREE,
u.CODENAME,
u.FB_UID,
u.WSBALANCE,
u.BEST_GAME_SCORE,
u.BEST_WORD_SCORE,
u.BEST_MOVE_SCORE,
u.MW_PLAYED ,
COUNT(g.SID) AS c
FROM
`user` u
LEFT JOIN games1 g
ON (
(g.P1_UID = u.PID
OR g.P2_UID = u.PID
OR g.P3_UID = u.PID
OR g.P4_UID = u.PID) AND g.ACTIVE = 1
)
WHERE (PID > 9
AND (( '. $serverTime .' - u.LASTLOGIN < 1209600)
OR (u.AD_FREE + u.PRODUCT1_COUNT + u.PRODUCT2_COUNT + u.PRODUCT3_COUNT + u.PRODUCT4_COUNT) > 0)
)
GROUP BY u.PID