order group_by in codeigniter - mysql

i have a table
id typesignal close open cont
1 4 150 300 2
2 3 20 40 1
3 2 50 75 15
4 1 15.2 30.8 5
5 1 200 225 8
6 6 200 500 8
7 7 10 20 4
8 8 9 12 2
9 9 5 8 3
10 5 40 50 4
11 4 5 8 9
12 5 4 2 3
13 8 5 4 0
i want to display the last row of each kind from the typesignal column.
$this->db->group_by('typesignal');
$query = $this->db->get('signals');
but I need the last row that was create i use
$this->db->group_by('typesignal');
$this->db->order_by("idSignal");
$query = $this->db->get('signals');
but its not working.
The expected output is: 13,12,11,9,8,7,6,5,3,2

i use this query
$this->db->group_by('typeSignal');
$this->db->select_max('idSignal');
$this->db->order_by("idSignal", "DESC");
$query = $this->db->get('signals');
return $query->result_array();
is working the secret is $this->db->select_max('idSignal');
Thanx for the help

Related

What is the problem with the size of the moving window (movfun) in octave?

I'm trying to use movfun (moving window) in octave
x = -1000:0.1:1000
y = sin(x)
movfun(#(arg) printf("%d\n", size(arg)), y(1:100), 4)
I expect to see all 4s
However, surprisingly (to me) I get:
4
1
4
97
2
1
3
1
3
1
ans =
Columns 1 through 29:
4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Columns 30 through 58:
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Columns 59 through 87:
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Columns 88 through 100:
5 5 5 5 5 5 5 5 5 5 5 5 4
What is wrong with my expectations?
This sheds a bit more light (I've increased window size to 8 for more clarity):
movfun( #(x) size(x, 1), y(1:100), 8 )
movfun( #(x) size(x, 2), y(1:100), 8 )
These produce the following outputs respectively:
4 5 6 7 8 8 8 8 [...] 8 8 8 8 7 6 5
1 1 1 1 93 93 93 93 [...] 93 93 93 93 1 1 1
This tells us that, presumably, under the hood, movfun makes sure to pass each 8-element vector in vertical form for processing, and that it treats the edges specially as single inputs, but then grabs all the 'non-edge' elements and performs some form of vectorised calculation involving all 93 columns at the same time.
We can confirm the 'edge' behaviour from the documentation:
"Endpoints"
This property controls how results are calculated at the
boundaries (endpoints) of the window. Possible values are:
"shrink" (default)
The window is truncated at the beginning and end of the array to exclude elements for which there is no source data. For example, with a window of length 3, 'Y(1) = FCN (X(1:2))', and 'Y(end) = FCN (X(end-1:end))'.

Selecting or Updating with 2 group criteria for a max value on mysql [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 3 years ago.
I'm trying to figure out how to use group and max() on joins correctly, I'm doing some parsing of a moodle(Open source school software) mysql database. Students are allowed to retake the quizes indefinatly for this particular program, but I need to be able to update the course completion date to reflect the last time they took the test because a lot of other things depend on the completion fields.
The mdl_quiz_attempts table stores all attempts for all quizes, the userid will have many of the same entries, but the attempt number is not unique to the table, but instead unique to both the student AND the key for the row. Meaning students have multiple entries. On the mdl_course_modules table, The instance field is the key for the mdl_quiz table, and the mdl_course_modules_completion coursemoduleid field is the key for mdl_course_modules.
So what I want to do is this:
given a student id
UPDATE mdl_course_completion.timemodified to mdl_quize_attempts.timemodified
WHERE the row on mdl_quiz_attempts is the max attempt by userid for each quiz.(the quiz field on the quiz_attempts has to be looked up through in course module instance table to get the, instance id for course completion module id)
Here are example partial tables.
mdl_quiz_attempts
id quiz userid attempt timemodified
2 1 3 6 1365408901
6 1 4 1 1369873688
7 2 4 1 1369877532
8 7 4 1 1369881431
9 7 4 2 1369882897
12 5 4 1 1505165504
13 6 4 1 1369887643
17 8 4 1 1369958105
18 1 4 2 1374557701
22 7 4 3 1374639901
23 6 4 7 1374640202
24 5 4 2 1374639901
25 8 4 2 1374639901
26 2 4 2 1374639301
27 2 6 1 1376620469
29 2 12 1 1389915486
30 1 23 1 1390978667
31 1 23 2 1391030924
32 2 23 1 1392113103
33 2 23 2 1392696602
34 2 23 3 1392767435
35 7 12 1 1398914256
36 8 43 1 1405281193
37 1 50 1 1405522411
38 5 43 1 1505165504
mdl_course_modules
id course module instance section
3 2 9 2 3
5 2 17 2 4
7 2 17 3 5
8 2 17 4 6
9 2 17 5 7
10 2 17 6 8
11 2 17 7 9
12 2 17 8 10
13 2 17 9 11
14 2 17 10 12
15 2 17 11 13
25 2 16 1 14
26 2 23 1 4
28 2 7 1 14
30 4 9 4 26
42 4 23 3 33
45 4 23 6 38
46 4 23 7 37
47 4 23 8 36
48 4 23 9 35
49 4 23 10 32
50 4 23 11 34
51 5 9 5 27
53 5 23 12 43
55 5 23 13 44
mdl_quiz
id name
10 Unit 10 Quiz
11 Unit 2 Quiz
12 Unit 3 Quiz
13 Unit 5 Quiz
14 Unit 1 Quiz
15 Unit 8 Quiz
16 Unit 9 Quiz
17 Unit 7 Quiz
18 Unit 4 Quiz
mdl_course_modules_completion
id coursemoduleid userid completionstate viewed timemodified
14 25 2 0 1 0
15 25 6 0 1 0
67 25 4 1 1 1369873688
68 28 4 1 0 1369874483
69 192 4 1 0 1369875233
70 184 4 1 1 1369877532
Something like this ?
update mdl_course_modules_completion c
join mdl_quiz_attempts a on a.userid = c.userid
join (select max(attempt) max_attempts from mdl_quiz_attempts group by userid) max on max.max_attempts = a.attempt
set c.timemodified = a.timemodified
where c.userid = :<USER_ID>

Finding continuous data from a table for months

I have a table which has month data in INT April=4, May=5 and so on. I want those records which have continuous data. My table is as follows. So if a record has discontinuous data, it should not be returned.
Continuous data means those records which having continuous four month. If records are there for 4,5,6,7 or 5,6,7,8 or 6,7,8,9 months then that record should come in result of a ID. If there records for a ID has 4,5,8,9 in month field this is discontinue data for me.
Initial query:
select ID, month from table1 where month in (4,5,6,7,8,9) group by month;
Initial Data:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
9 3 4 200
10 4 5 800
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
15 6 4 100
16 6 5 200
17 6 8 500
18 6 9 600
Result:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
My database is MySQL.
I have months from April(4) to Sept(9)
Try the below query this will work I hope!
SELECT ID, Month
FROM table1 WHERE month in (4,5,6,7,8,9)
GROUP BY ID, Month
HAVING count(ID)>3
kindly let me know this is working or not

sql display in matrix form but column and row values not fixed

tables i am using
product_parameter
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on rows and columns are not fixed
tables i am using
product_parameter
Sorry wanted to correct the output format i needed after query
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on... rows and columns values are not fixed
It would be much easier to create 2D array in application code.
In SQL you have to add a join for each column.

Selecting items from one column based on values in another.

I have the following data:
id1,id2
1 3
1 8
1 10
1 11
2 3
2 10
2 11
3 2
3 18
3 20
4 3
4 8
5 3
5 10
5 11
5 40
5 45
5 50
6 1
6 59
6 70
I won't get all id1 with id2 = 3,10,11.
For example, id1=4 only with id2=3, should not return.
The results should be
id1
1
2
5
SELECT distinct(ID1) FROM TBTEST WHERE ID2 IN(3,10,11)
SQL code
SELECT ID1,COUNT(ID2) FROM TBTEST
WHERE ID2 IN(3,10,11)
GROUP BY ID1
HAVING COUNT(ID2)=3
Is this what you need?