sql - Compress rows with common id's to one row -


i have question have not found answer for. there similar questions solutions don't quite work in situation. have data set has 4 columns example:

name   session   sequence   page bob     001       001       home bob     001       002       news bob     001       003       contact_us bob     001       004       home sally   001       001       home sally   001       002       contact_us bob     002       001       home john    001       001       home john    001       002       about_us 

what this

name    session   pages bob     001       home-news-contact_us-home sally   001       home-contact_us bob     002       home john    001       home-about-us 

now trick sequence can 1:44, or anywhere in between. coding in r , have sqlite available. need concatenate in dashes, easy. if r had 'lag' in sas snap.

you have excellent answers, here dplyr 1 lends readability.

library(dplyr)  df %>%     group_by(name, session) %>% # create summary data each unique group     summarise(page = paste0(page, collapse = "-"))  

which gives

source: local data frame [4 x 3] groups: name     name session                      page 1   bob       1 home-news-contact_us-home 2   bob       2                      home 3  john       1             home-about_us 4 sally       1           home-contact_us 

rereading question seems sequence of pages important, i.e. page column have pages visited in sequence left right. therefore, include step.

library(dplyr)  df %>%     group_by(name, session) %>% # create summary data each unique group     arrange(sequence) %>% # makes sure sequence each group in ascending order.     summarise(page = paste0(page, collapse = "-"))  

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -