Group sequential numbers into min and max group pairs - algorithm

I have a table with a list of numbers. Each number belongs to an entity.
Entity Number
1 1
1 2
1 3
1 4
...
1 20
2 21
2 22
2 23
1 24
2 25
2 26
2 30
2 31
2 32
2 33
The goal is to list the numbers, grouped by the entities as ranges (min-max pairs).
I need to find a way to group the above table as:
Entity Min Max
1 1 20
2 21 23
1 24 24
2 25 26
2 30 33
I've succesfully done this in my education, but I always found it hard and can't remember how the algorithm was done

This looks similar to SQL Data Range Min Max category
and TSQL Select Min & Max row when grouping

Related

Creating subset of a data

I have a column called Project_Id which lists the names of many different projects, say Project A, Project B and so on. A second column lists the sales for each project.
A third column shows time series information. For example:
Project_ID Sales Time Series Information
A 10 1
A 25 2
A 31 3
A 59 4
B 22 1
B 38 2
B 76 3
C 82 1
C 23 2
C 83 3
C 12 4
C 90 5
D 14 1
D 62 2
From this dataset, I need to choose (and thus create a new data set) only those projects which have at least 4 time series points, say, to get the new dataset (How do I get this by using an R code, is my question):
Project_ID Sales Time Series Information
A 10 1
A 25 2
A 31 3
A 59 4
C 82 1
C 23 2
C 83 3
C 12 4
C 90 5
Could someone please help?
Thanks a lot!
I tried to do some filtering with R but had little success.

How can I get the max value of the sum value in a N x M matrix, each column and each rows can be chosen only once

Example input:
3 4
10 20 30
40 10 30
20 10 0
5 15 5
Example output:
1 3
2 1
4 2
85
30 + 40 + 15 = 85
I need get the result of a matrix with thousand rows and columns

Scilab sort by second column

I have some data:
P = [3 10 25 32 43 1 3
6 12 35 39 49 4 9
2 9 23 36 47 2 9
...
7 20 35 42 44 3 7
15 18 19 41 42 4 6
10 18 32 35 46 3 10];
Data is always between 1 and 50.
I am selecting left 5 columns and 2 right columns:
L=P(:,1:5);
R=P(:,6:7);
I am counting occurrences:
a=tabul(L);
b=tabul(R);
In this moment, in a I am getting:
50. 3.
49. 4.
48. 3.
which tells me, that value 50 occurs 3 times, 49 occurs 4 times and so on.
What I need now is sort matrix a by second column but the first column should be arranged with the second column values. So it would look like this:
49. 4.
50. 3.
48. 3.
How can I sort matrix a this way (later I will sort b the same way)?
I was trying something like:
[a,idx]=gsort(a(:,2),"g","d");
a=a(idx,:);
but this not does what I need.
It does not work because you are overwriting a in the gsort call although you just need the index here. The following does what you want:
[dummy,idx]=gsort(a(:,2),"g","d");
a=a(idx,:);

How to find max level in each path in a Oracle hierarchical SQL?

I would like to know the way to find the max level number in Oracle hierarchical SQL within the given path.
For example : If connect by clause starts with root 1 having below relation .
parent_id node_id votes
NULL 1 -
1 2 10
2 3 12
3 4 11
1 20 5
20 30 20
20 40 4
40 50 22
Here first 3 records belongs to one path with max levle 3.
Next 2 records belong to another path with max level 2.
Last two record belongs to another path with max level 3.
I need output with these max level within the given distinct path and minimum votes:
parent_id node_id LEVEL MAX_LEVL MIN_VOTE
1 2 1 3 10
2 3 2 3 10
3 4 3 3 10
1 20 1 2 5
20 30 2 2 5
1 20 1 3 4
20 40 2 3 4
40 50 3 3 4
1
|
--------------
| |
2 20
| |
3 --------------
| | |
4 30 40
|
50
Thanks,
Guru

count records by specifying many fields and many count clauses on sql oracle

I have this query:
SELECT * FROM PATIENT_MED_SPEC
which gives this result:
NUM_MALADE| NUM_MED| SPECIALITE
----------+--------+------------
3 53 Traumatologue
3 85 Anesthésiste
3 126 Radiologue
6 34 Pneumologue
6 85 Anesthésiste
6 114 Traumatologue
6 135 Anesthésiste
13 4 Orthopédiste
13 8 Cardiologue
13 114 Traumatologue
21 19 Traumatologue
21 64 Radiologue
21 135 Anesthésiste
23 4 Orthopédiste
23 8 Cardiologue
23 88 Cardiologue
And I want to get the result below using count and group by clauses
NUM_MALADE| count_MED| count_SPEc
----------+----------+------------
3 3 3
6 4 3
13 3 3
21 3 3
23 3 2
i.e for every MALADE (patient) we count the MEDECIN's
(doctors) number and the number of SPECIALITES concerned
You could use COUNT DISTINCT:
SELECT MALADE, COUNT(*) AS count_MED, COUNT(DISTINCT SPECIALITE) AS num_SPEC
FROM PATIENT_MED_SPEC
GROUP BY MALADE

Resources