Sql-Server

有沒有辦法使用 MS SQL Server 2016 將兩個沒有 JOIN 的 SQL 表傳遞到 R 中?

  • August 9, 2016

有沒有辦法使用 MS SQL Server 2016 將兩個沒有 JOIN 的 SQL Server 表傳遞到 R 中?

例如@input_data_1=TABLE, 將 ‘TABLE’ 作為數據框傳遞到具有預設變數名稱的 R 中InputDataSet

有沒有辦法通過 R 以某種方式訪問駐留在 SQL Server 中的其他表?

@input_data_1可以是 T-SQL 語句,也可以是對隱藏複雜 SQL 語句的視圖的引用,所以事情真的可以像你做的那樣複雜。下面是一些關於如何組合相同或不同結構的表的簡單範例。可以想像,您可以在 r 腳本中對錶源列 ( xsource) 進行一些過濾:

DROP VIEW IF EXISTS dbo.vw_union1
DROP VIEW IF EXISTS dbo.vw_union2
DROP TABLE IF EXISTS dbo.table1
DROP TABLE IF EXISTS dbo.table2
DROP TABLE IF EXISTS dbo.table3
GO

CREATE TABLE dbo.table1 (
   col1 INT NOT NULL,
   col2 INT NOT NULL,

   CONSTRAINT PK_table1 PRIMARY KEY ( col1 )
)
GO

-- Same structure as table1
CREATE TABLE dbo.table2 (
   col1 INT NOT NULL,
   col2 INT NOT NULL,

   CONSTRAINT PK_table2 PRIMARY KEY ( col1 )
)
GO

-- Different structure to table1
CREATE TABLE dbo.table3 (
   col7 VARCHAR(5) NOT NULL,
   col8 DATE NOT NULL,

   CONSTRAINT PK_table3 PRIMARY KEY ( col7 )
)
GO

INSERT INTO dbo.table1 ( col1, col2 )
VALUES 
   ( 1, 2 ),
   ( 3, 4 )


INSERT INTO dbo.table2 ( col1, col2 )
VALUES 
   ( 1, 6 ),
   ( 3, 8 )

GO

INSERT INTO dbo.table3 ( col7, col8 )
VALUES 
   ( 'A', GETDATE() ),
   ( 'B', GETDATE() )

GO

CREATE VIEW dbo.vw_union1
AS
SELECT 'table1' AS xsource, col1, col2 
FROM dbo.table1
UNION ALL
SELECT 'table2' AS xsource, col1, col2 
FROM dbo.table2
GO


CREATE VIEW dbo.vw_union2
AS
SELECT 'table1' AS xsource, col1, col2 , NULL AS col7, NULL AS col8
FROM dbo.table1
UNION ALL
SELECT 'table3' AS xsource, NULL AS col1, NULL AS col2, col7, col8 
FROM dbo.table3
GO

-- Long SQL statement; same text as vw_union1
EXEC dbo.sp_execute_external_script
   @language = N'R',
   @input_data_1 = N'SELECT ''table1'' AS xsource, col1, col2 FROM dbo.table1 UNION ALL SELECT ''table2'' AS xsource, col1, col2 FROM dbo.table2',
   @script = N'OutputDataSet <- aggregate(InputDataSet$col2, by=list(InputDataSet$col1), FUN=median)'
GO

-- SELECT from view where structures are the same
EXEC dbo.sp_execute_external_script
   @language = N'R',
   @input_data_1 = N'SELECT xsource, col2 FROM dbo.vw_union1',
   @script = N'OutputDataSet <- aggregate(InputDataSet$col2, by=list(InputDataSet$xsource), FUN=median)'
GO


-- SELECT from view where structures are the different
EXEC dbo.sp_execute_external_script
   @language = N'R',
   @input_data_1 = N'SELECT xsource, col2 FROM dbo.vw_union2',
   @script = N'OutputDataSet <- aggregate(InputDataSet$col2, by=list(InputDataSet$xsource), FUN=median)'
GO

-- subset example
EXEC dbo.sp_execute_external_script
   @language = N'R',
   @input_data_1 = N'SELECT xsource, col2 FROM dbo.vw_union1',
   @script = N'InputDataSet <- subset( InputDataSet, xsource == "table1")
OutputDataSet <- aggregate(InputDataSet$col2, by=list(InputDataSet$xsource), FUN=median)'
GO

此時沒有@input_data_2@input_data_n

正如 wBob 建議的那樣,在兩個表上使用 UNION 創建一個 VIEW,使用 R 導入它,然後根據要求切片所需的屬性以獲取兩個數據幀或數據幀工作正常。

但出於我的目的,使用 RevoScaleR 庫在 SQL Server 中直接通過 R 導入表效果更好。下面的範常式式碼顯示了在 SQL Server 2016 中使用 RevoScaleR 包導入表的兩種不同方法:

execute sp_execute_external_script    
@language = N'R'    
, @script = N'
connStr <- "Driver=SQL Server;Server=*Server*;Database=*DB*;Uid=*User*;Pwd=*Password*";

table1 <- RxSqlServerData(connectionString = connStr, table="TABLE1");
table1_df <- rxDataStep(table1);
print(table1_df)

table2 <- RxSqlServerData(connectionString = connStr, sqlQuery=paste("SELECT * FROM TABLE2"));
OutputDataSet <- rxDataStep(table2);
'    
, @input_data_1 = N''

引用自:https://dba.stackexchange.com/questions/145614