database - Oracle SQL - map Table onto a "time raster" -


first of all: please excuse bad english. iam not native english speaker. ;) try best.

i have problem sql under oracle11.

this table data: (example)

  -------------------------------------------------------   | company  |      datetime       |  user   | actions  |   -------------------------------------------------------   |  1000    | 2015-09-10 00:00:00 |  usera   |   32     |   |  1000    | 2015-09-10 00:00:00 |  userb   |   12     |   |  1000    | 2015-09-10 00:00:00 |  userc   |   45     |    |  1001    | 2015-09-10 00:00:00 |  usera   |   64     |   |  1001    | 2015-09-10 00:00:00 |  userb   |   35     |   |  1001    | 2015-09-10 00:00:00 |  userc   |   78     |    |  1000    | 2015-09-10 00:06:00 |  usera   |   63     |   |  1000    | 2015-09-10 00:06:00 |  userc   |   24     |    |  1000    | 2015-09-10 00:12:00 |  usera   |   13     |   |  1000    | 2015-09-10 00:12:00 |  userb   |   33     |       |  1000    | 2015-09-10 00:12:00 |  userc   |   83     |    |  1000    | 2015-09-10 00:18:00 |  userc   |   35     |   |  1000    | 2015-09-10 00:18:00 |  userd   |   56     |   ------------------------------------------------------- 

you see, there different times user , actions.

i want make query, giving me record "every user , time"... want map more or less "timeraster".. build datetime column table. user no entry @ specific time..

i.e. company "1000"

  -------------------------------------------------------   | company  |      datetime       |  user   | actions  |   -------------------------------------------------------   |  1000    | 2015-09-10 00:00:00 |  usera   |   32     |   |  1000    | 2015-09-10 00:00:00 |  userb   |   12     |   |  1000    | 2015-09-10 00:00:00 |  userc   |   45     |   |  1000    | 2015-09-10 00:00:00 |  userd   |   0      |    |  1000    | 2015-09-10 00:06:00 |  usera   |   63     |   |  1000    | 2015-09-10 00:06:00 |  userb   |   0      |   |  1000    | 2015-09-10 00:06:00 |  userc   |   24     |   |  1000    | 2015-09-10 00:06:00 |  userd   |   0      |    |  1000    | 2015-09-10 00:12:00 |  usera   |   0      |   |  1000    | 2015-09-10 00:12:00 |  userb   |   0      |   |  1000    | 2015-09-10 00:12:00 |  userc   |   83     |   |  1000    | 2015-09-10 00:12:00 |  userd   |   0      |   ------------------------------------------------------- 

the amount of users differs companys... (mainly between 4 , 25)..

i tried allready pivot tables. think there should easier way achive goal?

partitioned outer join rescue!

with sample_data (select 1000 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'usera' usr, 32 actions dual union                      select 1000 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userb' usr, 22 actions dual union                      select 1000 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userc' usr, 45 actions dual union                      select 1001 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'usera' usr, 64 actions dual union                      select 1001 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userb' usr, 35 actions dual union                      select 1001 company, to_date('10/09/2015 00:00:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userc' usr, 78 actions dual union                      select 1000 company, to_date('10/09/2015 00:06:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'usera' usr, 63 actions dual union                      select 1000 company, to_date('10/09/2015 00:06:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userc' usr, 24 actions dual union                      select 1000 company, to_date('10/09/2015 00:12:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'usera' usr, 13 actions dual union                      select 1000 company, to_date('10/09/2015 00:12:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userb' usr, 33 actions dual union                      select 1000 company, to_date('10/09/2015 00:12:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userc' usr, 83 actions dual union                      select 1000 company, to_date('10/09/2015 00:18:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userc' usr, 35 actions dual union                      select 1000 company, to_date('10/09/2015 00:18:00', 'dd/mm/yyyy hh24:mi:ss') datetime, 'userd' usr, 56 actions dual),            dates (select distinct company, datetime sample_data) select sd.company,        dts.datetime,        sd.usr,        nvl(sd.actions, 0) actions   dates dts        left outer join sample_data sd partition (sd.usr, sd.company) on (dts.datetime = sd.datetime)  dts.company = sd.company order 1, 2, 3;     company datetime              usr      actions ---------- --------------------- ----- ----------       1000 10/09/2015 00:00:00   usera         32       1000 10/09/2015 00:00:00   userb         22       1000 10/09/2015 00:00:00   userc         45       1000 10/09/2015 00:00:00   userd          0       1000 10/09/2015 00:06:00   usera         63       1000 10/09/2015 00:06:00   userb          0       1000 10/09/2015 00:06:00   userc         24       1000 10/09/2015 00:06:00   userd          0       1000 10/09/2015 00:12:00   usera         13       1000 10/09/2015 00:12:00   userb         33       1000 10/09/2015 00:12:00   userc         83       1000 10/09/2015 00:12:00   userd          0       1000 10/09/2015 00:18:00   usera          0       1000 10/09/2015 00:18:00   userb          0       1000 10/09/2015 00:18:00   userc         35       1000 10/09/2015 00:18:00   userd         56       1001 10/09/2015 00:00:00   usera         64       1001 10/09/2015 00:00:00   userb         35       1001 10/09/2015 00:00:00   userc         78 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -