Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlquery in R does not return all rows from query

I am executing below commands in R:

dbhandle <- odbcDriverConnect('driver={SQL Server};server=serveripaddress;database=DBName;uid=sa;pwd=pwd;')

FactActivity <- sqlQuery(dbhandle, "SELECT DimCourseID,DimPatientID FROM DWH.FactActivity", as.is=TRUE)

nrow(FactActivity)

In my database I have total 238634 rows but in R I am getting total rows = 237652.

This is happening for all tables (I tried it for three tables).

Any idea what am I missing which is reducing no of rows in my resultset in R?

like image 480
Radhi Avatar asked Apr 04 '17 07:04

Radhi


People also ask

Which query will return all the rows?

Answer. Answer: SELECT query is used to retrieve data from a table. It is the most used SQL query. We can retrieve complete table data, or partial by specifying conditions using the WHERE clause.

How can you guarantee that one row will be returned from a query?

You can use the FETCH FIRST 1 ROW ONLY clause in a SELECT statement to ensure that only one row is returned. This action prevents undefined and unpredictable data from being returned when you specify the INTO clause of the SELECT statement.

Can result set records be restricted to certain rows?

So there is no limit to the number of rows that a ResultSet can contains, nor to the number of rows that a java client program can process. The only limit comes if you try to load all the rows in memory to populate a list for example and exhaust the client application memory.

How return all rows and columns in SQL?

SELECT * FROM <TableName>; This SQL query will select all columns and all rows from the table. For example: SELECT * FROM [Person].

Why does my SQL query return no rows?

That said, if you eliminate filter criteria from your query and still return no rows, then you know the problem is in your joins. Could be you need a LEFT or RIGHT join someplace instead of all INNERs. IF you suddenly do start returning rows, then your filters are the problem.

How do I run a SQL query in R?

Simply paste your SQL code into the R function as a quoted string. This method is sometimes referred to as pass through SQL code, and is probably the simplest way to query your data. Care should be used to escape your quotes as needed. For example, 'yes' is written as \'yes\'.

Why is my query not returning any results?

- SQL Server Forum Why Query is not returning any results? First and foremost, the following items are a test of something bigger. The following is a create table script for the tables, cestest and cescode. Followed by a some sample data for both. Lastly, is the query that I am trying to run. I get column headers but no results.

How do I use SQL in an R notebook?

To use SQL, open an R Notebook in the RStudio IDE under the File > New File menu. Start a new code chunk with {sql}, and specify your connection with the connection=con code chunk option.


1 Answers

I had the same problem, couldn't solve it, eventually switched to using RJDBC with a Java driver to get around it.

like image 76
WHoekstra Avatar answered Oct 05 '22 22:10

WHoekstra