如何将RODBC与包含DDL命令的查询一起使用?

问题描述 投票:0回答:1

我正在尝试使用R清除以下SQL命令的输出数据,但是在完成加载后会导致数据集为空。查询的主要目的是为组中的每个人创建多个时间序列

这是我到目前为止所拥有的:

conn<-odbcConnect("ks208")
Query<-"


---temp table for daily balances
Create Table #dmhi_hist
(rowno_dmddmhi_dmd int,
balance float, 
amount float,
effective date,
)

Insert Into #dmhi_hist

SELECT data_.rowno_dmddmhi_dmd,
       dmd.balance,
       Sum(data_.amount) AS amount,
       data_.effective
FROM   (
       --Top False Cap 
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              0                               AS amount,
              CONVERT(Date, Getdate()) AS effective
       FROM   ks208.dbo.dmd
              INNER JOIN dmon208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
       UNION
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              dmhi.amount,
              CONVERT(Date, dmhi.effective) AS effective
       FROM   ks208.dbo.dmd
              INNER JOIN ks208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
       UNION
       SELECT dmhi.rowno,
              dmhi.rowno_dmddmhi_dmd,
              dmhi.amount,
              CONVERT(Date, dmhi.effective) AS effective
       FROM   dmon208.dbo.dmd
              INNER JOIN dmon208.dbo.dmhi
                      ON dmd.rowno = dmhi.rowno_dmddmhi_dmd
        -- False Bottom Cap
        UNION
        SELECT dmhi.rowno,
               dmhi.rowno_dmddmhi_dmd,
               0                                                  AS amount,
               CONVERT(Date, Dateadd(year, -4, Getdate())) AS effective
        FROM   ks208.dbo.dmd
               INNER JOIN dmon208.dbo.dmhi
                       ON dmd.rowno = dmhi.rowno_dmddmhi_dmd) AS data_
       INNER JOIN ks208.dbo.dmd
               ON dmd.rowno = data_.rowno_dmddmhi_dmd
WHERE  Year(data_.effective) >= Year(Getdate()) - 4


GROUP  BY data_.rowno_dmddmhi_dmd,
          data_.effective,
          dmd.balance 

--- Joining the balances and transations to the calandar
DECLARE @start_date DATE = Dateadd(year, -4, Getdate());
DECLARE @end_date DATE = getdate();


    WITH    AllDays
          AS ( SELECT   @start_date AS [Date]
               UNION ALL
               SELECT   DATEADD(DAY, 1, [Date])
               FROM     AllDays
               WHERE    [Date] < @end_date )
Insert Into #dmhi_hist(effective,rowno_dmddmhi_dmd,amount,balance)
     SELECT [Date] as effective,dmd.rowno as rowno_dmddmhi_dmd,isnull(#dmhi_hist.amount,0) as amount,dmd.balance
     FROM   AllDays 
     cross join KS208.dbo.dmd
     left outer join #dmhi_hist on AllDays.Date = #dmhi_hist.effective and dmd.rowno = #dmhi_hist.rowno_dmddmhi_dmd
     OPTION (MAXRECURSION 0)

     select * from #dmhi_hist


"
conn<-odbcConnect("ks208")
dataset <-sqlQuery(conn,Query)

是RODBC仅限于查询还是它的命令结构?

r tsql ssms rodbc
1个回答
0
投票

sqlQuery()调用中仅允许单个语句。或者,您可以将所有内容保存在存储过程中,并使用sqlQuery()进行调用。或者更好的是,避免使用临时表,INSERTDECLARE行,并扩展对公用表表达式(CTE)的使用。假定您的数据库在同一条语句中支持递归和非递归CTE。

WITH dmhi_hist AS (

   SELECT data_.rowno_dmddmhi_dmd
        , dmd.balance
        , SUM(data_.amount) AS amount
        , data_.effective
   FROM   
     (  
       SELECT dmhi.rowno
              , dmhi.rowno_dmddmhi_dmd
              , 0 AS amount
              CONVERT(Date, Getdate()) AS effective
       FROM   ks208.dbo.dmd
       INNER JOIN dmon208.dbo.dmhi
          ON dmd.rowno = dmhi.rowno_dmddmhi_dmd

       UNION

       SELECT dmhi.rowno
              , dmhi.rowno_dmddmhi_dmd
              , dmhi.amount
              , CONVERT(Date, dmhi.effective) AS effective
       FROM   ks208.dbo.dmd
       INNER JOIN ks208.dbo.dmhi
          ON dmd.rowno = dmhi.rowno_dmddmhi_dmd

       UNION

       SELECT dmhi.rowno
              , dmhi.rowno_dmddmhi_dmd
              , dmhi.amount
              , CONVERT(Date, dmhi.effective) AS effective
       FROM   dmon208.dbo.dmd
       INNER JOIN dmon208.dbo.dmhi
           ON dmd.rowno = dmhi.rowno_dmddmhi_dmd

       UNION

       SELECT dmhi.rowno
              , dmhi.rowno_dmddmhi_dmd
              , 0  AS amount
              , CONVERT(Date, Dateadd(Year, -4, Getdate())) AS effective
       FROM   ks208.dbo.dmd
       INNER JOIN dmon208.dbo.dmhi
          ON dmd.rowno = dmhi.rowno_dmddmhi_dmd) AS data_
       INNER JOIN ks208.dbo.dmd
          ON dmd.rowno = data_.rowno_dmddmhi_dmd
     ) sub

   WHERE  Year(data_.effective) >= Year(Getdate()) - 4

   GROUP  BY data_.rowno_dmddmhi_dmd
             , data_.effective
             , dmd.balance
   ),

    AllDays AS ( 
      SELECT  Dateadd(year, -4, Getdate()) AS [Date]
      UNION ALL
      SELECT   DATEADD(DAY, 1, [Date])
      FROM     AllDays
      WHERE    [Date] < Getdate() 
   )

SELECT effective
       , rowno_dmddmhi_dmd
       , amount
       , balance
FROM dmhi_hist

UNION ALL

SELECT [Date] as effective 
       , dmd.rowno AS rowno_dmddmhi_dmd
       , ISNULL(#dmhi_hist.amount, 0) AS amount
       , dmd.balance
FROM   AllDays 
CROSS JOIN KS208.dbo.dmd
LEFT OUTER JOIN dmhi_hist 
   ON AllDays.Date = dmhi_hist.effective 
   AND dmd.rowno = dmhi_hist.rowno_dmddmhi_dmd

OPTION (MAXRECURSION 0)
© www.soinside.com 2019 - 2024. All rights reserved.