r - RODBC 表中无可用数据
问题描述
我使用 RODBC 从 sql 获取数据
sql <- paste0("
with cte as (
Select *,datePart(WEEKDAY,Dt) as WeekDay,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY SaleCount) Over (partition by ItemRelation,
DocumentNum, DocumentYear) as PERCENTILE,
avg(SaleCount) over (Partition by ItemRelation,
DocumentNum, DocumentYear,datePart(WEEKDAY,Dt), IsPromo) as AVG_WeekDay
From [Action].[dbo].[promo_data_copy])
Update a
Set SaleCount = cte.AVG_WeekDay
From CTE
join [Action].[dbo].[promo_data_copy] a
on a.Dt = cte.dt
and a.ItemRelation=cte.ItemRelation
and a.DocumentNum = cte.DocumentNum
and a.DocumentYear = cte.DocumentYear
and a.ispromo = cte.ispromo
Where CTE.PERCENTILE < CTE.SaleCount
and datePart(WEEKDAY,CTE.Dt) < 5
and CTE.ispromo = 0 ;")
df <- sqlQuery(dbHandle, sql)
View(df)
df 是空数据集。表中无可用数据
谁能帮我理解,为什么数据没有返回?
编辑
Dt ItemRelation SaleCount DocumentNum DocumentYear IsPromo
2017-10-12 00:00:00.000 13322 7 36 2017 0
2017-10-12 00:00:00.000 13322 35 4 2017 0
2017-10-12 00:00:00.000 158121 340 41 2017 0
2017-10-12 00:00:00.000 158122 260 41 2017 0
2017-10-13 00:00:00.000 13322 3 36 2017 0
2017-10-13 00:00:00.000 13322 31 4 2017 0
2017-10-13 00:00:00.000 158121 420 41 2017 0
2017-10-13 00:00:00.000 158122 380 41 2017 0
2017-10-14 00:00:00.000 11592 45 33 2017 0
2017-10-14 00:00:00.000 13189 135 33 2017 0
2017-10-14 00:00:00.000 13191 852 33 2017 0
2017-10-14 00:00:00.000 13322 1 36 2017 0
2017-10-14 00:00:00.000 13322 34 4 2017 0
2017-10-14 00:00:00.000 158121 360 41 2017 0
2017-10-14 00:00:00.000 158122 140 41 2017 0
这里是表的前 15 个观察结果。所以我希望我的查询会返回这个 data.frame
解决方案
我不确定百分位数的东西;我会把它留给你来理顺这部分。无论如何,这是我使用 R 查询数据库的方法。
library(RODBC)
dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=Server_Name; Database=DB_Name;Uid=; Pwd=; trusted_connection=yes")
initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
odbcClose(channel)
这里有一些非常有用的交叉参考资源。
http://stackoverflow.com/questions/15420999/rodbc-odbcdriverconnect-connection-error
https://andersspur.wordpress.com/2013/11/26/connect-r-to-sql-server-2012-and-14/
推荐阅读
- sql - 如何使用 SQL 从数据库中获取一条记录并获取该记录的所有警告?
- css - GET http://localhost:8080/static/css/1.e8747c0d.chunk.css net::ERR_ABORTED 404
- vb.net - WCF 抛出 System.ServiceModel.FaultException:访问被拒绝
- pipe - 使用标准输入的交互式 shell
- sql - 01-JAN-15 00:00:00 在 SQL Developer 中使用 SQL 更改为 01-JAN-2015
- css - 在 css 中完成的元素分组,改变了基于断点分组的元素数量
- c# - 需要从存储库层访问登录用户
- java - 具有复合主键的@OneToMany 映射未按预期工作
- android - 关于Vulkan初始化/去初始化和Android App生命周期事件的问题
- android - JavaBinder:未捕获的远程异常!(跨进程还不支持异常?