sql - Select Top User over a list of Pages -


i have table containing records of users' internet history. table's structure contains user_id, page accessed, , date accessed of page. example:

+==========================================+  |user_id  | page_accessed | date_accessed  | +==========================================+ |johh.doe | google        |    1/1/2015    | |johh.doe | google        |    1/1/2015    | |suzy.lue | google        |    7/11/2015   |  |suzy.lue | wikipedia     |    4/23/2015   | |babe ruth| stackoverflow |    9/1/2015    | +==========================================+  

i trying use sql query uses:

rank() on (partition [page accessed] order count(dateacc))

then use pivot() various sites. after selecting records where (num = 1) pivot() , group [rank], i'm ending resulting query similar to:

+=================================================+  |rank     | google  | wikipedia  |  stackoverflow | +=================================================+ |   1     | john doe|    null    |     null       | |   1     |   null  |  suzy lue  |     null       | |   1     |   null  |    null    |     babe ruth  |  +=================================================+  

instead need reformat output as:

+=================================================+  |rank     | google  | wikipedia  |  stackoverflow | +=================================================+ |   1     | john doe| suzy lue   |   babe ruth    | +=================================================+  

my current query:

select rank, google, wikipedia, stackoverflow     from(         select top (100) percent user_id, page_accessed, count(date_accessed) views,          rank() on (partition page_accessed order count(date_accessed) desc) rank            record_table         group dbo.location_key.subsite, dbo.user_info_list_parse.name     order views desc) tb       pivot (     max(tb.user_id)      page_accessed in ( google, wikipedia, stackoverflow)     ) pvt  (num = 1) 

are there creative solutions obtain result?

i think you've found solution information , others reading - let me erase noise in query. there no need order by, no need apply top (100) percent, views column redundant. simplify query follows:

create table internethistory (     [user_id] varchar(20),     [page_accessed] varchar(20),     [date_accessed] datetime ) insert internethistory values ('johh.doe', 'google', '2015-01-01'), ('johh.doe', 'google', '2015-01-01'), ('suzy.lue', 'google', '2015-07-11'), ('suzy.lue', 'wikipedia', '2015-04-23'), ('babe ruth', 'stackoverflow', '2015-01-09')  select * (     select [user_id], [page_accessed], rank() on (partition [page_accessed] order count(*) desc) ranking     internethistory     group [user_id], [page_accessed] ) src pivot (     max([user_id]) [page_accessed] in ([google], [wikipedia], [stackoverflow]) ) pvt ranking = 1 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -