2013-08-17 106 views
1

我已經寫了下面的代碼,我想將這些表連接到一個大表中;那麼如何使用SQL做R中如何在r中使用sql將兩個或多個表合併爲一個

user_lessthan10per <- sqldf("select count(uid) as count_of_students 
         from adopted_user_point 
         where points_scored between 0 and (1469*0.1)") 

接下來是

user_lessthan20per <- sqldf("select count(uid) as count_of_students 
         from adopted_user_point 
         where points_scored >(1469*0.1) and points_scored <= (1469*0.2)") 

user_lessthan30per <- sqldf("select count(uid) as count_of_students 
         from adopted_user_point 
         where points_scored >(1469*0.2) and points_scored <= (1469*0.3)") 

現在我想將它加入到含有這些三個表count_of_students列一個表。

如何在R我有UNION命令,但它顯示錯誤。

回答

2

您可以使用條件聚合。這將返回一個排,三列:

select sum(case when points_scored between 0 and (1469*0.1) then 1 else 0 
      end) as cnt1, 
     sum(case when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 1 else 0 
      end) as cnt2, 
     sum(case when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 1 else 0 
      end) as cnt3 
from adopted_user_point; 

如果你想三人行,你可以使用聚合與group by

select (case when points_scored between 0 and (1469*0.1) then 'Group1' 
      when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 'Group2' 
      when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 'Group3' 
      else 'Other' 
     end) as cnt3, count(*) as count_of_students 
from adopted_user_point 
group by (case when points_scored between 0 and (1469*0.1) then 'Group1' 
       when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 'Group2' 
       when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 'Group3' 
       else 'Other' 
      end); 
0

我已經命名了不同的原始選擇,erhaps「u_0_10」 u_10_20' ,‘u_20_30’明確指出,‘user_less than30per’真的‘user_btwn20_30’,但現在他們都是R dataframes在全球環境中,你並不真的需要sdldf把它們放在一起:

user_under30per <- rbind(user_lessthan10per. 
         user_lessthan20per, 
         user_lessthan30per) 

sqldf函數確實提供UNION:

one_and_two <- sqldf("select * from lessthan10per union all 
             select * from lessthan20per") 
all_three <- sqldf("select * from one_and_two union all 
             select * from lessthan30per")