I have a table wich keep phone call records. I need to get answered and not answered calls count for each hour ( example 8:00 to 9:00 ) in specificed date range ( example 1 march to 5 march ), i dont need to separate the date, just need to separate hours.
Already i used this query:
SELECT `event`, DATE(`time`) AS `date`, HOUR(`time`) AS `hour`, COUNT(*) AS `count` FROM `queue_log` WHERE `queuename` = 'Hozoori_Q' AND `event` IN ('CONNECT', 'RINGNOANSWER') AND `time` >= '2014-03-01' AND `time` <= '2014-03-05' GROUP BY `event`, `date`, `hour`;
the "event" field is call status.
this query output is something like this:
1 =>
array (size=4)
'event' => string 'CONNECT' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'count' => string '99' (length=2)
2 =>
array (size=4)
'event' => string 'RINGNOANSWER' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'count' => string '5' (length=2)
but i want to be like this:
1 =>
array (size=4)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'answered_count' => string '99' (length=2)
'not_answered_count' => string '5' (length=2)
is there any solution ?
Straight SQL answer:
SELECT DATE(`time`) AS `date`,
HOUR(`time`) AS `hour`,
SUM(`event` = 'CONNECT') AS `answered_count`,
SUM(`event` = 'RINGNOANSWER') AS `not_answered_count`
FROM `queue_log`
WHERE `queuename` = 'Hozoori_Q'
AND `event` IN ('CONNECT', 'RINGNOANSWER')
AND `time` >= '2014-03-01'
AND `time` <= '2014-03-05'
GROUP BY `date`, `hour`;
Related
I tried mutiple LEFT JOIN in mysql it works fine there but however if use the same query in php mysqli object method I am not getting what I get in direct mysql
HERE is the Query in MYSQL
SELECT f.*,o.*,fs.* FROM fruits f
LEFT JOIN orders o ON f.id = o.fruit_id
LEFT JOIN fruit_stock fs ON f.id = fs.f_id
MYSQL RESULT
id name price id fruit_id qty id f_id stock_qty
3 Banana 5 2 3 10 1 3 122
3 Banana 5 4 3 8 1 3 122
2 Apple 3 1 2 3 2 2 322
4 pomegranate 4 3 4 15 3 4 23
5 grape 3 NULLNULL NULL 4 5 12
1 mango 45 NULLNULL NULL NULLNULL NULL
Same query with php
$con1 = new mysqli('***','***','***','***');
$sel_sql = 'SELECT f.*,o.*,fs.* FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';
$result = $con1->query($sel_sql);
var_dump($result);
while($row = $result->fetch_assoc()){
var_dump($row);
}
I just var_dump the $row to see the result. When I see I am not getting id for the 6th row as seen on mysql instead I get null
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '10' (length=2)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '1' (length=1)
'name' => string 'Banana' (length=6)
'price' => string '5' (length=1)
'fruit_id' => string '3' (length=1)
'qty' => string '8' (length=1)
'f_id' => string '3' (length=1)
'stock_qty' => string '122' (length=3)
array (size=7)
'id' => string '2' (length=1)
'name' => string 'Apple' (length=5)
'price' => string '3' (length=1)
'fruit_id' => string '2' (length=1)
'qty' => string '3' (length=1)
'f_id' => string '2' (length=1)
'stock_qty' => string '322' (length=3)
array (size=7)
'id' => string '3' (length=1)
'name' => string 'pomegranate' (length=11)
'price' => string '4' (length=1)
'fruit_id' => string '4' (length=1)
'qty' => string '15' (length=2)
'f_id' => string '4' (length=1)
'stock_qty' => string '23' (length=2)
array (size=7)
'id' => string '4' (length=1)
'name' => string 'grape' (length=5)
'price' => string '3' (length=1)
'fruit_id' => null
'qty' => null
'f_id' => string '5' (length=1)
'stock_qty' => string '12' (length=2)
array (size=7)
'id' => null
'name' => string 'mango' (length=4)
'price' => string '45' (length=2)
'fruit_id' => null
'qty' => null
'f_id' => null
'stock_qty' => null
I am expecting to get the id from the fruit table(which is 1 and the fruit name is mango) but its returning null.
Not sure why is that happening. Any help?
fruit_stock TABLE
id f_id stock_qty
1 3 122
2 2 322
3 4 23
4 5 12
orders Table
id fruit_id qty
1 2 3
2 3 10
3 4 15
4 3 8
I don't want to care about id in other table I just want the main(fruits) table id and other corresponding whole data from other tables.
You can add alias to your ids or remove the useless ones (optional ones are commented):
$sel_sql = 'SELECT
f.*,
-- o.id as order_id,
o.fruit_id,
o.qty,
-- fs.id as fruit_stock_id,
fs.f_id,
fs.stock_id
FROM fruits f LEFT JOIN orders o ON f.id = o.fruit_id LEFT JOIN fruit_stock fs ON f.id = fs.f_id';
Then you will have only one index id on your array.
You can only have one key with a value of Id so when the query runs it is first 1 and then when it hits the 2nd Id it overwrites it with null note that you only have 1 Id in your array but two in the query. You will need to be a bit more selective in the query and alias some of the columns.
I have database records such as below
Name Date(timestamp) Time(timestamp)
I want to order them by time DESC. But it shows me incorrect order. It shows now:
Name 19:00
Other 18:30
One more 19:00
As you can see hours are not going from earliest to latest. I want it to be like this:
Other 18:30
Name 19:00
One more 19:00
What am i doing wrong?
SELECT * FROM table WHERE date='".$date."' ORDER BY book_time DESC
Try to use Timestamp() for specifying that you expect the fields you wanna extract data from as timestamps.
something like this:
SELECT timestamp( `timestamp` ) as 'timestamp'
FROM randomTable
ORDER BY 1 ASC;
it would be great if you can post screenshots of your output to make the issue clearer.
Are you sure that select only for the date that you want? Because I think that select multiple date not only one.
Also for what you want you need to use ASC instead of DESC.
Because I have try and it's work for me. This is my simple code :
$sql = "SELECT * FROM test WHERE date = '2016-02-10' ORDER BY time ASC";
$res = $DB->query($sql);
while($enr = $res->fetch()){
var_dump($enr);
}
With this DB :
And the result is :
array (size=8)
'id' => string '4' (length=1)
0 => string '4' (length=1)
'date' => string '2016-02-10' (length=10)
1 => string '2016-02-10' (length=10)
'time' => string '01:00:07' (length=8)
2 => string '01:00:07' (length=8)
array (size=8)
'id' => string '3' (length=1)
0 => string '3' (length=1)
'date' => string '2016-02-10' (length=10)
1 => string '2016-02-10' (length=10)
'time' => string '02:03:05' (length=8)
2 => string '02:03:05' (length=8)
The result is order in the ASC order like you want
Hope that helps you.
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have written this code to get some info from my database.
"SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
ORDER BY time DESC"
it's work great except one thing. Since this is a inbox script there are many message from same user. but i only need to check if there is a message from this user or not.
array (size=28)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
2 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:15:44' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
3 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:13:21' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg'(length=19)
4 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 01:58:59' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg'(length=19)
as you can see in the var_dump there are 3 john doe and 2 james bond. but I need only the last ones according to the time.
so in this case john doe from 19:53:58, and james bond from 16:59:50. Like this:
array (size=2)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
and if there are some other users i want to get their last records too.
how can i do this? is this possible with only one query?
try this one I tested.
"SELECT *
FROM (SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION
SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
) AS bynames
GROUP BY user_id ORDER BY time ASC"
Put the union in a subquery, order it in the main query, and limit that to the most recent row.
SELECT *
FROM (SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION
SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
)
ORDER BY time DESC
LIMIT 1
Untested
SELECT c.user_id
, MAX(c.time)
, u.user_firstname
, u.user_lastname
, u.user_profile_picture
FROM (
SELECT from AS user_id
, time
FROM chat
WHERE to = :id1
UNION
SELECT to AS user_id
, time
FROM chat
WHERE from = :id2
) as c
JOIN users u
ON u.user_id = c.user_id
GROUP BY c.user_id
, u.user_firstname
, u.user_lastname
, u.user_profile_picture
I have an array that looks like this:
array (size=21)
0 => string '2' (length=1)
1 => string '' (length=0)
2 => string '20' (length=2)
3 => string '19' (length=2)
4 => string '14' (length=2)
5 => string '13' (length=2)
6 => string '' (length=0)
7 => null
8 => null
9 => string '20' (length=2)
10 => null
11 => string '10' (length=2)
12 => string '' (length=0)
13 => null
14 => string '13' (length=2)
15 => null
16 => string '' (length=0)
17 => null
18 => null
19 => string '' (length=0)
20 => string '20' (length=2)
And I would like to create a new array from this array by grouping rows with the same string. e.g.
2 => string '20' (length=2) with 20 => string '20' (length=2) and with 9 => string '20' (length=2)
and
5 => string '13' (length=2) with 5 => string '13' (length=2)
etc.
and order the new created array rows based on how many times the string occures there.
Order need to be DESC from the most occurrences to the last like a classic top something chart (The most present strings are first and the least are low)
So, the modified array will look like this:
array (size=21)
0 => string '20' (length=2)
1 => string '13' (length=2)
...
I also need somehow to handle null results e.g. 17 => null to be not incorporated at all in the final array modified result.
This should do the trick:
// Filter the "null results" first
$myarray = array_filter($myarray, create_function('$arg', '
return !is_null($arg);
'));
$occurrences = array_count_values($myarray);
// EDIT: arsort preserves the key => value correlation
arsort($occurrences, SORT_NUMERIC);
var_dump(array_keys($occurrences));
Try this.
$result = array_count_values($a);
arsort($result);
$result = array_keys($result);
I have Doctrine query setup with mySQL as database:
$q = Doctrine_Query::create();
$q->select('make.id, make.make,
model.id, model.year, model.model')
->from('Make make')
->innerJoin('make.Models model');
$q->groupBy('make.id, model.id'); // <-- this is where the problem
$q->orderBy('make ASC');
$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);
$pager = new Doctrine_Pager($q, 1, 1); // just want one result to prove the concept
$items = $pager->execute();
This executes this three queries (from mysql log):
SELECT COUNT(*) AS num_results FROM (SELECT m.id FROM make m INNER JOIN model m2 ON m.id = m2.make_id GROUP BY m.id, m2.id) dctrn_count_query
SELECT DISTINCT m3.id FROM make m3 INNER JOIN model m4 ON m3.id = m4.make_id GROUP BY m3.id, m4.id ORDER BY m3.make ASC LIMIT 1
SELECT m.id AS m__id, m.make AS m__make, m2.id AS m2__id, m2.year AS m2__year, m2.model AS m2__model FROM make m INNER JOIN model m2 ON m.id = m2.make_id WHERE m.id IN ('33') GROUP BY m.id, m2.id ORDER BY m.make ASC
And the result set is
array
0 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '288' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string '159' (length=3)
1 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '289' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string 'MiTo' (length=4)
2 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '290' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string '159 SPORTWAGON' (length=14)
The problem is with second query that returns make.id which is used in third query to select 1 make (Alfa-Romeo which has 3 models). What I want is to return NUMBER OF make/model combinations.
If I change number of items returned in Doctrine_Pager to 2, I get 33 rows (because from two selected makes Alfa-Romeo has 3 models and Audi (which is next) has 30 models.
Where I'm making a mistake?
You can not combine multiple columns in a single group by. Try this...
$q->groupBy('make.id');
$q->addGroupBy('model.id');
or a little more compressed...
$q->groupBy('make.id')->addGroupBy('model.id');
$table->setAttribute(Doctrine_Core::ATTR_QUERY_LIMIT, Doctrine_Core::LIMIT_ROWS);
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en