Top, Bottom, Nth Record and Random Record Queries

Description

When Alpha Anywhere performs a query, sometimes you are only interested in the top or bottom x records in query. For example you may have a query that selects all donors who have given more than $1,000, sorted by size of donation. Perhaps you only want to see the 10 biggest donors. Alpha Anywhere has several functions designed to extract the top or bottom records from a query. It also has a function designed to extract every Nth record from a query. This is useful in certain mailing list applications where you want to mail to a subset of your list. Finally, Alpha Anywhere has functions that allow you to select records at random from a table.

  •  BOTTOM_PERCENT_RECORDS()

    Returns the bottom Percent of records in a table for a given sort order.

  •  BOTTOM_RECORDS()

    Selects the bottom X% of the records in a table, for a given sort order.

  •  RANDOM_PERCENT_RECORDS()

    Selects X% of the records in a table at random. If the optional filter is supplied, the records are selected only from those that satisfy the filter.

  •  RANDOM_RECORDS()

    Selects count records at random from a table. If the optional filter is supplied, the records are selected only from those that satisfy the filter.

  •  SAMPLE_RECORDS()

    Selects every Nth record in a table, for a given sort order.

  •  TOP_PERCENT_RECORDS()

    Selects the top X% of the records in a table, for a given sort order. (Percent is expressed as a whole number, e.g. 50 for 50%).

  •  TOP_RECORDS()

    Selects the top count records in a table, for a given sort order. If the optional filter is supplied, only records that satisfy the filter are returned. The table's sort order is determined by the query's Order parameter.

 Examples

All of the following examples are assumed to be scripts that are attached to buttons on a form. The following query returns the 100 biggest donors.
t = table.current()
query.filter = "top_records(100)"
query.order = "donation_size"
i = t.query_create()
topparent.resynch()'synchronize the form to show the query

The following query returns the bottom 10 orders placed after May 1, 2015.

t = table.current()
query.filter = "bottom_records(10, \"orderdate > {5/1/2015}\")"
query.order = "order_total"
i = t.query_create()
topparent.resynch()

The following query returns the top 10% of students in a school

t = table.current()
query.filter = "top_precent_records(10)"
query.order = "grade_point_average"
i = t.query_create()
topparent.resynch()

The following query runs a saved query, but only shows the top 10 records selected by the query. The saved query is called "Overdue_Accounts".

t = table.current()
query.filter = "top_records(10," + query_filter_get("overdue_accounts") + ")"
query.order = ""
i = t.query_create()
topparent.resynch()

The following query selects 10% of the records in table, for state of MA.

t = table.current()
query.filter = "random_percent_records(10,\"state = 'ma' \")"
query.order = "rand()*100000"
i = t.query_create()
topparent.resynch()