Subsetsa table created with the BUILD_TABLE function.
Note that the entire second parameter is a string and must be enclosed in quotes. Also, note that the vertical bar (|) means "or" in this usage. For instance, use either "*" or "coli [alias] [, ..., coln [alias]]", but not both.
* An optional wildcard character that includes all columns from the original table in the resulting table.
Distinct A qualifier that removes duplicate rows from the resulting table.
coli The list of columns that you want to appear in the resulting table. Use the asterisk (*) wildcard character to select all columns in the original table. The col names can be arguments to the calculation functions used with the Group By clause.
alias Changes the input table's column name, coli, to a new name in the output table. If no alias is specified, the input table's column name is used in the resulting table.
Where cond A clause containing a conditional expression, cond, that is used to specify the rows to be placed in the resulting table. The expression can contain Boolean and/or relational operators.
Group By colgi A clause specifying one or more columns by which the rows are grouped in the resulting table. Normally, the grouped rows are data summaries containing results of calculation functions (Sum, Avg, etc.) applied to the columns. (Group By and Order By clauses are mutually exclusive: they cannot be used in the same function call.)
Order By coloi Name of the column(s) to be sorted (ordered) in the resulting table. The first column named is sorted first. The second column named is sorted within the primary column, and so on. (Group By and Order By clauses are mutually exclusive: they cannot be used in the same function call.)
direction Either Asc (the default) or Desc. Asc sorts the column in ascending order. Desc sorts the column in descending order. If neither are specified, the column is sorted in ascending order.
A table query always produces a new table containing the query results, or -1 if the query is empty.
Any string or numeric constant used in a QUERY_TABLE call can be passed into the function as a variable parameter. This means that you can use variables for numeric or string values in relational or Boolean expressions. For more information on passing parameters into QUERY_TABLE, see
NOTE: Within a QUERY_TABLE call, the Group By and Order By clauses are mutually exclusive. That is, you cannot place both Group By and Order By in the same QUERY_TABLE call.
You can also use relational operators (EQ, GE, GT, LE, LT, and NE) in a Where clause instead of the SQL-style operators listed above.
NOTE: When a literal string is used in a comparison, it must be enclosed in quotesa different set of quotes than those used to delimit the entire QUERY_TABLE parameter string. For more information on using strings in comparisons, see the PV-WAVE User's Guide.
The calculation functions used with Group By are the following, where col is the name of a column:
phone_data
contains information on company phone calls. This table contains eight columns of phone information: the date, time, duration of call, caller's initials, phone extension, cost of call, area code of call, and number of call.The table used in these examples and the data used to create it are available to you. Enter the following command at the
WAVE
prompt to restore the table and data:
RESTORE, !dir+'/data/phone_example.sav'
RESTORE, !dir+'[DATA]PHONE_EXAMPLE.SAV'
RESTORE, !dir+'\data\phone_example.sav'
For an example showing the use of the Distinct qualifier, see the PV-WAVE User's Guide.
The following examples show how to query this table in various ways using QUERY_TABLE.
This example demonstrates a simple table query that produces a three-column subset of the original table.
new_table = QUERY_TABLE(phone_data, $ 'EXT, AREA, NUMBER')
EXT | AREA | NUMBER |
---|---|---|
311 | 215 | 2155554242 |
358 | 303 | 5553869 |
320 | 214 | 2145559893 |
289 | 303 | 5555836 |
248 | 617 | 6175551999 |
TIP: For information on printing tables, see the PV-WAVE User's Guide.
This example demonstrates how a Where clause is used to produce a subset of the original table, where all rows that contain a cost value of less than one dollar are filtered out.
new_tbl = QUERY_TABLE(phone_data, $ '* Where COST 1.0')
DATE | TIME | DUR | INIT | EXT | COST | AREA | NUMBER |
---|---|---|---|---|---|---|---|
901002 | 093200 | 21.40 | TAC | 311 | 5.78 | 215 | 2155554242 |
901002 | 094700 | 17.44 | EBH | 320 | 4.71 | 214 | 2145559893 |
901004 | 095000 | 3.77 | DJC | 331 | 1.02 | 512 | 5125551228 |
This example demonstrates the use of the Group By clause. The column specified after Group By is the column by which the other specified columns are grouped. The calculation function Sum( ) is used to return the total cost and duration for each extension in the table.
The following command produces this result:
sum_table = QUERY_TABLE(phone_data, $ 'EXT, SUM(COST), SUM(DUR) Group By EXT')
sum_table
containing the columns EXT
, SUM_COST
, and SUM_DUR
:
EXT | SUM_COST | SUM_DUR |
---|---|---|
0 | 0.00000 | 4.49000 |
248 | 0.350000 | 1.31000 |
289 | 0.00000 | 16.2300 |
311 | 5.78000 | 21.4000 |
320 | 4.71000 | 17.4400 |
331 | 1.02000 | 3.77000 |
TIP: The cost and duration columns are named in the result table, by default, with the prefixSUM_
. This prevents any confusion with the existing table columns that are already namedCOST
andDUR
. You can change these default names by including aliases in the QUERY_TABLE function call.
INFO, /Structure, sum_table ** Structure TABLE_GB_2, 3 tags, 12 length:
EXT | LONG | 0 |
---|---|---|
SUM_COST | FLOAT | 0.000000 |
SUM_DUR | FLOAT | 4.49000 |
This example demonstrates a multiple Group By clause. For example, you can obtain a grouping by extension and by date. The result is a "grouping within a grouping".
The following command produces the desired result:
tbl = QUERY_TABLE(phone_data, $ 'EXT, DATE, Sum(DUR) Group By EXT, DATE')
370 | 901003 | 0.450000 |
0.450000
) of all calls made from extension 370
on date 901003
.
This example demonstrates the Group By clause used with the Count function.
cost_sum = QUERY_TABLE(phone_data, $ 'EXT, Count(NUMBER) Group By EXT')
EXT | COUNT_NUMBER |
0 | 3 |
248 | 1 |
289 | 1 |
311 | 1 |
320 | 1 |
331 | 1 |
332 | 1 |
358 | 1 |
370 | 2 |
379 | 2 |
418 | 1 |
cost_sum = QUERY_TABLE(phone_data, $ 'EXT, Count(DUR) Group By EXT')
This example demonstrates how the Order By clause is used to sort a column in a table.
ext_sort = QUERY_TABLE(phone_data, '* Order By EXT')
The table can be further refined by sorting the
COST
field as well.
cost_sort = QUERY_TABLE(phone_data, '* Order By EXT, COST DESC')
COST
column is now sorted in descending order within each group of extensions. The following illustrates the new table organization:
INIT
column of the phone_data
table:
nameset = ['TAC', 'KAR', 'OLL', 'ERD']
nameset
:
res = QUERY_TABLE(phone_data, ' * Where INIT In nameset')
For more information on QUERY_TABLE, see the PV-WAVE User's Guide.