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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`date` date not null,
`probe` smallint unsigned not null,
`temperature` decimal(5,2) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`date`,`probe`,`temperature`) values
('2018-01-24', 1, 21.5),
('2018-01-24', 2, 22.5),
('2018-01-24', 3, 23.5),
('2018-01-25', 1, 18.5),
('2018-01-25', 2, 19.5),
('2018-01-25', 3, 20.5),
('2018-01-26', 1, 15.5),
('2018-01-26', 3, 15.5),
('2018-01-27', 1, 17.5),
('2018-01-27', 2, 18.5)
--------------
--------------
select * from `test`
--------------
+----+------------+-------+-------------+
| id | date | probe | temperature |
+----+------------+-------+-------------+
| 1 | 2018-01-24 | 1 | 21.50 |
| 2 | 2018-01-24 | 2 | 22.50 |
| 3 | 2018-01-24 | 3 | 23.50 |
| 4 | 2018-01-25 | 1 | 18.50 |
| 5 | 2018-01-25 | 2 | 19.50 |
| 6 | 2018-01-25 | 3 | 20.50 |
| 7 | 2018-01-26 | 1 | 15.50 |
| 8 | 2018-01-26 | 3 | 15.50 |
| 9 | 2018-01-27 | 1 | 17.50 |
| 10 | 2018-01-27 | 2 | 18.50 |
+----+------------+-------+-------------+
--------------
select t1.date,
t1.probe as Probe1,
t2.probe as Probe2,
t3.probe as Probe3
from test as t1
left outer join test as t2
on t2.date = t1.date
and t2.probe > t1.probe
left outer join test as t3
on t3.date = t1.date
and t3.probe > t2.probe
order by t1.date
--------------
+------------+--------+--------+--------+
| date | Probe1 | Probe2 | Probe3 |
+------------+--------+--------+--------+
| 2018-01-24 | 3 | NULL | NULL |
| 2018-01-24 | 1 | 2 | 3 |
| 2018-01-24 | 1 | 3 | NULL |
| 2018-01-24 | 2 | 3 | NULL |
| 2018-01-25 | 3 | NULL | NULL |
| 2018-01-25 | 1 | 2 | 3 |
| 2018-01-25 | 1 | 3 | NULL |
| 2018-01-25 | 2 | 3 | NULL |
| 2018-01-26 | 1 | 3 | NULL |
| 2018-01-26 | 3 | NULL | NULL |
| 2018-01-27 | 1 | 2 | NULL |
| 2018-01-27 | 2 | NULL | NULL |
+------------+--------+--------+--------+
--------------
select distinct t1.date,
t2.temperature as 'Probe 1',
t3.temperature as 'Probe 2',
t4.temperature as 'Probe 3'
from test as t1
left outer join test as t2
on t2.date = t1.date
and t2.probe = 1
left outer join test as t3
on t3.date = t1.date
and t3.probe = 2
left outer join test as t4
on t4.date = t1.date
and t4.probe = 3
order by t1.date
--------------
+------------+---------+---------+---------+
| date | Probe 1 | Probe 2 | Probe 3 |
+------------+---------+---------+---------+
| 2018-01-24 | 21.50 | 22.50 | 23.50 |
| 2018-01-25 | 18.50 | 19.50 | 20.50 |
| 2018-01-26 | 15.50 | NULL | 15.50 |
| 2018-01-27 | 17.50 | 18.50 | NULL |
+------------+---------+---------+---------+
--------------
select date,
group_concat(temperature order by probe separator " - ") as temperature
from test
group by date
order by date
--------------
+------------+-----------------------+
| date | temperature |
+------------+-----------------------+
| 2018-01-24 | 21.50 - 22.50 - 23.50 |
| 2018-01-25 | 18.50 - 19.50 - 20.50 |
| 2018-01-26 | 15.50 - 15.50 |
| 2018-01-27 | 17.50 - 18.50 |
+------------+-----------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager