Построение дерева с парами узлов

Использование INNER JOIN:

UPDATE TABLE1
INNER JOIN TABLE2 ON TABLE1.SUBST_ID = TABLE2.SERIAL_ID
SET TABLE2.BRANCH_ID = TABLE1.CREATED_ID;

Другое альтернативное решение, как показано ниже: Здесь я использую предложение WHERE вместо JOIN

UPDATE 
    TABLE1,
    TABLE2
WHERE
    TABLE1.SUBST_ID = TABLE2.SERIAL_ID
SET 
    TABLE2.BRANCH_ID = TABLE1.CREATED_ID;
1
задан Jonny Phelps 17 January 2019 в 14:40
поделиться

1 ответ

У меня есть решение после небольшого утешения, но я бы предпочел что-то более эффективное, если оно существует.

library(stringi)
# convert to string
setkey(input, Parent)
sep <- ">>"
split_regex <- "(?<=%1$s)[^(%1$s)]*$" 
trees <- sprintf("%s%s%s", input$Parent, sep, input$Child)
# get the base nodes, the children
children <- stri_extract_first_regex(trees, sprintf(split_regex, sep),
                                     simplify = TRUE)
# find that which are parents
grid <- input[J(unique(children)), ][!is.na(Child), ]
update <- unique(grid$Parent)
N <- nrow(grid)

while(N > 0){ 

  # add the children on for the ones at the base of the chains, might mean 
  # making more tree splits
  all_trees <- unique(unlist(lapply(update, function(x){
    pos <- children == x
    y <- grid[Parent %in% x, Child]
    trees <- c(trees[!pos], CJ(trees[pos], y)[, sprintf("%s%s%s", V1, sep, V2)])
    trees
  })))
  # I have some trees embedded now, so remove these ones
  trim <- sapply(seq_along(all_trees), function(i){
    any(stri_detect_fixed(all_trees[-i], all_trees[i]))
  })
  trees <- all_trees[!trim]

  # update operations on expanded trees until no children remain with a dependency
  children <- stri_extract_first_regex(trees, sprintf(split_regex, sep, sep),
                                      simplify = TRUE)
  grid <- input[J(unique(children)), ][!is.na(Child), ]
  update <- unique(grid$Parent)
  N <- nrow(grid)
}

# re-structure to appropriate format
output <- data.table(pattern = trees)
output[, Tree := 1:.N]
output[, split := stri_split_regex(pattern, sep)]
output <- output[, .(List = split[[1]],
                     Hierarchy = 1:length(split[[1]])), by=Tree]
output[]
0
ответ дан Jonny Phelps 17 January 2019 в 14:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: