R语言中运行SQL语句

使用sqldf包练习sql语句
R语言
数据库
SQL
作者

shalom

发布日期

2024年4月12日

library(sqldf)
mtcars
sqldf('select cyl,mpg,sum(mpg) over(partition by cyl) from mtcars')
sqldf('select mpg,sum(mpg) over (order by rowid) from mtcars')
sqldf('select cyl,mpg,sum(mpg) over (partition by cyl order by rowid) from mtcars')
sqldf('select cyl,vs,min(mpg) over (partition by cyl, vs) as min from mtcars')
sqldf('select cyl,gear,
      rank() over (partition by cyl order by gear) as rank,
      dense_rank() over (partition by cyl order by gear) as dense_rank,
      row_number() over (partition by cyl order by gear) as row_number
      from mtcars')
sqldf('select mpg,sum(mpg) over (order by mpg) sum, count(mpg) over (order by mpg) count ,avg(mpg) over (order by mpg) avg from mtcars')
sqldf('select mpg,rowid as rn from mtcars')
sqldf('select mpg,sum(mpg) over (order by rowid) as rn from mtcars')
sqldf('select cyl,mpg,rowid as rn from mtcars where rn between 1 and 5 order by rowid desc')
sqldf('select hp from mtcars where hp like "%0"')
sqldf('select distinct cyl from mtcars')
sqldf('select * from mtcars limit 1,2' )
sqldf('select mpg MPG,cyl as CYL from mtcars')
sqldf('select avg(mpg),cyl from mtcars a where hp>100 group by cyl')
sqldf('select avg(mpg),cyl from mtcars a where hp>100 group by cyl having cyl>4')