0

how can i join these two query together ... i mean get the CTR for certain amount of time...i have tried different way to solve it but couldn't find any way to write the query in rethinknDB...

**r.expr({total_page_position:r.table('test_pagol')('position').sum(), total_page_load: r.table('test_pagol')('page').sum()}).merge({CRT: r.row('total_page_load').div(r.row('total_page_position'))**


**r.table("test_pagol").filter(
    (r.row["timestamp"] >= 1429617902988)
    & (r.row["timestamp"] >= 1429617922119))**
Istiak Mahmood
  • 2,330
  • 8
  • 31
  • 73

1 Answers1

0

you can use the map and reduce command for that:

r.table("test_pagol").filter(
    r.row("timestamp").ge(1429617902988)
    .and(r.row("timestamp").le(1429617922119))
).map({
    'total_page_position': r.row('position'),
    'total_page_load': r.row('page')
}).reduce(function(left, right) {
    return {
        'total_page_position': left('total_page_position').add(right('total_page_position')),
        'total_page_load': left('total_page_load').add(right('total_page_load'))
    }
}).merge({
    'CRT': r.row('total_page_load').div(r.row('total_page_position'))
})

More information on map: http://www.rethinkdb.com/api/javascript/map/ More information on reduce: http://www.rethinkdb.com/api/javascript/reduce/

(an unrelated note: if you want to make this query faster, you could create an index on the timestamp field and replace the filter by between(1429617902988, 1429617922119, {index: "timestamp", rightBound="closed"}), see http://www.rethinkdb.com/api/javascript/between/ )

Daniel Mewes
  • 1,876
  • 14
  • 16
  • i am using javascript and it show "TypeError: Cannot read property 'position' of undefined" – Istiak Mahmood Apr 29 '15 at 17:06
  • I changed the code to JavaScript. (In JS you need to use parentheses instead of square brackets for accessing a field of a document and instead of <= you have to use le and instead of >= ge) – Daniel Mewes Apr 29 '15 at 17:43
  • it show now ... Server error: RqlRuntimeError: Expected type FUNCTION but found DATUM: { "total_page_load": 5872, "total_page_position": 20329 } in: .............. – Istiak Mahmood Apr 29 '15 at 17:50
  • Ah sorry, my query was actually wrong because I didn't use the documents from the filter in the `map`. I changed it to a map-reduce query, that computes both sums in one pass by using a reduction function. Hope it works now. – Daniel Mewes Apr 29 '15 at 18:06