【问题标题】:R language sqldf package update table not workingR语言sqldf包更新表不起作用
【发布时间】:2013-08-19 23:44:48
【问题描述】:

我将一些数据从 csv 文件导出到 R。我正在使用 sqldf 包更新数据。我的以下查询运行。我可以提供我的 csv 文件,但我不知道如何在此处附加文件:(

> sqldf("select * from
+ (
+  
+ select Disposition as dummy_disposition, case Disposition
+ when 'Disposition' then  'UNKNOWN'
+ when 'Donate' then  'EOL'
+ when 'Resale' then  'EOL'
+ when 'Harvest' then  'EOL'
+ when 'IntelSpinOff' then  'EOL'
+ when 'Scrap' then  'EOL'
+ when 'BufferFeedForward' then   'ACTIVE'
+ when 'FB' then  'UNKNOWN'
+ when 'Unknown' then  'UNKOWN'
+ when 'HarvestResale' then  'EOL'
+ when 'ReturnToSupplier' then  'EOL'
+ when 'IcapResale' then  'EOL'
+ when 'NonEmsManaged' then  'EOL'
+ when 'BufferEOL' then  'EOL'
+ when 'ResaleScrap' then  'EOL'
+ when 'ST' then  'UNKNOWN'
+ when 'AS' then  'UNKNOWN'
+ else null end as new_status,
+  
+ *  from a where FloorStatus is null and  disposition is not null
+  
+  
+ )  as table1
+   where new_status is null")
 [1] dummy_disposition new_status        Ueid              Date              EndDate          
 [6] Site              CFD               Type              ScheduleId        EventType        
[11] FloorStatus       EntityCode        Ceid              Process           Supplier         
[16] FA                MfgType           Disposition       Comments          extra1           
[21] extra2            extra3           
<0 rows> (or 0-length row.names)

但是当我在查询下运行时它不会运行:(这很有趣,因为除了更新表部分之外,其余查询是上述查询的一部分。

> sqldf("update a set FloorStatus = case Disposition
+ when 'Disposition' then  'UNKNOWN'
+ when 'Donate' then  'EOL'
+ when 'Resale' then  'EOL'
+ when 'Harvest' then  'EOL'
+ when 'IntelSpinOff' then  'EOL'
+ when 'Scrap' then  'EOL'
+ when 'BufferFeedForward' then   'ACTIVE'
+ when 'FB' then  'UNKNOWN'
+ when 'Unknown' then  'UNKNOWN'
+ when 'HarvestResale' then  'EOL'
+ when 'ReturnToSupplier' then  'EOL'
+ when 'IcapResale' then  'EOL'
+ when 'NonEmsManaged' then  'EOL'
+ when 'BufferEOL' then  'EOL'
+ when 'ResaleScrap' then  'EOL'
+ when 'ST' then  'UNKNOWN'
+ when 'AS' then  'UNKNOWN'
+ else null end
+    from a where FloorStatus is null and  disposition is not null")
Error in sqliteExecStatement(con, statement, bind.data) : 
  RS-DBI driver: (error in statement: near "from": syntax error)

我的更新表命令有什么问题?当我在 sql development studio 中运行我的代码时,它运行良好......但我更喜欢代码在 R 中运行

更新:

我需要对以下代码进行哪些更改????

declare @outputs table 
([day] datetime,
floorstatus varchar,  
 quantity int)

declare @Date datetime;


DECLARE dates_cursor CURSOR FOR
SELECT clndr_dt from epsd.dbo.t_calendar
              where clndr_dt between '2008-01-01' and '2013-08-13'
              order by clndr_dt
OPEN dates_cursor
FETCH NEXT from dates_cursor INTO @Date

WHILE @@FETCH_STATUS = 0
    BEGIN

insert @outputs
       select @Date as [Day], floorstatus, count(floorstatus) as quantity from Graph_data
       where @Date between [Date] and EndDate
       group by floorstatus

FETCH NEXT from dates_cursor INTO @Date
END
CLOSE dates_cursor
DEALLOCATE dates_cursor

select * from @outputs

【问题讨论】:

    标签: sql r sqldf


    【解决方案1】:

    Update 没有from 子句(请参阅update syntax on SQLite site),另请参阅sqldf FAQ #8,了解在 SQLite 中更新表但未将表返回给 R 的常见错误。

    【讨论】:

    • 您的建议有帮助...对我的更新/新问题有任何想法吗?
    • 在 sqldf 中,您一次完成所有操作,而不是逐行执行。如果问题基本上是如何插入变量,请参见 sqldf 主页上的示例 5。 code.google.com/p/sqldf/#Example_5._Insert_Variables
    • 我的问题有点不同......我正在编写一个游标,因为我想遍历我的数据。我的数据的每一行都有 2 列 - 开始日期和结束日期。我将从 1-1-2008 开始,如果我的日期在开始和结束日期列中,我将计算其他列中的条目。我正在写一个游标,因为写一个游标很容易,而且它提供了快速的输出。我的光标代码没有在 R 中运行。有没有更好的方法在 R 和 sqldf 中实现相同的目标?谢谢
    • 您可以使用wherelimit 子句来获取输出的一部分,或者如果大小不是问题,只需一次获取所有输出。或者使用支持游标的 RSQLite 包。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2015-07-16
    • 2017-11-27
    • 2012-01-05
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多