Merge data on multiple rows in one - php

I have a table(MySQL) which stores the utilization of users. Now, what I want to do is, get the total utilization per day for each user. I can get data for each user, however, I am finding it really difficult to merge data from multiple rows for a single day.
Right now, the data I get is as :
id date download upload
1 2015-10-28 08:05:10 1 5
2 2015-10-28 10:25:15 2 5
3 2015-10-28 11:25:10 3 4
4 2015-10-29 11:25:10 8 5
5 2015-10-29 11:25:10 2 7
6 2015-10-29 11:25:10 1 3
7 2015-10-30 11:25:10 11 10
8 2015-10-30 11:25:10 4 5
9 2015-10-30 11:25:10 5 1
10 2015-10-30 11:25:10 10 1
But what I want it to appear like is :
id date download upload
1 2015-10-28 6 14
4 2015-10-29 11 15
7 2015-10-30 30 17

You can use the following query:
SELECT MIN(id) AS id, DATE(`date`) AS 'date',
SUM(download) AS download, SUM(upload) AS upload
FROM mytable
GROUP BY DATE(`date`)
The query uses DATE function in order to extract the date value from the date field.
Demo here

You can try this -
select id, date(`date`) `date`, sum(download) `download`, sum(upload) `upload`
from your_table
group by date(`date`)
order by `date`
It will sum all the downloads & uploads grouping by the date.

you need to a simple sql query lik this:
select max(user_id),op_date,sum(download_count) from test group by op_date;
data:
1 1 09-SEP-16 2
2 1 09-SEP-16 1
5 1 09-SEP-16 3
4 1 10-SEP-16 2
3 1 10-SEP-16 4
filtered data:
1 09-SEP-16 6
1 10-SEP-16 6

Use group by command
select id,`date`,sum(download) as 'download',sum(upload) as 'upload'
from ur_table
group by date(`date`)

Related

Why does not work whereBetween in laravel

I have a model named Student and this model contains some field
id
teacher_id
roll_number
1
1
1
2
1
2
3
1
3
4
1
4
5
1
5
6
1
6
7
1
7
8
1
8
9
1
9
10
1
10
11
2
11
12
2
12
13
2
13
I have a form that passes values teacher_id, start_roll and end_roll
I have a query where I use whereBetween method to get student information
$studentData = Student::where('teacher_id',$request->teacher_id)->whereBetween('roll_number',[$request->start_roll,$request->end_roll])->get();
where start_roll value is 1 and end_roll value is 10 and teacher_id value is 1 but it is strange it only return two array values where id 1 and 10 only
Again if I search start_roll value is 5 and end_roll value is 10 and teacher_id value is 1 but it also strange it returns an empty array
If I print the above query when search it prints like below
Student::where('teacher_id',1)->whereBetween('roll_number',[5,10])->get();
If I execute the above query hardcoded(entry roll_number manually from 5 to 10) then it works fine for me and it returns 6 number of the array where id start 5 and end 10
I don't know why my search is not working. Any suggestion is appreciate please
You should add all requested values 1,2,3,4,5,6,7,8,9,10.
Like This:
$queryRolls = range($request->start_roll,$request->end_roll);
$studentData = Student::where('teacher_id',$request->teacher_id)->whereIn('roll_number',$queryRolls)->get();

MYSQL query that contains 2 things I need in 1 query

I have built a live scoring system for a game. When the scores are entered into the MySQL database, they are entered like this...
ID userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3
1 1 1 1 10 5 2
2 2 1 1 10 5 3
3 3 1 1 12 6 3
4 1 1 2 10 6 4
5 2 1 2 12 5 3
6 3 1 2 9 4 3
As you can see from the above, there is 1 session, 2 different sprints (iterations) and 3 different users. I want to display a leader board. The leader board would show (by iteration), user name (from another table), sprint, point value 1, point value 2, point value 3, sum of point value 1 cumulative for all iterations in that session. So on iteration 1, point value 1 and sum of point value 1 cumulative would be the same. But, in iteration 2, I should see a sum of point value 1 of 20 for user 1, 22 for user 2, and 21 for user 3, in the proper order (descending). I tried a subquery using a select sum, but couldn't quite get it right, and I tried a SUM(IF()), which seemed like it would be right, but it's not. I guess I need some guidance on which direction to go.
I think this query will get you the raw data you want. You will probably want to adjust the query with some form of ordering on total points, and possibly WHERE and LIMIT clauses to meet your exact needs.
SELECT s1.userid,
s1.sessionid,
s1.sprintid,
s1.pointvalue1,
s1.pointvalue2,
s1.pointvalue3,
SUM(s2.pointvalue1) AS sum_pointvalue1,
SUM(s2.pointvalue2) AS sum_pointvalue2,
SUM(s2.pointvalue3) AS sum_pointvalue3
FROM scores s1
JOIN (SELECT userid,
sessionid,
sprintid,
SUM(pointvalue1) AS pointvalue1,
SUM(pointvalue2) AS pointvalue2,
SUM(pointvalue3) AS pointvalue3
FROM scores
GROUP BY userid, sessionid, sprintid
) s2
ON s2.userid = s1.userid AND s2.sessionid = s1.sessionid AND s2.sprintid <= s1.sprintid
GROUP BY userid, sessionid, sprintid
ORDER BY sprintid
Output:
userid sessionid sprintid pointvalue1 pointvalue2 pointvalue3 sum_pointvalue1 sum_pointvalue2 sum_pointvalue3
2 1 1 10 5 3 10 5 3
3 1 1 12 6 3 12 6 3
1 1 1 10 5 2 10 5 2
2 1 2 12 5 3 22 10 6
3 1 2 9 4 3 21 10 6
1 1 2 10 6 4 20 11 6
SQLFiddle Demo

array_push and array_search array_splice on Laravel query builder

Are there any other eloquent query method to deal with the gross process on laravel query builder?
product table
id A_product B_product
1 4,5,8,9 3,7,10,15
2 4,7,10,11 2,3,5,13
which is better does A_product column use array or json?
----search--------------------------------------
if I want to confirm if is there 3 in A_product column on id 1
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
if(in_array("3"){
$A_result='1';
}else{
$A_result='0';
};
------insert----------------------------
if I want to add new num 20 to the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
array_push($querys,"20");
$quearorder=array_value($querys);
DB::table('product')
->where('id', 1)
->update(['A_product' => $queryorder]);
--delete--------------------------------------
if I want to delete num 3 from the A_product column
I use:
$querys=DB::table('product')
->where('id','=','1')
->value('A_product');
$key=array_search(3,$querys);
array_splice($querys,$key,1);
DB::table('product')
->where('id', 1)
->update(['A_product' => $querys]);
Its suggestion that you should store all product into separate column , with product type and the group or for user_id
id product product_type category(or for user)
1 4 A 1
2 5 A 1
3 8 A 1
4 9 A 1
5 4 A 2
6 7 A 2
7 10 A 2
8 11 A 2
1 3 B 1
2 7 B 1
3 10 B 1
4 15 B 1
5 2 B 2
6 3 B 2
7 5 B 2
8 13 B 2
Then you can easily apply CRUD operation

mysql - select count multiple tables, group by week, custom date range

For example, I have 2 tables and a date range (1 dec 2015 - 10 jan 2016).
First table: USERS
id (int) date (datetime)
1 3-dec-2015
2 4-dec-2015
3 19-dec-2015
4 20-dec-2015
5 21-dec-2015
6 29-dec-2015
7 30-dec-2015
Second table: BIRTHDAYS
id (int) date (datetime)
1 6-dec-2015
2 8-dec-2015
3 9-dec-2015
4 17-dec-2015
5 28-dec-2015
The result after the query should be the following:
[0] 1st week => 2 users, 1 birthday
[1] 2nd week => 0 users, 2 birthday
[2] 3ed week => 1 users, 1 birthday
[3] 4th week => 1 users, 0 birthday
[4] 5th week => 2 users, 1 birthday
[5] 6th week => 0 users, 0 birthday
Any ideas how to achive this result or something close? I can use and PHP if needed.
I would start off with something like this:
select ((week(dateb) - week('2015-12-01')) + 1) as week_number, count(a.dateb) as userdates
from users as a
where dateb between '2015-12-01' and '2016-01-01'
group by week(dateb)
order by week(dateb);
and
select ((week(dateb2) - week('2015-12-01')) + 1) as week_number, count(dateb2) as birthdays
from birthdays
where dateb2 between '2015-12-01' and '2016-01-01'
group by week(dateb2)
order by week(dateb2);
Demo, http://sqlfiddle.com/#!9/c83cb/21
from there you can fiddle with the outputting with PHP.
Also note with this approach only rows with populated data are returned. So you should check on the iteration that each row is incremented by 1.
e.g. so for users when you got from week 1 to week 3 you should output week 2 = 0; or however you want to display it.

MySql - count from beginning until each day

Let's say I have the following table (keep in mind that this table will have 10000+ rows):
id total date
1 5 2015-05-16
2 8 2015-05-17
3 4 2015-05-18
4 9 2015-05-19
5 3 2015-05-20
I want the query to give the following result:
1
date => 2015-05-16
total => 5
2
date => 2015-05-17
total => 13
3
date => 2015-05-18
total => 17
4
date => 2015-05-19
total => 26
5
date => 2015-05-20
total -> 29
I can't think of any query that would do this right now, that's why I am not providing any code that I have tried.
Any thoughts? I am not sure if this is possible only with mysql, maybe I have to use and php.
This could be done using user defined variable in mysql and then get the running total as
select
id,
total,
date
date from
(
select
id,
#tot:= #tot+total as total,
date from my_table,(select #tot:=0)x
order by date
)x
You can do this -
SELECT
a.id,
a.date,
(SELECT SUM(b.total) FROM your_table WHERE b.date <= a.date) as new_total
FROM your_table a, your_table b
ORDER BY a.date ASC
This should do it:
select id, (select sum(total) from table a where a.date <= b.date) from table b

Categories