0
How does this Mysql code look in SQL (in Dbeaver with Azure connection)?
I just need to change SELECT to work in SQL
CREATION OF TABLES
-- VACANCY TABLE
create table vagas (
idSensor int primary key,
idVagas varchar(5),
fkSetor int,
foreign key(fkSetor) references setor(idSetor),
descVagas varchar(20)
)auto_increment = 10;
-- MOVEMENT TABLE
create table movimento (
idMovimento int primary key auto_increment,
movimentacao int, -- Alterei aqui pra por como int pq binary não tava rodando, não sei porquê
dataHora datetime,
estadia varchar(20),
fkSensor int, -- foreign key
foreign key (fkSensor) references vagas (idSensor)
);
-- INSERTING DATA INTO THE TABLE
-- VACANCY TABLE
insert into vagas values
(10,'A1',1, 'Coberta'),
(11,'A2',1, 'Descobertas'),
(12,'A3',1, 'PcD'),
(13,'A4',1, 'Idosos'),
(14,'B1',2, 'Coberta'),
(15,'B2',2, 'PcD'),
(16,'B3',2, 'Descobertas'),
(17,'B4',2, 'Coberta'),
(18,'G1',3, 'Descobertas'),
(19,'G2',3, 'Idosos'),
(20,'G3',3, 'PcD'),
(21,'G4',3, 'Coberta'),
(22,'F1',4, 'Idosos'),
(23,'F2',4, 'Coberta'),
(24,'F3',4, 'PcD'),
(25,'F4',4, 'Coberta'),
(26,'A5', 1, 'Idosos'),
(27,'E1', 4, 'Idosos'),
(28,'E2', 4, 'Idosos');
-- TABLE MOTION
insert into movimento values
(null,1,'2020-05-02 15:03:00','15 min',24),
(null,0,'2020-05-02 15:18:00','15 min',24),
(null,1,'2020-06-17 13:00:00','1 Hr+',11),
(null,0,'2020-06-17 14:00:00','1 Hr+',11),
(null,1,'2020-06-17 14:10:00','45 min',11),
(null,0,'2020-06-17 14:55:00','45 min',11),
(null,1,'2020-06-17 15:43:00','30 min',15),
(null,0,'2020-06-17 16:13:00','30 min',15),
(null,0,'2020-06-17 12:14:00','1 Hr+',13),
(null,1,'2020-06-17 19:43:00','1 Hr+',13),
(null,1,'2020-05-02 07:03:00','15 min',12),
(null,0,'2020-05-02 17:18:00','15 min',12),
(null,1,'2020-05-02 02:03:00','15 min',13),
(null,0,'2020-05-02 02:18:00','15 min',13),
(null,1,'2020-05-02 19:03:00','15 min',12),
(null,0,'2020-05-02 19:18:00','15 min',12),
(null,1,'2020-05-02 19:03:00','15 min',14),
(null,0,'2020-05-02 19:18:00','15 min',14);
-- THIS SELECT HERE Q DOES NOT RUN IN SQL
SET @total = (SELECT COUNT(estadia) FROM movimento where movimentacao = 1);
SET @totalGeral = (SELECT SUM(DISTINCT(@total)) from movimento where movimentacao = 1);
SELECT -- Total aqui tem 10
CONCAT(TRUNCATE(((SUM(IF(TIME(dataHora) between '06:00:00' and '11:59:59', 1,0 ) and movimentacao = 1)/ @totalGeral) * 100),0),'%') as 'Manhã',
CONCAT(TRUNCATE(((SUM(IF(TIME(dataHora) between '12:00:00' and '18:59:59', 1,0 )and movimentacao = 1)/ @totalGeral) * 100),0),'%') as 'Tarde',
CONCAT(TRUNCATE(((SUM(IF(TIME(dataHora) between '19:00:00' and '23:59:59', 1,0 )and movimentacao = 1)/ @totalGeral) * 100),0),'%') as 'Noite',
CONCAT(TRUNCATE(((SUM(IF(TIME(dataHora) between '00:00:00' and '05:59:59', 1,0 )and movimentacao = 1)/ @totalGeral) * 100),0),'%') as 'Madrugada'
FROM movimento;
But what does your code do? It’s nice to at least give a summary so the staff can give you suggestions.
– Sorack
So, it is a parking system This code counts how many entries they had in X period and sorts them by morning, afternoon, night or dawn Only that this value is shown in percentage
– BaaDe
Can put the structure of tables (
CREATE TABLE
) and some data so we can test differentqueries
and suggest solutions?– Sorack
I deleted from here and edited right in the question! I think it gets more organized! I’m sorry the question initially got very vague, it’s my first time here and I didn’t know exactly how I should ask and be clear
– BaaDe