【问题标题】:Filtering with sqldf in R when fields have quotation marks当字段带有引号时,在 R 中使用 sqldf 进行过滤
【发布时间】:2021-01-24 17:08:41
【问题描述】:

我有一个大型 sql db (7gbs),其中的字段似乎带有引号。

例如:

res <- dbSendQuery(con, "
                   SELECT *
                   FROM master")

dbf2 <- fetch(res, n = 3)

dbClearResult(res)

产量

 NPI                EntityTypeCode ReplacementNPI EmployerIdentificationNumber.EIN.
1 "1679576722"            "1"             ""                                ""
2 "1588667638"            "1"             ""                                ""
3 "1497758544"            "2"             ""                       "<UNAVAIL>"
  ProviderOrganizationName.LegalBusinessName. ProviderLastName.LegalName. ProviderFirstName
1                                          ""                     "WIEBE"           "DAVID"
2                                          ""                   "PILCHER"         "WILLIAM"
3    "CUMBERLAND COUNTY HOSPITAL SYSTEM, INC"                          ""                ""

我一直试图通过过滤来获得一个更小的表格,比如EntityTypeCode,但我没有得到任何结果。这是一个查询没有得到任何东西的例子,有什么建议吗?我认为问题在于在字段中使用双引号。

# Filter on State 
res <- dbSendQuery(npi2, "
                   SELECT * 
                   FROM master
                   WHERE (ProviderBusinessPracticeLocationAddressStateName = 'PA') 
                   ")

# Filter on State and type 
res <- dbSendQuery(npi2, "
                   SELECT * 
                   FROM master
                   WHERE (ProviderBusinessPracticeLocationAddressStateName = 'PA') AND 
                   (EntityTypeCode = '1') 
                   ")

【问题讨论】:

    标签: sql r sqldf


    【解决方案1】:

    使用\ 转义内部双引号(ie,即单元格中的双引号)。

    res <- dbSendQuery(npi2, "
                       SELECT * 
                       FROM master
                       WHERE (ProviderBusinessPracticeLocationAddressStateName = '\"PA\"') AND 
                       (EntityTypeCode = '1') 
                       ")
    

    这会产生以下字符串:

                       SELECT * 
                       FROM master
                       WHERE (ProviderBusinessPracticeLocationAddressStateName = '"PA"') 
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2018-01-17
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2018-08-23
      相关资源
      最近更新 更多