Sunteți pe pagina 1din 3

install.

packages('RPostgreSQL')
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
#10.10.0.17
con <- dbConnect(drv, host="", port="5432", dbname="postgres", user="postgres",
password="123456")
tables <- dbGetQuery(con, "select table_name from information_schema.tables wher
e table_schema = 'public'")
tables
formatii <- dbReadTable(con, "formatii" )
str(formatii)
head(formatii)
discografie <- dbReadTable(con, "discografie" )
str(discografie)
head(discografie)
alcatuire_disc <- dbReadTable(con, "alcatuire_disc" )
str(alcatuire_disc)
head(alcatuire_disc)
muzicieni <- dbReadTable(con, "muzicieni" )
str(muzicieni)
head(muzicieni)

realizare_disc <- dbReadTable(con, "realizare_disc" )


str(realizare_disc)
head(realizare_disc)
vanzari_discuri <- dbReadTable(con, "vanzari_discuri" )
str(vanzari_discuri)
head(vanzari_discuri)

#Se pot copia cu acest script direct din postgress


invoice_detailed <- dbGetQuery(con,
" SELECT distinct DenFormatie from FORMATII INNER
JOIN DISCOGRAFIE on formatii.idform = discografie.idform
INNER JOIN REALIZARE_DISC ON discografie.iddisc =
realizare_disc.iddisc
INNER JOIN MUZICIENI ON realizare_disc.idmuz = mu
zicieni.idmuz
WHERE NumeMuz = 'Eric Clapton';")
head(invoice_detailed)
# display first rows of the result
head(invoice_detailed,5)

install.packages("dplyr")
library(dplyr)
#WHERE
filter(formatii,tara=="Anglia")
#ORDER BY desc
arrange(formatii,desc(tara))
#SELECT+WHERE
temp<-select(formatii,idform)
filter(temp,temp>102)
#teste cu toate select in select
alcatuire_disc%>%
filter(duratasec>200)%>%
arrange(nrordine)%>%
group_by(iddisc)%>%
sum(duratasec)%>%
select (iddisc:duratasec)
1.eu
inner1<-inner_join(formatii,discografie)
inner2<-inner_join(inner1,realizare_disc)
inner3<-inner_join(inner2,muzicieni,by="idmuz")
filter4<-filter(inner3,numemuz=="Eric Clapton")
distinct (filter4,denformatie)
2.eu
inner1<-inner_join(realizare_disc,muzicieni)
filter2<-filter(inner1,numemuz=="Steve Howe")
distinct(filter2)->dist1
summarise(dist1,iddisc=n())

3.eu
inner1<-inner_join(formatii,discografie,by="idform")
inner2<-inner_join(inner1,vanzari_discuri,by="iddisc")
group_by(inner2,denformatie)->grp1
aggregate(inner2$incas_usd,by=list(denformatie=inner2$denformatie),FUN=sum)->agg
1
head(agg1,1)
4.
inner1<-inner_join(alcatuire_disc,discografie,by="iddisc")
inner2<-inner_join(inner1,formatii,by="idform")
filter1<-filter(inner2,denformatie=="Beatles" & titludisc=="Let It Be")
aggregate(filter1$duratasec,by=list(denformatie=filter1$denformatie),FUN=sum)>ag
g1
x<-select(agg1,2)
x
8.eu
inner1<-inner_join(vanzari_discuri,discografie,by="iddisc")
inner2<-inner_join(inner1,formatii, by="idform")
filter(inner2,an=="2015")->fil1
group_by(fil1,denformatie)
distinct(grp1,tara.x)->dis1
count(dis1)->c1

distinct(vanzari_discuri,tara)->dis2
count(dis2)->c2
filter(c1,n==c2)

9.eu
count(discografie,anlansare,iddisc=n())->c1
arrange(c1,desc(n))->arr1
select(arr1,3)->sel1
head(sel1,3)
12.eu
inner1<-inner_join(formatii,discografie,by="idform")
inner2<-inner_join(inner1,realizare_disc,by="iddisc")
inner3<-inner_join(inner2,muzicieni,by="idmuz")
group_by(inner3,denformatie)->gpr1
distinct(gpr1,numemuz)->dist1
dist1

ne<-within(dist1,{
Emp_list<-ave(numemuz,denformatie,FUN=function(x)paste(x,collapse = " ;"))
})
select(ne,-1)->sel1
distinct(sel1,denformatie,Emp_list)