How do I export a SQL query into SPSS?

ε祈祈猫儿з 提交于 2019-12-04 11:43:36

You can use GET DATA procedure to import data from SQL directly in SPSS. See the SQL subcommand. You can use your complicated query here. For example:

GET DATA
 /TYPE = ODBC
 /CONNECT = "DSN = DSNname"
 /SQL = "SELECT * FROM empl_data "
        "WHERE ((bdate>=#1/1/1960# and edate<=#12/31/1960#) or bdate is null)".

It is clear why (values (N'BAS1',BAS1) caused the error. Because you are using single quotes for the argument of the SQL subcommand \SQL = ' '. And the first single quote in (values (N'BAS1',BAS1) defines the end of the argument. Switching to double quotes solves it.

I tried to rearrange your code. I can not test it, but I believe it should work:

GET DATA
  /TYPE = ODBC 
  /CONNECT = "DSN=temp_Hisp;DATABASE=temp_HispTreat"
  /SQL = "With CTE_BASENG As (Select StudyID, Visit, Question, "
           "CAST(Response As Int) As RESPONSE "
         "from temp_HispTreat.dbo.BAS AS PVTable "
         "outer apply (values (N'BAS1',BAS1), (N'BAS24',BAS24)) "
           "P(Question, Response)) "
         "select SubVis.IRB#, SubVis.StudyID, SubVis.Clin_num, Subvis.Visit, "
           "BASENG.BAS_ENGTOT "
         "From (Select Distinct IRB#, StudyID, Clin_Num, Visit_ID As Visit "
         "from temp_HispTreat.dbo.Subjects, temp_HispTreat.dbo.StudyStructure "
         "where subjects.IRB# = 5516 and StudyStructure.IRB = 5516) As SubVis "
         "left join (Select StudyID, Visit, "
           "SUM(Scoring.dbo.GetValue9(response)) As BAS_ENGTOT "
         "from CTE_BASENG group by StudyID, Visit) AS BASENG On "
           "SubVis.Studyid = BASENG.StudyID And SubVis.Visit = BASENG.Visit".

The SQL is processed by the ODBC driver, so the capabilities of that driver will determine what sort of SQL can be issued. The capabilities may be database specific. Someetimes there are multiple drivers available for a particular database, some from the IBM SPSS Data Access Pack and some from a db vendor directly, so you may want to investigate what is available for your particular database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!