forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportNumberTypes.sql
More file actions
executable file
·72 lines (54 loc) · 1.78 KB
/
portNumberTypes.sql
File metadata and controls
executable file
·72 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* portNumberTypes -- MySQL
Given a table "portgroups" as following:
CREATE TABLE portgroups (
port_number integer not null,
group_type integer not null,
value float not null,
time timestamp not null
);
create a SQL query that returns all port numbers (port_number) with the
number of different group types (group_type) registered on each port,
ordered by port_number.
For example, given the following table:
port_number | group_type | value | time
-------------+------------+--------+---------------------
4 | 3 | 9.31 | 2017-09-14 11:32:00
4 | 5 | -24.42 | 2017-09-14 11:17:03
4 | 3 | 834.5 | 2017-09-14 10:34:37
7 | 2 | -31.27 | 2017-09-14 14:45:06
4 | 8 | 8.64 | 2017-09-14 18:31:31
your query should return the following result:
port_number | types
-------------+-------
4 | 3
7 | 1
*/
/* Create database
mysql -u root -p
CREATE DATABASE portypes;
use portypes;
*/
/* Create example table and populate it
CREATE TABLE portgroups (
port_number integer not null,
group_type integer not null,
value float not null,
time timestamp not null
);
INSERT INTO portgroups (port_number, group_type, value, time) VALUES
(4, 3, 9.31, '2017-09-14 11:32:00'),
(4, 5, -24.42, '2017-09-14 11:17:03'),
(4, 3, 834.5, '2017-09-14 10:34:37'),
(7, 2, -31.27, '2017-09-14 14:45:06'),
(4, 8, 8.64, '2017-09-14 18:31:31');
*/
SELECT port_number, COUNT(DISTINCT group_type) AS types
FROM portgroups
GROUP BY port_number
ORDER BY port_number;
/*
port_number | types
-------------+-------
4 | 3
7 | 1
*/