sql—向表中添加有关键列结构的行

t40tm48m  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(193)

我有一个非常大的表,它遵循这些结构(我在这里简化了它):
产品线名称数量单位成本PEPE10000 LUCIA4UD8PEPEE20000SANTIAGO7UD5.5PEPE30000MARIANGELES10KG6ANTONIO10000NAIARA4KG8ANTONIO20000TONI7KG3VANESA1000LUCIA4UD8VANESA200000SANTIAGO7KG8VANESA30000TONI10KG3VANESA40000GINES4KG8
我需要为每个产品添加一个新名称(dany),因此该表如下所示:
姓名:丹妮;quantity:15; 单位:升;成本:2
结果是:
产品线名称数量单位成本PEPE100000LUCIA4UD8PEPEE20000SANTIAGO7UD5.5PEPE300000MARIANGELES10KG6PEPEPE40000DANY15L2ANTONIO10000NAIAR2KG8ANTONIO300000DANY15L2VANEA10000LUCIA4UD8VANEA20000SANTIAGO7KG8VANESA300000TONI10KG3VANESA40000GINES4KG8VANESA50000DANY15L2
事实上,“行”必须是现有行的后续编号(例如,如果产品的姓是30000(见pepe),dany的行必须是40000,如果产品的姓是20000,dany的行必须是30000(见antonio))。
我可以使用sql(dbeaver或microsoftaccess)或r来实现,我正在考虑一个r循环或一个带有count()的sql复杂查询,但是如果您能提供一些帮助,我将不胜感激。
非常感谢。

tpgth1q7

tpgth1q71#

您可以使用 insert :

insert into t (Product, Line, Name, Quantity, Unit, Cost)
    select product, max(line) + 10000, 'Dany', 15, 'L', 2
    from t
    group by product;

如果不想修改表,只想查看数据,可以运行查询:

select Product, Line, Name, Quantity, Unit, Cost
from t
union all
select product, max(line) + 10000, 'Dany', 15, 'L', 2
from t
group by product
wwtsj6pe

wwtsj6pe2#

我相信sql可能是更好的地方,但是如果您想知道如何在r中实现它,我假设您已经将相关数据下载到 dat .
(在这三个例子中 order / arrange 仅用于演示,生产中不需要。)

底部r

newdat <- data.frame(Quantity=15L, Name="Dany", Unit="L", Cost=2)
newlines <- aggregate(dat$Line, list(Product=dat$Product), FUN=function(z) max(z) + 10000)
names(newlines)[2] <- "Line"
newlines

# Product  Line

# 1 Antonio 30000

# 2    Pepe 40000

# 3  Vanesa 50000

out <- rbind(dat, merge(newlines, newdat, by = NULL))
out <- out[order(out$Product, out$Line),]
out

# Product  Line        Name Quantity Unit Cost

# 4  Antonio 10000      Naiara        4   KG  8.0

# 5  Antonio 20000        Toni        7   KG  3.0

# 10 Antonio 30000        Dany       15    L  2.0

# 1     Pepe 10000       Lucia        4   UD  8.0

# 2     Pepe 20000    Santiago        7   UD  5.5

# 3     Pepe 30000 Mariangeles       10   KG  6.0

# 11    Pepe 40000        Dany       15    L  2.0

# 6   Vanesa 10000       Lucia        4   UD  8.0

# 7   Vanesa 20000    Santiago        7   KG  8.0

# 8   Vanesa 30000        Toni       10   KG  3.0

# 9   Vanesa 40000       Gines        4   KG  8.0

# 12  Vanesa 50000        Dany       15    L  2.0

潮人

library(dplyr)

# library(tidyr) # crossing

# newdat from above

dat %>%
  group_by(Product) %>%
  summarize(Line = max(Line) + 10000) %>%
  tidyr::crossing(., newdat) %>%
  bind_rows(dat) %>%
  arrange(Product, Line)

# # A tibble: 12 x 6

# Product  Line Quantity Name        Unit   Cost

# <chr>   <dbl>    <int> <chr>       <chr> <dbl>

# 1 Antonio 10000        4 Naiara      KG      8

# 2 Antonio 20000        7 Toni        KG      3

# 3 Antonio 30000       15 Dany        L       2

# 4 Pepe    10000        4 Lucia       UD      8

# 5 Pepe    20000        7 Santiago    UD      5.5

# 6 Pepe    30000       10 Mariangeles KG      6

# 7 Pepe    40000       15 Dany        L       2

# 8 Vanesa  10000        4 Lucia       UD      8

# 9 Vanesa  20000        7 Santiago    KG      8

# 10 Vanesa  30000       10 Toni        KG      3

# 11 Vanesa  40000        4 Gines       KG      8

# 12 Vanesa  50000       15 Dany        L       2

数据表

library(data.table)
datDT <- as.data.table(dat)
newdatDT <- as.data.table(newdat)
newlinesDT <- datDT[, .(Line = max(Line) + 10000), by = .(Product)]

rbindlist(list(
  datDT,
  base::merge.data.frame(newlinesDT, newdat, by = NULL)
), use.names = TRUE)[ order(Product,Line),]

# Product  Line        Name Quantity   Unit  Cost

# <char> <num>      <char>    <int> <char> <num>

# 1: Antonio 10000      Naiara        4     KG   8.0

# 2: Antonio 20000        Toni        7     KG   3.0

# 3: Antonio 30000        Dany       15      L   2.0

# 4:    Pepe 10000       Lucia        4     UD   8.0

# 5:    Pepe 20000    Santiago        7     UD   5.5

# 6:    Pepe 30000 Mariangeles       10     KG   6.0

# 7:    Pepe 40000        Dany       15      L   2.0

# 8:  Vanesa 10000       Lucia        4     UD   8.0

# 9:  Vanesa 20000    Santiago        7     KG   8.0

# 10:  Vanesa 30000        Toni       10     KG   3.0

# 11:  Vanesa 40000       Gines        4     KG   8.0

# 12:  Vanesa 50000        Dany       15      L   2.0

我不知道怎么去 data.table:::merge.data.table 允许没有连接键的笛卡尔连接,所以我强制 base -这是我的案子。我可以在两个框架中添加一个虚拟的单值列,然后作为另一个选项连接到该列。

相关问题