SQL查询不起作用(rsqlite_send_query中的错误)

时间:2017-04-21 16:42:35

标签: sql r sqldf

这就是我数据框的头部

> head(d19_1)
  SMZ SIZ1_diff SIZ1_base SIZ2_diff SIZ2_base SIZ3_diff SIZ3_base SIZ4_diff SIZ4_base SIZ5_diff SIZ5_base
1   1      -620      4170      -189      1347       -35      2040        82      1437       244      1533
2   2      -219       831       -57       255        -4       392         8       282        14       297
3   3      -426       834      -162       294      -134       379       -81       241       -22       221
4   4      -481       676      -142       216      -114       267       -50       158       -43       166
5   5      -233      1711      -109       584        54       913        71       624        74       707
6   6      -322      1539       -79       512       -50       799        23       532        63       576
  Total_og Total_base   %_SIZ1   %_SIZ2    %_SIZ3    %_SIZ4    %_SIZ5 Total_og Total_base
1    11980      12648 14.86811 14.03118  1.715686  5.706333 15.916504    11980      12648
2     2156       2415 26.35379 22.35294  1.020408  2.836879  4.713805     2156       2415
3     1367       2314 51.07914 55.10204 35.356201 33.609959  9.954751     1367       2314
4      790       1736 71.15385 65.74074 42.696629 31.645570 25.903614      790       1736
5     5339       5496 13.61777 18.66438  5.914567 11.378205 10.466761     5339       5496
6     4362       4747 20.92268 15.42969  6.257822  4.323308 10.937500     4362       4747

数据框的数据类型如下所示str(d19_1)

> str(d19_1)
'data.frame':   1588 obs. of  20 variables:
 $ SMZ       : int  1 2 3 4 5 6 7 8 9 10 ...
 $ SIZ1_diff : int  -620 -219 -426 -481 -233 -322 -176 -112 -34 -103 ...
 $ SIZ1_base : int  4170 831 834 676 1711 1539 720 1396 998 1392 ...
 $ SIZ2_diff : int  -189 -57 -162 -142 -109 -79 -12 72 -36 -33 ...
 $ SIZ2_base : int  1347 255 294 216 584 512 196 437 343 479 ...
 $ SIZ3_diff : int  -35 -4 -134 -114 54 -50 16 4 26 83 ...
 $ SIZ3_base : int  2040 392 379 267 913 799 361 804 566 725 ...
 $ SIZ4_diff : int  82 8 -81 -50 71 23 36 127 46 75 ...
 $ SIZ4_base : int  1437 282 241 158 624 532 242 471 363 509 ...
 $ SIZ5_diff : int  244 14 -22 -43 74 63 11 143 79 125 ...
 $ SIZ5_base : int  1533 297 221 166 707 576 263 582 429 536 ...
 $ Total_og  : int  11980 2156 1367 790 5339 4362 2027 4715 3465 4561 ...
 $ Total_base: int  12648 2415 2314 1736 5496 4747 2168 4464 3278 4375 ...
 $ %_SIZ1    : num  14.9 26.4 51.1 71.2 13.6 ...
 $ %_SIZ2    : num  14 22.4 55.1 65.7 18.7 ...
 $ %_SIZ3    : num  1.72 1.02 35.36 42.7 5.91 ...
 $ %_SIZ4    : num  5.71 2.84 33.61 31.65 11.38 ...
 $ %_SIZ5    : num  15.92 4.71 9.95 25.9 10.47 ...
 $ Total_og  : int  11980 2156 1367 790 5339 4362 2027 4715 3465 4561 ...
 $ Total_base: int  12648 2415 2314 1736 5496 4747 2168 4464 3278 4375 ...

当我运行以下查询时,它会返回以下错误,我不知道原因。我在表格中没有任何专栏

查询

d20_1 <- sqldf('SELECT *, CASE
         WHEN SMZ BETWEEN 1 AND 110 THEN "Baltimore City"
         WHEN SMZ BETWEEN 111 AND 217 THEN "Anne Arundel County"
         WHEN SMZ BETWEEN 218 AND 405 THEN "Baltimore County"
         WHEN SMZ BETWEEN 406 AND 453 THEN "Carroll County"
         WHEN SMZ BETWEEN 454 AND 524 THEN "Harford County"
         WHEN SMZ BETWEEN 1667 AND 1674 THEN "York County"
         ELSE 0
         END Jurisdiction
         FROM d19_1')

错误:

Error in rsqlite_send_query(conn@ptr, statement) : 
  table d19_1 has no column named <NA>

1 个答案:

答案 0 :(得分:1)

您的代码可以正常使用我:

d19_1 <- structure(list(SMZ = 1:6, SIZ1_diff = c(-620L, -219L, -426L,
-481L, -233L, -322L), SIZ1_base = c(4170L, 831L, 834L, 676L,
1711L, 1539L), SIZ2_diff = c(-189L, -57L, -162L, -142L, -109L,
-79L), SIZ2_base = c(1347L, 255L, 294L, 216L, 584L, 512L), SIZ3_diff = c(-35L,
-4L, -134L, -114L, 54L, -50L), SIZ3_base = c(2040L, 392L, 379L,
267L, 913L, 799L), SIZ4_diff = c(82L, 8L, -81L, -50L, 71L, 23L
), SIZ4_base = c(1437L, 282L, 241L, 158L, 624L, 532L), SIZ5_diff = c(244L,
14L, -22L, -43L, 74L, 63L), SIZ5_base = c(1533L, 297L, 221L,
166L, 707L, 576L), Total_og = c(11980L, 2156L, 1367L, 790L, 5339L,
4362L), Total_base = c(12648L, 2415L, 2314L, 1736L, 5496L, 4747L
), X._SIZ1 = c(14.86811, 26.35379, 51.07914, 71.15385, 13.61777,
20.92268), X._SIZ2 = c(14.03118, 22.35294, 55.10204, 65.74074,
18.66438, 15.42969), X._SIZ3 = c(1.715686, 1.020408, 35.356201,
42.696629, 5.914567, 6.257822), X._SIZ4 = c(5.706333, 2.836879,
33.609959, 31.64557, 11.378205, 4.323308), X._SIZ5 = c(15.916504,
4.713805, 9.954751, 25.903614, 10.466761, 10.9375), Total_og.1 = c(11980L,
2156L, 1367L, 790L, 5339L, 4362L), Total_base.1 = c(12648L, 2415L,
2314L, 1736L, 5496L, 4747L)), .Names = c("SMZ", "SIZ1_diff",
"SIZ1_base", "SIZ2_diff", "SIZ2_base", "SIZ3_diff", "SIZ3_base",
"SIZ4_diff", "SIZ4_base", "SIZ5_diff", "SIZ5_base", "Total_og",
"Total_base", "X._SIZ1", "X._SIZ2", "X._SIZ3", "X._SIZ4", "X._SIZ5",
"Total_og.1", "Total_base.1"), row.names = c(NA, -6L), class = "data.frame")

library(sqldf)
sqldf('SELECT *, CASE
         WHEN SMZ BETWEEN 1 AND 110 THEN "Baltimore City"
         WHEN SMZ BETWEEN 111 AND 217 THEN "Anne Arundel County"
         WHEN SMZ BETWEEN 218 AND 405 THEN "Baltimore County"
         WHEN SMZ BETWEEN 406 AND 453 THEN "Carroll County"
         WHEN SMZ BETWEEN 454 AND 524 THEN "Harford County"
         WHEN SMZ BETWEEN 1667 AND 1674 THEN "York County"
         ELSE 0
         END Jurisdiction
         FROM d19_1')