简体   繁体   English

使用RODBC连接从R中的SQL查询获取精确的百分比

[英]Getting precise percentages from a SQL Query in R using the RODBC Connection

I am trying to create a function in R using the RODBC package to loop through five tables in a SQL database to find the count of rows for each year for each variable, the % null for each year for each var, and the % missing for each year for each var. 我正在尝试使用RODBC包在R中创建一个函数,以循环遍历SQL数据库中的五个表,以查找每个变量每年的行数,每个变量每年的%null以及缺少的%每个变种每年 I am having trouble creating a single function to give me this accurate output. 我在创建单个函数以提供此准确输出方面遇到麻烦。 I created a function that outputs the total count and percent null, but cannot seem to get it to produce an accurate percent missing directly - it seems to be rounding to whole integers, and not consistently rounding up or down. 我创建了一个输出总计数和null百分比的函数,但似乎无法使它直接产生准确的百分比缺失-它似乎是舍入为整数,而不是始终舍入或舍入。 Below is my code. 下面是我的代码。 Any help with this would be much appreciated. 任何帮助,将不胜感激。

PctNull <- sqlQuery(channel, "select 
                [EVENT_YEAR] AS 'YEAR', 
                COUNT(*) AS 'TOTAL',
                (((COUNT(CASE WHEN MOTHER_EDUCATION_TRENDABLE = -1 THEN 1 END))*100)/COUNT(*)) AS 'PctMiss',
                (((COUNT(*) - COUNT(MOTHER_EDUCATION_TRENDABLE))*100)/COUNT(*)) AS 'PctNull'


                from [GA_CMH].[dbo].[BIRTHS]

                GROUP BY [EVENT_YEAR]
                ORDER BY [EVENT_YEAR]")

Here is my output and desired format, however I would like to improve my PctMiss accuracy: 这是我的输出和所需的格式,但是我想提高PctMiss的准确性:

This is a known SQL Server situation where integer columns if used in expressions must be converted to decimals which you can do so implicitly by using at least one decimal value in your expression or explicitly by using CAST or CONVERT . 这是一种已知的SQL Server情况,其中必须将整数列(如果用于表达式中) 转换为小数 ,您可以通过在表达式中使用至少一个十进制值或使用CASTCONVERT显式地进行隐式CONVERT

For implicit conversion, multiply your COUNT() value by 100.00 (with 2 decimal values) or entire value with 1.00: 对于隐式转换,请将COUNT()值乘以100.00(带有2个十进制值)或将整个值乘以1.00:

PctNull <- sqlQuery(channel, 
                    "SELECT [EVENT_YEAR] AS 'YEAR', 
                            COUNT(*) AS 'TOTAL',
                            (((COUNT(CASE WHEN MOTHER_EDUCATION_TRENDABLE = -1 
                                          THEN 1 
                                     END)) * 100.00) / COUNT(*)) AS 'PctMiss',
                            (((COUNT(*) - COUNT(MOTHER_EDUCATION_TRENDABLE)) * 100.00) /  
                               COUNT(*)) AS 'PctNull'
                     FROM [GA_CMH].[dbo].[BIRTHS]
                     GROUP BY [EVENT_YEAR]
                     ORDER BY [EVENT_YEAR]")

For explicit conversion, specifically declare the type and precision using CAST : 对于显式转换,请使用CAST专门声明类型和精度:

(((CAST(COUNT(CASE WHEN MOTHER_EDUCATION_TRENDABLE = -1 
                   THEN 1 
              END)) * 100) AS DECIMAL(10,2)) / COUNT(*))

Or CONVERT : CONVERT

(((CONVERT(DECIMAL(10,2), COUNT(CASE WHEN MOTHER_EDUCATION_TRENDABLE = -1 
                                     THEN 1 
                                END)) * 100)) / COUNT(*))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM