Matlab using accumarray with cell array - mysql

I am quite new to Matlab, but I have some experience with other programming languages. I have a very large table imported from MySQL in Matlab. It is given as cell array that looks something like this:
date key sales weight name
12/11 101 1200 20 blue
12/11 178 1200 70 purple
13/11 209 1300 15 purple
12/11 101 1200 10 blue
14/11 678 1200 10 yellow
12/11 340 1500 30 green
17/11 178 1900 50 purple
And I want the output to be this:
key sales weight name
101 2400 30 blue
178 3100 120 purple
209 1300 15 purple
678 1200 10 yellow
340 1500 30 green
So I would like to combine the rows which have the same number in the column 'key'. Meanwhile I would like to sum the column 'sales' and 'weight' and keep the column 'name' (each 'key' has the same 'name', but each 'name' can have multiple 'keys')
I know this is possible with a for loop, but as I am having to manipulate a lot of tables in similar but different ways this is computational intensive.
I have read in similar problems this can be solved using accumarray, but can this be done with accumarray with a sub as cell array? And how would that look like?

Here is one method using accumarray, however it might be worth your while to consider the new table data structure instead of a cell matrix (I'm sure you can easily convert to it)
T = { 101 1200 20 'blue'
178 1200 70 'purple'
209 1300 15 'purple'
101 1200 10 'blue'
678 1200 10 'yellow'
340 1500 30 'green'
178 1900 50 'purple'};
[u, ind, x] = unique([T{:,1}])
key = T(ind,1)
sales = accumarray(x', [T{:,2}])
weight = accumarray(x', [T{:,3}])
name = T(ind,4)
[key, num2cell(sales), num2cell(weight), name]

x={ '12/11' 101 1200 20 'blue'
'12/11' 178 1200 70 'purple'
'13/11' 209 1300 15 'purple'
'12/11' 101 1200 10 'blue'
'14/11' 678 1200 10 'yellow'
'12/11' 340 1500 30 'green'
'17/11' 178 1900 50 'purple'};
[~,b,c]=unique([x{:,2}]);
y=[x(b,2),...
num2cell(sparse(1,c,[x{:,3}]))',...
num2cell(sparse(1,c,[x{:,4}]))',...
x(b,[5])];
unique is used to get the indices of duplicate keys. sparse is used to get the sum.

Related

Finding Total Percentage From 3 Different Mysql Table

table_1
ST_ID NAME MATHS GEOGRAPHY ENGLISH
1001 Alan Wegman 80 85 70
1002 Robert Franko 79 65 60
1003 Francis John 90 75 67
1004 Finn Harry 88 87 93
table_2
ST_ID NAME MATHS GEOGRAPHY ENGLISH
2001 Alan Wegman 69 75 80
2002 Robert Franko 99 85 70
2003 Francis John 80 65 77
2004 Finn Harry 78 97 83
table_3
ST_ID NAME MATHS GEOGRAPHY ENGLISH
3001 Alan Wegman 90 81 72
3002 Robert Franko 97 65 61
3003 Francis John 74 75 67
3004 Finn Harry 77 88 73
From above three tables, i want to to the following, i want to take value of MATHS of student Alan Wegman which is 80 divide by 100 from TABLE 1 then take value of GEOGRAPHY of the same student Alan Wegman which is 85 divide by 100 from TABLE 3 then from last table take value of ENGLISH of same student Alan Wegman which is 70 divide by 100 then they should be added to get one value like this example (80/100+85/100+70/100) and output should be displaying NAME and total value after addition example below
Alan Wegman 2.27
Robert Franko 2.11
Finn Harry 3.29
Is this really possible? i want this to be performed by a single query for all records or if there is an alternative way of doing this please share with me, the query i am trying to achieve this result is this one below but it does not return any thing i don't know where am wrong.
select
table_1.NAME MATHS/100+table_2.NAME GEOGRAPHY/100+table_3.NAME ENGLISH/100
WHERE table_1.NAME = table_2.NAME = table_3.NAME
I am not competent with mysql need help here guys.

MS Access calculation i cant figure out

I need to create a table in MS Access like the following.
It's plain and simple, but I just cant figure it out.
For example
ID debit credit balance
1 50 50
2 20 70
3 30 40
4 15 55
5 30 85
Thanks

How to populate data in one table from another table in MySQL

i have two tables in my toll database in MySQL, namely traffic_shift_wise and traffic_day_wise. And the columns of these tables are as follows:
Table traffic_shift_wise
toll_id date shift car_single car_return car_total
1 2016-09-01 1 25 10 35
1 2016-09-01 2 50 25 75
1 2016-09-02 1 100 50 150
1 2016-09-02 2 200 100 300
traffic_day_wise
toll_id date car_single car_return car_total
1 2016-09-01 75 35 110
1 2016-09-02 300 150 450
There are two shifts in day for a specific toll, here you can see that i have to take data from the traffic_shift_wise to traffic_day_wise, automatically just after insert in traffic_shift_wise.
So, please help me out with this. What should i use, triggers.. ?

How to rewrite "Recursive Query using WITH Clause" in MySQL to fetch all levels of parent records for child record

I am required to rewrite a query in MySQL which is originally written in MS SQL Server using the WITH clause. This is basically to fetch all levels of parent records for child records. Here, I am using the classic EMPLOYEES table of the HR schema in Oracle database as an example.
Originally data in EMPLOYEES table is in this format.
select employee_id, manager_id
from employees
order by 1,2;
---------------------------------------------
EMPLOYEE_ID MANAGER_ID
---------------------- ----------------------
100
101 100
102 100
103 102
104 103
107 103
124 100
141 124
142 124
143 124
144 124
149 100
174 149
176 149
178 149
200 101
201 100
202 201
205 101
206 205
My requirement is to view all level of parent records for child records. I am able to achieve this using the following query in Oracle and MS SQL Server.
WITH Asd(Child,
Parent
)
AS (SELECT Employee_Id,
Manager_Id
FROM Employees
UNION ALL
SELECT E.Employee_Id,
A.Parent
FROM Employees E, Asd A
WHERE E.Manager_Id = A.Child
)
SELECT Child,
Parent
FROM Asd
WHERE Parent IS NOT NULL
ORDER BY Child, Parent;
----------------------------------------------------------
CHILD PARENT
---------------------- -----------------------------------
101 100
102 100
103 100
103 102
104 100
104 102
104 103
107 100
107 102
107 103
124 100
141 100
141 124
142 100
142 124
143 100
143 124
144 100
144 124
149 100
174 100
174 149
176 100
176 149
178 100
178 149
200 100
200 101
201 100
202 100
202 201
205 100
205 101
206 100
206 101
206 205
36 rows selected
As you can see, I am bringing all the parents as well as grand parents under PARENT column in the query.
However, this approach does not work in MySQL as WITH clause is not supported.
Could anyone please help me on how to rewrite this query in MySQl?
As for sept/2012, there is not direct substitute on MySQL for the Common Table Expressions (CTE) of MS SQL Server. You should rely on stored procedures.
Check this answer to another identical question here in SO. It should answer all your doubts. I'm not copying any code as on the link everything is well detailed and explained.

Sorting varchar fields for mysql database

Trying to sort the following TEAM_TOTAL Column Desc
MATCHID TEAM_TOTAL
---------- -----------------
573 Total 112
573 Total 2 for 115
574 Total 9 for 97
574 Total 2 for 100
575 Total 9 for 129
575 Total 9 for 101
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
578 Total 6 for 193
578 Total 119
But the problem is TEAM_TOTAL is varchar, is there a way with query alone I can get the results in the sorted desc order.
More over there is a text as well in that column. I am running out of ideas to get this up
Result should have beeen like the below Result Set
MATCHID TEAM_TOTAL
---------- -----------------
578 Total 6 for 193
576 Total 4 for 191
576 Total 9 for 160
577 Total 8 for 157
577 Total 7 for 137
575 Total 9 for 129
578 Total 119
573 Total 2 for 115
573 Total 112
575 Total 9 for 101
574 Total 2 for 100
574 Total 9 for 97
Give this a try:
select * from t
order by substring(
team_total, locate('for', team_total) +
if(locate('for', team_total) > 0, 4, 7))+0 desc
Fiddle here.
Try to extract the integer (string after the last space):
-- 'Total 112' - extracts 112
SELECT SUBSTRING('Total 112', LENGTH('Total 112') - LOCATE(' ', REVERSE('Total 112')));
-- 'Total 6 for 193' - extracts 193
SELECT SUBSTRING('Total 6 for 193', LENGTH('Total 6 for 193') - LOCATE(' ', REVERSE('Total 6 for 193')));
Now, you can convert that string to a number and then order by it.
SELECT * FROM teams
ORDER BY
CAST(SUBSTRING(TEAM_TOTAL, LENGTH(TEAM_TOTAL) - LOCATE(' ', REVERSE(TEAM_TOTAL))) AS INT) DESC