Sunteți pe pagina 1din 8

sessionInfo()

getwd()

install.packages('stringr')
library(stringr)

install.packages("sqldf")
library(sqldf)

install.packages("lubridate")
library(lubridate)

setwd("C:\\Users\\SUZI\\Desktop\\tema3")

#################################################
################################
### 9. Import data from PostgreSQL databases
###
#################################################
################################

# this section commands work only if you previously created and populated the
# databases on the PostgreSQL server installed on your system
# (of course connection data could vary in case of remote database servers
# or other databases, tables and attributes)

install.packages('RPostgreSQL')
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")

## On Windows systems, PostgreSQL database service must already be started


con <- dbConnect(drv, dbname="postgres", user="postgres",
password="suzana")

### E. Import (as a data frame) each PostgreSQL database table with a piece
### of code
alcatuire_disc<- dbReadTable(con,"alcatuire_disc")
discografiec<- dbReadTable(con,"discografie")
formatii<- dbReadTable(con,"formatii")
muzicieni<- dbReadTable(con,"muzicieni")
realizare_disc<- dbReadTable(con,"realizare_disc")
vanzari_discuri<- dbReadTable(con,"vanzari_discuri")

install.packages("dplyr")
library(dplyr)

install.packages("tidyr")
library(tidyr)

#1 Care sunt formaiile care au lansat discuri n anul 1963?

interogare_1<-formatii %>%
inner_join (discografiec) %>%
filter(anlansare==1963) %>%
select (idform, denformatie)
#2 Pe ce discuri ale cror formaii apare piesa Knocking on heaven's door('Two of
us') ?

interogare_2<-discografiec%>%
inner_join(formatii) %>%
inner_join(alcatuire_disc)%>%
filter(piesa=='Two of us')%>%
select (titludisc, denformatie)

# 3 Care este formaia n care au activat, pn n prezent, cei mai muli muzicieni
?

interogare_3<- muzicieni%>%
inner_join(realizare_disc)%>%
inner_join(discografiec)%>%
inner_join(formatii)%>%
group_by(denformatie) %>%
summarise(nrmuzicieni=n()) %>%
filter(nrmuzicieni>=max(nrmuzicieni))

#4 Care este cel mai bine vndut disc al formaiei Coldplay(Beatles) n anul
2009(1970)?

interogare_4<- discografiec %>%


inner_join(formatii)%>%
inner_join(vanzari_discuri)%>%
filter(denformatie=='Beatles' & an==1970) %>%
group_by(an)%>%
summarise(incasari_totale=sum(incas_usd))%>%
filter(incasari_totale>=max(incasari_totale))
# 5 Care sunt discurile care s-au vndut i n 1989(1970) i n 2009(1971) ?

interogare_5<- vanzari_discuri %>%


inner_join(discografiec) %>%
filter(an==1970)%>%
select(iddisc,titludisc)%>%
semi_join(vanzari_discuri %>%
inner_join(discografiec) %>%
filter(an==1971)%>%
select(iddisc,an,titludisc))

#semi_join afiseaza doar randurile din x care se gasesc si in y,pastrand doar


coloanele din x
#(daca am fi pus si anul, ar fi diferit datele(1970 vs 1971)si nu s-ar gasi
inregistrari disponibile)

# 6 Care sunt discurile formaiei Yes(beatles) la care a participat Jon


Anderson(John Lennon) fr Rick Wakeman(Paul McCarney)

interogare_6<- realizare_disc%>%
inner_join(discografiec)%>%
inner_join(muzicieni) %>%
filter(numemuz=='John Lennon')%>%
select(iddisc, titludisc) %>%
anti_join(realizare_disc%>%
inner_join(discografiec)%>%
inner_join(muzicieni) %>%
filter(numemuz=='Paul McCarney')%>%
select(iddisc, titludisc))

#7 Care este cea mai lung pies de pe albumul Abbey Road(Let It Be) al
formaiei Beatles?

interogare_7<-discografiec%>%
inner_join(alcatuire_disc)%>%
inner_join(formatii)%>%
filter(titludisc=='Let It Be' & denformatie=='Beatles')%>%
summarise(durata_max=max(duratasec))

#8 Care sunt discurile care au avut vnzri n toate lunile (de pn la data
curent) ale anului curent?

interogare_8<- discografiec%>%
inner_join(vanzari_discuri)%>%
filter(an>=1960)%>%
arrange(desc(an, luna))%>%
select(titludisc, an, luna, incas_usd)%>%
spread (luna, incas_usd, fill = 0)

# 9 Afisati top 10 discuri , in functie de valoarea incasarilor

interogare_9 <- discografiec%>%


inner_join(vanzari_discuri)%>%
group_by(iddisc)%>%
summarise(incasari_totale=sum(incas_usd))%>%
arrange (desc(incasari_totale))%>%
top_n(10, incasari_totale)

# daca punem intai summarise si dupa group by, nu vom obtine rezultatul
dorit(pe ani), ci obtinem totalul incasarilor
#pe coloana incas_usd

# 10 Ce formaii au avut cel puin membrii formaiei Smile(Yes) ?

interogare_zece <- formatii%>%


inner_join(muzicieni)%>%
inner_join(discografiec)%>%
inner_join(realizare_disc)%>%
group_by(denformatie)%>%
select( denformatie,numemuz) %>%
semi_join( formatii%>%
inner_join(muzicieni)%>%
inner_join(discografiec)%>%
inner_join(realizare_disc)%>%
filter(denformatie=='Yes')%>%
group_by(denformatie)%>%
select(denformatie,numemuz) )

#11 S se obin primul raport de pe verso (vnzri pe formaii i albume, cu


subtotaluri)

interogare_zzunsprez <- bind_cols(


formatii%>%
inner_join(discografiec)%>%
inner_join(vanzari_discuri)%>%
group_by(denformatie)%>%
select(denformatie, titludisc, anlansare, incas_usd)
)

#12.
interogare_zzzzz<- bind_rows(
bind_rows(
formatii%>%
inner_join(discografiec)%>%
inner_join(vanzari_discuri)%>%
filter(anlansare==an)%>%
group_by(denformatie, titludisc, anlansare) %>%
summarise(incasari= sum(incas_usd)),
formatii%>%
inner_join(discografiec)%>%
inner_join(vanzari_discuri)%>%
filter(anlansare==an)%>%
group_by(denformatie, anlansare,titludisc) %>%
summarise(incasari= sum(incas_usd)) %>%

mutate (titludisc= 'subtotal'))%>%


arrange (denformatie, titludisc, anlansare),
formatii%>%
inner_join(discografiec)%>%
inner_join(vanzari_discuri)%>%
filter(anlansare==an)%>%
summarise(incasari= sum(incas_usd))%>%
mutate (denformatie = 'TOTAL'))