How to agregate/group php array by SQL timedate/unix timestamp? - php

How to count/group data from array into hours from MySQL timedate format or unix timestamp?
My array:
Array (
[0] => Array (
[0] => id
[1] => name
[2] => 2013-01-10 00:36:00
)
[1] => Array (
[0] => id
[1] => name
[2] => 2013-01-10 00:36:00
)
)
How to shrink thousands of items like this and agregate them somehow to hours or even days? I just need rough number of rows with given hour in specyfic day.

Valid code for my case from answer from #Robert Seddon-Smith:
SELECT id, name, day, key
FROM table
WHERE day > '2013 6-01 00:00:00' && day < '2013-6-31 23:59:59'
GROUP BY YEAR(day), MONTH(day), DAY(day), HOUR(day), SECOND(day)

try SELECT * from table GROUP BY HOUR(date_column) WHERE condition...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour

Related

Laravel 4.2 get records of current month

i got a problem in my project i am using Laravel 4.2.
Piece of code which have problem..
$xx = xx::where('user_id','=',$user_id)
->where('date','>=',$first_day) //$first_day is first day of any month
->where('date','<=',$last_day)//$last_day is last day of same month
->get();
print_r($xx);die();
//records present but empty array for current month but working fine for any previous month
Result
Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) )
Laravel last query log
Array ( [query] => select * from x_xx where user_id = ? and date
= ? and date <= ? [bindings] => Array ( [0] => 17 [1] => 2016-04-1 [2] => 2016-04-30 ) [time] => 0.69 )
Running in mysql phpmyadmin i got 15 records
select * from x_xx where user_id = 17 and date >= '2016-04-1'
and date <= '2016-04-30' //15 total records found in mysql

Compare two DATETIME fields from multidimensional array and return a value or index

I have a user table. I have 2 DATETIME per row that I'm looking at. I will look at $lastLog date and $lastReceived (for last received lead) and determine which is newer and that becomes their $eligibleTime. We will then look for the oldest $eligibleTime to select which user should receive the lead. I am filtering through users to make sure they are licensed in the appropriate state that the lead is in and also that they haven't reached their daily quota.
I'm thinking maybe I built the multi-d array wrong and should have grouped instead all of the $lastLog dates and $lastReceived dates into their own arrays and run max($array) or something like this.
How can I look at this? I've built a multidimensional array using:
$sql = "SELECT `lastLog`,`firstName`,`lastReceived` FROM customers";
$sql = mysqli_query($conn,$sql);
$result = array();
while($line = mysqli_fetch_assoc($sql)){
$result[] = $line;
}
It is returning the following data:
> Array ( [0] => Array ( [lastLog] => 2015-06-12 02:00:00 [firstName] => Nathaniel [lastReceived] => 2015-06-12 05:16:10 ) [1] => Array ( [lastLog] => 2015-06-12 01:00:00 [firstName] => Ignacio [lastReceived] => 2015-06-01 10:00:00 ) [2] => Array ( [lastLog] => 2015-06-12 00:00:00 [firstName] => James [lastReceived] => 2015-06-08 00:00:00 ) [3] => Array ( [lastLog] => 2015-06-12 04:00:00 [firstName] => Robert [lastReceived] => 2015-06-10 00:00:00 ) )
Thank you, nate
Thanks to Marc B this got solved.
$sql = mysqli_query($conn,
"SELECT firstName, greatest(lastLog, lastReceived) AS eligibleTime
FROM customers
ORDER BY eligibleTime DESC
LIMIT 1");
$result = mysqli_fetch_assoc($sql);
print_r($result);

Get the highest value of array query mysql, php

With a big Thanks to others here i get this query now. its working fine, but i would need to get just the highest value of the post_id (how_many). it seems not possible to set a MAX(count(*)) on that. so how could i change this to get only the highest count by every id? i just need the value of every id where count == highest. how could i do this? thanks for any help.
$test = $wpdb->get_results('select
posts_id, value,count(*) as how_many
From wp_mrp_rating_item_entry_value
group by
posts_id, value
order by count(*) desc');
echo '<pre>';
print_r($test);
echo '</pre>';
i would need something like order by MAX((count(*)) or MAX((count(how_many))
i already read this but i dont know how to use this for my purpose Filtering log file using COUNT, GROUP BY, ORDER BY MAX
this is an example of the output i get now. so number 1 should not appear because number [0] has been voted 2 times. (how_many). i just need every id 1x in the output. no id shouldt appear twice or more. cause just the highest count is needed. thanks and sorry for the bad english.
Array
(
[0] => stdClass Object
(
[posts_id] => 336
[value] => 8
[how_many] => 2
)
[1] => stdClass Object
(
[posts_id] => 336
[value] => 7
[how_many] => 1
)
[2] => stdClass Object
(
[posts_id] => 380
[value] => 5
[how_many] => 1
)
[3] => stdClass Object
(
[posts_id] => 378
[value] => 7
[how_many] => 1
)
[4] => stdClass Object
(
[posts_id] => 329
[value] => 2
[how_many] => 1
)
[5] => stdClass Object
(
[posts_id] => 327
[value] => 3
[how_many] => 1
)
)
You can filter the resulting groups in a HAVING clause based on a match against a subquery for the maximal count:
SELECT posts_id, value, COUNT(*) AS how_many
FROM wp_mrp_rating_item_entry_value t1
GROUP BY posts_id, value
HAVING how_many = (
SELECT COUNT(*)
FROM wp_mrp_rating_item_entry_value t2
WHERE t2.posts_id = t1.posts_id
GROUP BY t2.value
ORDER BY COUNT(*) DESC
LIMIT 1
)

PHP & MySQL, laying out in CSV format with special parameters

I have a database that has budgets in them (for this, I have modified the budgets to be fake).
Long story short, I am attempting to run the following SQL query on the database, and then display a report based on it.
Here is the query: SELECT MONTHNAME(date) AS month, product, SUM(amount) AS spend FROM client_budgets WHERE advertiser_id = '$advertiser_id' GROUP BY MONTHNAME(date), product ORDER BY MONTH(date) ASC
I then clean the data a little, here is the code for that:
foreach($data AS $key => $val) {
$clean_data[] = $val;
}
From this, I get the following (this is a PHP array of the data. I have cut it off since it has about 100K rows in the database currently).
Array
(
[0] => Array
(
[month] => January
[product] => Internet
[spend] => 12000.00
)
[1] => Array
(
[month] => January
[product] => Radio
[spend] => 12250.00
)
[2] => Array
(
[month] => February
[product] => Billboards
[spend] => 6000.00
)
[3] => Array
(
[month] => February
[product] => Internet
[spend] => 16000.00
)
)
My goal is to end up being able to display in a CSV (I already have the headers set for the CSV), the following:
Month, Internet, Radio, Billboards, Television
January, 12000, 12250, 6000, 0
February, 16000, 7000, 6000, 2000
....
I am stuck right now trying to sort the data correctly, and if there is no data for Television for instance one month, to display a 0. Any help would be appreciated! I have been attempting this for almost 3 days now.

selecting all orders for last 30 days, and counting how many per day

I am trying to select all orders for last 30 days from one customer, so I need to have customer_id = "$customer_id" and count how many orders I have per each day for that one customer.
I need to end up with array like this
Array (
[1] => Array (
[orders] => 41
[date] => 2011-06-13 17:43:50
)
[2] => Array (
[orders] => 11
[date] => 2011-07-13 17:43:50
)
[4] => Array (
[orders] => 2
[date] => 2011-12-13 17:43:50
)
and so on... for 30 days, if some day I dont have any orders, I dont need array or [orders] = 0 ...
}
I have table named "orders" with id, customer_id and date field.
I found this questions SQL query for Calculating Total No. of Orders per Day? but its not helping me, or I dont understand it very well, btw I am beginner. Thanks!
p.s. what I managed to do, is to select all orders for last 30 days.
$this->db->query("SELECT * FROM orders WHERE customer_id=" . $customer['id'] . " AND date > ADDDATE(CURDATE(), INTERVAL -30 DAY)")->result_array();
Use MySQL EXTRACT function to fetch day from your date field and then group by results according to this. I haven't try it but the following query should work:
SELECT COUNT(*) AS orders, date_field
FROM your_table
WHERE customer_id=$my_cusotmer
GROUP BY EXTRACT(DAY FROM date_field)

Categories