Вот проблема: Общее количество различных типов работников по регионам, по состоянию на 2019-12-31 и 2020-01-31. Укажите также% роста (всего и по каждому типу работников)
Это то, что я пробовал:
SELECT DISTINCT date,
worker_type,
location_region,
COUNT (worker_num) as worker_count,
CAST(100 * (count(worker_num) - lag(count(worker_num), 1) over (order by date)) / lag(count(worker_num), 1) over (order by date) as STRING) || '%' as growth
FROM myTable
WHERE date IN ('2019-12-31','2020-01-31')
GROUP BY 1,2,3;
Это дает вывод странных значений% (выше 100 и ниже 0). Хотел узнать, что я делаю не так с моими логи c.
Пример данных:
CREATE TABLE `myTable` (
`id` mediumint(8) unsigned NOT NULL auto_increment,
`date` varchar(255),
`worker_type` varchar(255) default NULL,
`location_region` varchar(255) default NULL,
`worker_num` varchar(255),
PRIMARY KEY (`id`)
) AUTO_INCREMENT=1;
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("08-11-19","Type2","EMEA","Y5J 0Q4"),("10-10-20","Type2","AMER","R1Y 9S9"),("02-13-19","Type3","APAC","P2E 1O5"),("10-02-20","Type2","AMER","O3T 9M0"),("01-11-20","Type2","AMER","J7X 6M5"),("08-14-20","Type1","EMEA","J9Q 5M3"),("10-17-20","","LATAM","E9D 6G2"),("03-09-20","Type3","EMEA","D6N 6K3"),("07-10-20","Type2","LATAM","N7W 3G6"),("05-02-19","Type2","APAC","P0I 5S1");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("01-26-20","Type3","APAC","X4J 1Z6"),("06-25-20","Type3","APAC","I3P 9O1"),("08-10-19","","LATAM","I4A 4J1"),("09-18-19","Type2","APAC","R6I 8B4"),("06-01-19","Type3","LATAM","H5M 3X2"),("07-09-20","Type3","AMER","B8C 4T3"),("04-11-19","Type2","APAC","K6J 3V8"),("09-19-20","Type2","APAC","X0P 2B7"),("06-10-20","Type2","AMER","H4Q 3D4"),("11-16-20","Type3","EMEA","R9B 6Z0");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("04-05-19","","APAC","Q8E 4O9"),("01-28-20","Type2","EMEA","Q6Y 5Z3"),("05-10-20","Type2","EMEA","X3I 4N1"),("08-06-20","Type2","AMER","U6D 5L1"),("12-10-20","Type3","APAC","D3P 5X6"),("03-08-20","","APAC","I3G 9T7"),("07-05-20","Type2","APAC","L7Y 2L0"),("09-28-19","","EMEA","A9I 0N8"),("04-18-20","Type3","EMEA","Y0Z 9P9"),("09-06-20","Type1","LATAM","P3E 5U6");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("03-31-20","Type3","EMEA","F1A 9B5"),("02-17-19","Type3","EMEA","L6U 8H1"),("08-24-19","Type3","LATAM","E7F 9J4"),("11-25-19","Type1","APAC","G8N 8L8"),("06-29-19","Type2","APAC","I2V 1E6"),("04-24-20","Type3","EMEA","U8O 6K1"),("11-10-19","Type1","LATAM","H0Q 4A1"),("08-14-20","","EMEA","L4J 0G9"),("05-19-19","Type2","APAC","O4B 1C9"),("09-28-19","Type2","AMER","J6B 9N2");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("12-14-19","Type2","LATAM","L3J 7H1"),("03-23-20","Type3","APAC","K8U 9F0"),("01-22-21","","AMER","S0S 2U2"),("01-13-21","Type1","APAC","G7F 0J7"),("03-12-20","Type2","EMEA","R1I 3S6"),("11-12-19","Type2","EMEA","O9A 6Y2"),("06-29-19","Type1","LATAM","Q0X 4A1"),("03-05-19","","LATAM","H8J 1G7"),("01-04-20","Type3","EMEA","M7Y 4N4"),("02-24-20","Type2","APAC","H0E 6A0");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("03-16-20","","APAC","U2X 3L9"),("08-08-19","Type1","AMER","A7V 1B1"),("02-27-20","Type3","LATAM","X4G 2P0"),("10-10-20","Type3","APAC","W3E 1Q7"),("06-10-19","Type2","LATAM","A4Y 0K7"),("03-11-19","","APAC","Y7C 7Q8"),("02-28-20","Type3","LATAM","H8S 6E2"),("03-23-20","Type2","LATAM","V0J 4I4"),("05-21-20","Type1","APAC","R2L 1P4"),("04-05-19","","LATAM","B1R 1D9");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("10-12-20","Type3","EMEA","L1Y 6L9"),("10-20-20","Type2","EMEA","C7I 3D8"),("01-10-20","Type3","APAC","K3C 0E5"),("12-12-19","","AMER","Z0V 4Z4"),("05-10-20","Type3","AMER","S0M 6M7"),("03-27-20","Type3","LATAM","S8N 3G3"),("12-24-19","Type3","APAC","C6H 4V1"),("04-06-19","","EMEA","E3Z 3O8"),("08-08-19","Type1","APAC","L1F 6A6"),("11-28-19","","EMEA","T2O 6O1");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("05-08-19","Type1","AMER","V1J 1N1"),("09-28-19","Type3","AMER","S4Z 9Q9"),("09-29-19","Type1","LATAM","R9K 2J1"),("08-26-20","","AMER","M3O 1R0"),("02-17-19","Type3","APAC","E2E 5E3"),("04-17-20","Type3","EMEA","G6A 4E3"),("11-21-20","Type1","EMEA","N1X 8A1"),("09-29-20","Type2","APAC","C8R 3A9"),("11-21-19","","LATAM","E7O 8O8"),("11-04-19","Type3","EMEA","P8F 4K2");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("01-28-21","","LATAM","B5A 3E7"),("06-21-20","Type1","APAC","D6A 3V1"),("06-09-20","Type3","AMER","U6F 1Z9"),("04-11-19","Type1","EMEA","H3V 7G6"),("04-11-20","","AMER","D9O 5V7"),("05-31-19","Type3","LATAM","Z3J 6C7"),("10-13-20","","AMER","T0G 7F9"),("05-20-19","","APAC","G1C 2K7"),("04-14-20","Type1","APAC","Z2P 2I8"),("04-19-19","Type3","AMER","O3U 8I1");
INSERT INTO `myTable` (`date`,`worker_type`,`location_region`,`worker_num`) VALUES ("05-06-20","","AMER","H3C 9X5"),("05-16-19","Type3","AMER","N5K 3W1"),("06-25-20","","AMER","W1F 2L2"),("01-18-20","Type1","AMER","T2N 2J4"),("07-19-19","","LATAM","Y2B 4Z5"),("08-21-20","Type1","APAC","B1O 0W5"),("12-18-19","Type2","APAC","M3Y 3Z3"),("09-28-20","Type2","EMEA","G7U 2T7"),("09-26-19","Type1","LATAM","B0V 4Q9"),("08-12-20","Type1","LATAM","W0O 1M4");
Надеюсь, это поможет!