Создать таблицу со сгенерированными столбцами - PullRequest
0 голосов
/ 17 апреля 2019

Мы пытаемся создать таблицу со сгенерированными столбцами.

Пожалуйста, посмотрите эти запросы для нашей текущей структуры таблицы:

Таблица Heepsy:

CREATE TABLE `Heepsy` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `photo_url` varchar(300) COLLATE utf8mb4_bin DEFAULT NULL,
  `followers` int(10) DEFAULT NULL,
  `engagement` decimal(5,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51731 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

Таблица HypeAuditor:

CREATE TABLE `HypeAuditor` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `engaement` decimal(5,2) DEFAULT NULL,
  `Country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Country_percentage` int(3) DEFAULT NULL,
  `Interest1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest1_percentage` int(3) DEFAULT NULL,
  `Interest2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest2_percentage` int(3) DEFAULT NULL,
  `Interest3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `Interest3_percentage` int(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=27123 DEFAULT CHARSET=utf8;

Таблица NinjaOutreach:

CREATE TABLE `NinjaOutreach` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `fullname` varchar(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `photo_url` varchar(300) CHARACTER SET utf8 DEFAULT NULL,
  `followers` int(10) DEFAULT NULL,
  `engagement` decimal(5,2) DEFAULT NULL,
  `city` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `state` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `country` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `category_1` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_2` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_3` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_4` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_5` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  `category_6` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=296788 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

Таблица Kombiniert:

CREATE TABLE `Kombiniert` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `fullname` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `followers_heepsy` int(11) DEFAULT NULL,
  `followers_ninjaoutreach` int(11) DEFAULT NULL,
  `followers_average` int(11) DEFAULT NULL,
  `engagement_heepsy` decimal(5,4) DEFAULT NULL,
  `engagement_ninjaoutreach` decimal(5,4) DEFAULT NULL,
  `engagement_hypeauditor` decimal(5,4) DEFAULT NULL,
  `engagement_average` decimal(5,4) DEFAULT NULL,
  `city` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `state` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category4` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category5` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `category6` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest1` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest1_share` decimal(5,4) DEFAULT NULL,
  `follower_interest2` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest2_share` decimal(5,4) DEFAULT NULL,
  `follower_interest3` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_interest3_share` decimal(5,4) DEFAULT NULL,
  `follower_country` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `follower_country_share` decimal(5,4) DEFAULT NULL,
  `follower_country_total` int(11) DEFAULT NULL,
  `follower_country_engaged` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. Как нам нужно обновить SQL-запрос для создания таблицы Kombiniert со следующими сгенерированными столбцами:

follow_average: Среднееиз столбцов «follow_heepsy» и «follow_ninjaoutreach», но только если оба значения больше 0. Если только один столбец больше 0, используйте значение этого столбца.

engagement_average: Среднее изстолбцы "engagement_heepsy" и "engagement_ninjaoutreach" и "engagement_hypeauditor", но только если все они больше 0. Если только два столбца больше 0, используйте среднее из этих 2 столбцов.Если только один столбец больше 0, используйте значение этого столбца.

follower_country_total: follow_average * follower_country_share

follower_country_engaged: follow_country_total * engagement_average

Все сгенерированные столбцы должны храниться, а не виртуальные?

Как нам нужно создать запрос INSERT, который будет выполнять следующее:

Для каждого username ОТ HypeAuditor создайте строку со следующими данными в таблице Kombiniert:

https://docs.google.com/spreadsheets/d/1qU0WaExjg8cCsA3cdDvKf3UqdLxqXKw_8QnDRLvJwMA/edit?usp=sharing?

Ответы [ 3 ]

0 голосов
/ 17 апреля 2019

Я немного поработал с ответом от @ianic и пришел к такому результату:

INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when hy.followers=0 and nj.followers>0 then nj.followers
                when hy.followers>0 and nj.followers=0 then hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when hy.engagement/100=0 and nj.engagement/100=0 and h.engagement/100>0 then h.engagement/100
                        when hy.engagement/100=0 and nj.engagement/100>0 and h.engagement/100=0 then nj.engagement/100
                        when hy.engagement/100>0 and nj.engagement/100=0 and h.engagement/100=0 then hy.engagement/100
                        when hy.engagement/100=0 and nj.engagement/100>0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0 and nj.engagement/100>0 and h.engagement/100=0 then (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0 and nj.engagement/100=0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb

Пока все работает нормально, за исключением одного: есть имена пользователей в HypeAuditor существует тольков одной из таблиц Heepsy ИЛИ NinjaOutreach.Если это так, то в столбцы этой конкретной таблицы вставлено NULL, что пока хорошо, но тогда два случая для вычисления среднего значения не работают.

Как я могу изменитьслучаи для работы в этом случае также?

0 голосов
/ 17 апреля 2019

Только что нашел окончательный ответ с помощью @ianic потрясающего запроса!Большое вам спасибо!

Вот оно:

INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when hy.followers IS NULL AND nj.followers>0 THEN nj.followers
                when hy.followers>0 AND nj.followers IS NULL THEN hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when hy.engagement IS NULL AND nj.engagement IS NULL AND h.engagement>0 THEN h.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement IS NULL THEN nj.engagement/100
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement IS NULL THEN hy.engagement/100
                        when hy.engagement IS NULL AND nj.engagement>0 AND h.engagement>0 THEN (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement>0 AND h.engagement IS NULL THEN (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement>0 AND nj.engagement IS NULL AND h.engagement>0 THEN (h.engagement/100+hy.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb
0 голосов
/ 17 апреля 2019

ОК, я предполагаю объединение имени пользователя и отдельной записи для каждого имени пользователя в каждой таблице.

    INSERT INTO Kombiniert
(username, fullname, followers_heepsy, followers_ninjaoutreach, followers_average, engagement_heepsy, engagement_ninjaoutreach, engagement_hypeauditor, engagement_average, city, state, country, category1, category2, category3, category4, category5, category6, follower_interest1, follower_interest1_share, follower_interest2, follower_interest2_share, follower_interest3, follower_interest3_share, follower_country, follower_country_share, follower_country_total, follower_country_engaged)
    Select bb.*, follower_country_total*engagement_average as follower_country_engaged from
        (
            Select aa.*, followers_average*follower_country_share as follower_country_total from 
            (
                select 
                h.username,
                nj.fullname,
                hy.followers as followers_heepsy,
                nj.followers as followers_ninjaoutreach,
                case when (hy.followers=0 or hy.followers is null) and nj.followers>0 then nj.followers
                when hy.followers>0 and (nj.followers=0 or nj.followers is null) then hy.followers
                else (hy.followers+nj.followers)/2 end as followers_average,
                hy.engagement/100 as engagement_heepsy,
                nj.engagement/100 as engagement_ninjaoutreach,
                h.engagement/100 as engagement_hypeauditor,
                case    when (hy.engagement/100=0 or hy.engagement is null) and (nj.engagement/100=0 or nj.engagement is null) and h.engagement/100>0 then h.engagement/100
                        when (hy.engagement/100=0 or hy.engagement is null) and nj.engagement/100>0 and (h.engagement/100=0 or h.engagement is null) then nj.engagement/100
                        when hy.engagement/100>0  and (nj.engagement/100=0  or nj.engagement is null) and (h.engagement/100=0 or h.engagement is null) then hy.engagement/100
                        when (hy.engagement/100=0 or hy.engagement is null) and nj.engagement/100>0 and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0  and nj.engagement/100>0 and (h.engagement/100=0 or h.engagement is null) then (hy.engagement/100+nj.engagement/100)/2
                        when hy.engagement/100>0  and (nj.engagement/100=0 or nj.engagement is null) and h.engagement/100>0 then (h.engagement/100+nj.engagement/100)/2
                        else (hy.engagement/100+nj.engagement/100+h.engagement/100)/3 end as engagement_average,
                nj.city,
                nj.state,
                nj.country ,
                nj.category_1,
                nj.category_2,
                nj.category_3,
                nj.category_4,
                nj.category_5,
                nj.category_6,
                h.interest1,
                h.Interest1_percentage/100 as follower_interest_share1,
                h.interest2,
                h.Interest2_percentage/100 as follower_interest_share2,
                h.interest3,
                h.Interest3_percentage/100 as follower_interest_share3,
                h.country as follower_country,
                h.country_percentage/100 as follower_country_share


                from HypeAuditor h
                left join Heepsy hy on h.username = hy.username
                left join NinjaOutreach nj on nj.username=h.username
                ) aa 
            ) bb
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...