Examples Of Sequence By - HP Neoview SQL Reference Manual

Hide thumbs Also See for Neoview SQL:
Table of Contents

Advertisement

SEQUENCE BY ordernum;
Like aggregate functions, sequence functions generate an intermediate result. If the query
has a WHERE clause, its search condition is applied during the generation of the intermediate
result. Therefore, you cannot use sequence functions in the WHERE clause of a SELECT
statement.
— This query returns an error:
SELECT ordernum, partnum, RUNNINGAVG(unit_price)
FROM odetail
WHERE ordernum > 800000 AND RUNNINGAVG(unit_price) > 350
SEQUENCE BY qty_ordered;
— Apply a search condition to the result of a sequence function, use a derived table for
the SEQUENCE BY query, and use the derived column in the WHERE clause:
SELECT ordernum, partnum, runavg_price
FROM
(SELECT ordernum, partnum, RUNNINGAVG(unit_price)
WHERE ordernum > 800000 AND runavg_price > 350;

Examples of SEQUENCE BY

Sequentially number each row for the entire result and also number the rows for each part
number:
SELECT RUNNINGCOUNT(*) AS RCOUNT, MOVINGCOUNT(*,
ROWS SINCE (d.partnum<>THIS(d.partnum)))
AS MCOUNT,
d.partnum
FROM orders o, odetail d
WHERE o.ordernum=d.ordernum
SEQUENCE BY d.partnum, o.order_date, o.ordernum
ORDER BY d.partnum, o.order_date, o.ordernum;
RCOUNT
--------------------
--- 70 row(s) selected.
Show the orders for each date, the amount for each order item and the moving total for each
order, and the running total of all the orders. The query sequences orders by date, order
number, and part number. (The CAST function is used for readability only.)
SELECT o.ordernum,
CAST (MOVINGCOUNT(*,ROWS SINCE(THIS(o.ordernum) <>
o.ordernum)) AS INT) AS MCOUNT,
d.partnum,
(d.unit_price * d.qty_ordered) AS AMOUNT,
MOVINGSUM (d.unit_price * d.qty_ordered,
ROWS SINCE(THIS(o.ordernum)<>o.ordernum)) AS ORDER_TOTAL,
RUNNINGSUM (d.unit_price * d.qty_ordered) AS TOTAL_SALES
AS tab2 (ordernum, avg_price)
FROM odetail
SEQUENCE BY qty_ordered)
AS tab2 (ordernum, partnum, runavg_price)
MCOUNT
---------------------
1
2
3
4
5
...
67
68
69
70
o.order_date,
Part/Num
--------
1
212
2
212
1
244
2
244
3
244
...
...
1
7301
2
7301
3
7301
4
7301
SEQUENCE BY Clause
269

Advertisement

Table of Contents
loading

Table of Contents