Class: ConceptQL::Nodes::Occurrence

Inherits:
Node
  • Object
show all
Defined in:
lib/conceptql/nodes/occurrence.rb

Overview

Represents a node that will grab the Nth occurrence of something

Specify occurrences as integers, excluding O 1 => first 2 => second … -1 => last -2 => second-to-last

The node treats all streams as a single, large stream. It partitions that larget stream by person_id, then sorts within those groupings by start_date and then select at most one row per person, regardless of how many different types of streams enter the node

If two rows have the same start_date, the order of their ranking is arbitrary

If we ask for the second occurrence of something and a person has only one occurrence, this node returns nothing for that person

Direct Known Subclasses

First, Last

Constant Summary

Constants inherited from Node

Node::KNOWN_TYPES

Instance Attribute Summary

Attributes inherited from Node

#values

Instance Method Summary collapse

Methods inherited from Node

#arguments, #children, #columns, #date_columns, #evaluate, #initialize, #select_it, #stream, #types

Constructor Details

This class inherits a constructor from ConceptQL::Nodes::Node

Instance Method Details

#occurrenceObject



31
32
33
# File 'lib/conceptql/nodes/occurrence.rb', line 31

def occurrence
  @occurrence ||= arguments.first
end

#query(db) ⇒ Object



25
26
27
28
29
# File 'lib/conceptql/nodes/occurrence.rb', line 25

def query(db)
  db.from(stream.evaluate(db)
    .select_append { |o| o.row_number(:over, partition: :person_id, order: ordered_columns){}.as(:rn) })
    .where(rn: occurrence.abs)
end