Download presentation
Presentation is loading. Please wait.
Published byTerence Kelley Modified over 8 years ago
1
Ingres 2006 release 2 ANSI date/time data types Derived Tables
2
ANSI Dates DATE can be Ingres or ANSI date ANSIDATE/INGRESDATE can be used New data types: ANSIDATE TIME TIMESTAMP INTERVAL
3
ANSI Date Date Types ansidate(expr) time(expr), time_wo_tz(expr), time_local(expr), time_with_tz(expr), timestamp(expr), timestamp_wo_tz(expr), timestamp_local(expr), timestamp_wo_tz(expr) year, quarter, month, week_iso, day, hour, minute, second, microsecond
4
Derived Tables In SQL-92, can specify a SELECT statement in the FROM clause as a table. Example: SELECT col_name FROM (SELECT column_name FROM iitables) AS col (col_name);
5
Derived Tables Select all numbers from 1 to 100: SELECT tens.nr * 10 + units.nr + 1 AS nr FROM ( SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ) as tens(nr), ( SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ) as units(nr);
6
Derived Tables Select the work day of the week and the total of dates for each work day of the week: SELECT week_day.dy, sum(case dates.dt is null then 0 else 1) FROM (SELECT 'Mon' UNION ALL SELECT 'Tue' UNION ALL SELECT 'Wed' UNION ALL SELECT 'Thu' UNION ALL SELECT 'Fri') as week_day(dy) LEFT OUTER JOIN dates ON dow(dates.dt) = week_day.dy GROUP BY week_day.dy;
7
Derived Tables Update a row with an aggregate function: UPDATE totals FROM (SELECT item, avg(cost) from transaction group by item) AS avg(item, average) SET average = avg.average WHERE totals.item = avg.item;
8
Derived Tables For each stock, provide the minimum, maximum, average and the variation from the average of each individual price: SELECT P.stock_id, S.min_price, S.max_price, S.avg_price, (P.price - S.avg_price) as fluctuation FROM Portfolio P, (SELECT stock_id, MIN(price), MAX(price), AVG(price) FROM portfolio GROUP BY stock_id ) AS S(stock_id, min_price, max_price, avg_price) WHERE P.stock_id = S.stock_id;
9
Derived Tables Or, using the new JOIN … USING syntax: SELECT P.stock_id, S.min_price, S.max_price, S.avg_price, (P.price - S.avg_price) as fluctuation FROM Portfolio P INNER JOIN (SELECT stock_id, MIN(price), MAX(price), AVG(price) FROM portfolio GROUP BY stock_id ) AS S(stock_id, min_price, max_price, avg_price) USING (stock_id);
10
Derived Tables Pair up males and females: CREATE TABLE People ( name VARCHAR(15) NOT NULL, gender CHAR(1) DEFAULT 'M' NOT NULL CHECK (gender IN ('M','F')) ); MODIFY People TO BTREE UNIQUE ON name; INSERT INTO People VALUES('John', 'M'); INSERT INTO People VALUES('Jim', 'M'); INSERT INTO People VALUES('Jill', 'F'); INSERT INTO People VALUES('Jane', 'F'); INSERT INTO People VALUES('Bill', 'M');
11
Derived Tables A self-join can define incrementing numbers for the unique key values: SELECT P1.name, rank = count(*) FROM People P1, People P2 WHERE P1.gender = 'M' AND P2.gender = 'M' AND P2.name <= P1.name GROUP BY P1.name;
12
Derived Tables Use derived tables to instantiate: SELECT M.name as male, F.name as female FROM (SELECT P1.name, count(*) FROM People P1, People P2 WHERE P1.gender = 'M' AND P2.gender = 'M' AND P2.name <= P1.name GROUP BY P1.name) AS M(name,rank), (SELECT P1.name, count(*) FROM People P1, People P2 WHERE P1.gender = 'F' AND P2.gender = 'F' AND P2.name <= P1.name GROUP BY P1.name) AS F(name,rank) WHERE M.rank = F.rank;
13
BEFORE triggers Called before UPDATE, INSERT, DELETE Can change values of columns in rows being inserted or updated, or can inhibit the deletion of rows. CREATE RULE emp_updates BEFORE UPDATE OF employee EXECUTE PROCEDURE check_emp_updates (name=new.name);
14
Global temp table indexes DECLARE GLOBAL TEMPORARY TABLE session.t( id integer4 not default not null, name varchar(20) not default not null ) ON COMMIT PRESERVE ROWS WITH NORECOVERY; INSERT INTO session.t VALUES(1,’Jim’); INSERT INTO session.t VALUES(2,’Joe’); MODIFY session.t TO BTREE UNIQUE ON id; CREATE INDEX t_idx1 ON session.t(name);
15
Sequence Defaults The default value for a column can be the next value in a sequence. CREATE SEQUENCE seq; CREATE TABLE lineitem ( itemid integer not null, itemseq integer with default next value for seq not null ); INSERT INTO lineitem(itemid) VALUES(4); INSERT INTO lineitem VALUES(8, next value for seq); INSERT INTO lineitem VALUES(9,22);
16
TCP/IPv6 Supports TCP/IP v6 out of the box
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.