Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reports Using SQL Script Please check speaker notes for additional information!

Similar presentations


Presentation on theme: "Reports Using SQL Script Please check speaker notes for additional information!"— Presentation transcript:

1 Reports Using SQL Script Please check speaker notes for additional information!

2 Report script SQL> edit rptslide1 This will bring up the editor (Notepad) so I can create a new script or edit an existing script with the name rptslide1. These are the column/field names that I want to appear in the report. These are the column headings. The bar between the words causes a break so the header will appear on two lines. In the notes, I repeatedly have the message Input truncated to 1 characters (sometimes a different number). The solution is to press ENTER after the / so you have a blank line. That avoids the message.

3 SQL> @ rptslide1 Empl Employee Start Id# Name Jo Date Salary Bonus ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. Reports CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' COLUMN name HEADING 'Employee|Name' COLUMN jobcode HEADING 'Job Code' COLUMN startdate HEADING 'Start|Date' COLUMN salary HEADING 'Salary' COLUMN bonus HEADING 'Bonus' / COLUMN is followed by the name of the column/field from the table that is going to be used. HEADING gives the column heading. Note that the bar between words in heading separates the words onto two lines. @ means to execute - the code is named rptslide1

4 SQL> SELECT * FROM donor; Employee IDNO Name STADR CITY ST ZIP DATEFST YRGOAL CONTACT ----- --------------- --------------- ---------- -- ----- --------- --------- ------------ 11111 Stephen Daniels 123 Elm St Seekonk MA 02345 03-JUL-98 500 John Smith 12121 Jennifer Ames 24 Benefit St Providence RI 02045 24-MAY-97 400 Susan Jones 22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones 23456 Susan Ash 21 Main St Fall River MA 02720 04-MAR-92 100 Amy Costa 33333 Nancy Taylor 26 Oak St Fall River MA 02720 04-MAR-92 50 John Adams 34567 Robert Brooks 36 Pine St Fall River MA 02720 04-APR-98 50 Amy Costa 6 rows selected. SQL> SELECT * FROM first_pay; Empl Employee Start Id# Name Jo Date Salary Bonus ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. Reports Once the report has been executed, whatever table is used in the a SELECT will try to fit the table layout.

5 SQL> clear columns columns cleared SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. Reports Columns have been cleared so the column settings in the script are not used.

6 Reports CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' COLUMN name HEADING 'Employee|Name' COLUMN jobcode HEADING 'Job Code' COLUMN startdate HEADING 'Start|Date' COLUMN salary HEADING 'Salary' COLUMN bonus HEADING 'Bonus' SELECT * FROM first_pay / SQL> edit rptslide2 This is rptslide2 as it appears in the editor (Notepad). SQL> @ rptslide2 Empl Employee Start Id# Name Jo Date Salary Bonus ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. @ rptslide2 runs the script which includes the SELECT so the output is produced.

7 Report SQL> @ rptslide3 Thu Jun 22 page 1 List of Employees Empl Employee Start Id# Name Jo Date Salary Bonus ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' COLUMN name HEADING 'Employee|Name' COLUMN jobcode HEADING 'Job Code' COLUMN startdate HEADING 'Start|Date' COLUMN salary HEADING 'Salary' COLUMN bonus HEADING 'Bonus' SET LINESIZE 65 TTITLE 'List of Employees' SELECT * FROM first_pay / SQL> edit rptslide3 Rptslide3 as it came up in the editor when I keyed in edit rptslide3 at the SQL> Report that was generated by @rptslide3 SET LINESIZE can be used to establish the length of the line. TTITLE establishes a title for the report.

8 Report Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 1111 Linda Costa CI 15-JAN-97 $45,000.00 $1,000.00 2222 John Davidson IN 25-SEP-92 $40,000.00 $1,500.00 3333 Susan Ash AP 05-FEB-00 $25,000.00 $500.00 4444 Stephen York CM 03-JUL-97 $42,000.00 $2,000.00 5555 Richard Jones CI 30-OCT-92 $50,000.00 $2,000.00 6666 Joanne Brown IN 18-AUG-94 $48,000.00 $2,000.00 7777 Donald Brown CI 05-NOV-99 $45,000.00 8888 Paula Adams IN 12-DEC-98 $45,000.00 $2,000.00 8 rows selected. SQL> edit rptslide4 SQL> @ rptslide4 CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' SELECT * FROM first_pay / FORMAT is used to establish the length of the filed or to format using special characters in the numeric fields shown.

9 Reports Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 3333 Susan Ash AP 05-FEB-00 $25,000.00 $500.00 1111 Linda Costa CI 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown 05-NOV-99 $45,000.00 5555 Richard Jones 30-OCT-92 $50,000.00 $2,000.00 4444 Stephen York CM 03-JUL-97 $42,000.00 $2,000.00 2222 John Davidson IN 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown 18-AUG-94 $48,000.00 $2,000.00 8 rows selected. CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON jobcode SELECT * FROM first_pay ORDER BY jobcode / SQL> edit rptslide5 SQL> @ rptslide5 The table has to be ordered by jobcode if you want to break on jobcode.

10 CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON jobcode SKIP 2 COMPUTE SUM OF salary ON jobcode COMPUTE SUM OF bonus ON jobcode SELECT * FROM first_pay ORDER BY jobcode / I have now added the COMPUTE statement which will compute the SUM of salary and the SUM of bonus based on the jobcode break. Reports

11 Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 3333 Susan Ash AP 05-FEB-00 $25,000.00 $500.00 ***** ------------ ----------- sum $25,000.00 $500.00 1111 Linda Costa CI 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown 05-NOV-99 $45,000.00 5555 Richard Jones 30-OCT-92 $50,000.00 $2,000.00 ***** ------------ ----------- sum $140,000.00 $3,000.00 4444 Stephen York CM 03-JUL-97 $42,000.00 $2,000.00 ***** ------------ ----------- sum $42,000.00 $2,000.00 Thu Jun 22 page 2 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 2222 John Davidson IN 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown 18-AUG-94 $48,000.00 $2,000.00 ***** ------------ ----------- sum $133,000.00 $5,500.00 8 rows selected. The two SUMS are included based on jobcode break.

12 SQL> CREATE VIEW view_first_pay 2 AS 3 SELECT jobcode, pay_id, name, startdate, salary, bonus 4 FROM first_pay; View created. Reports CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON jobcode SKIP 2 COMPUTE SUM OF salary ON jobcode COMPUTE SUM OF bonus ON jobcode SELECT * FROM view_first_pay ORDER BY jobcode / SQL> edit rptslide7 I would prefer to have jobcode appear first in my report, so I created a view called view_first_pay from first_pay and put jobcode in as the first column. Then I went into rptslide7 (shown below) and changed the SELECT to use view_first_pay.

13 Reports Thu Jun 22 page 1 List of Employees Job Employee Employee Start Code Id# Name Date Salary Bonus ----- ---------- -------------------- ---------- ------------ ----------- AP 3333 Susan Ash 05-FEB-00 $25,000.00 $500.00 ***** ------------ ----------- sum $25,000.00 $500.00 CI 1111 Linda Costa 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown 05-NOV-99 $45,000.00 5555 Richard Jones 30-OCT-92 $50,000.00 $2,000.00 ***** ------------ ----------- sum $140,000.00 $3,000.00 CM 4444 Stephen York 03-JUL-97 $42,000.00 $2,000.00 ***** ------------ ----------- sum $42,000.00 $2,000.00 Thu Jun 22 page 2 List of Employees Job Employee Employee Start Code Id# Name Date Salary Bonus ----- ---------- -------------------- ---------- ------------ ----------- IN 2222 John Davidson 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown 18-AUG-94 $48,000.00 $2,000.00 ***** ------------ ----------- sum $133,000.00 $5,500.00 8 rows selected. Note that jobcode is now in the first column.

14 CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON REPORT COMPUTE SUM OF salary ON REPORT COMPUTE SUM OF bonus ON REPORT SELECT * FROM first_pay ORDER BY jobcode / Reports SQL> @ rptslide8 Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 3333 Susan Ash AP 05-FEB-00 $25,000.00 $500.00 1111 Linda Costa CI 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown CI 05-NOV-99 $45,000.00 5555 Richard Jones CI 30-OCT-92 $50,000.00 $2,000.00 4444 Stephen York CM 03-JUL-97 $42,000.00 $2,000.00 2222 John Davidson IN 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams IN 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown IN 18-AUG-94 $48,000.00 $2,000.00 ------------ ----------- sum $340,000.00 $11,000.00 8 rows selected. BREAK ON REPORT generates final totals.

15 Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus ---------- -------------------- ----- ---------- ------------ ----------- 3333 Susan Ash AP 05-FEB-00 $25,000.00 $500.00 1111 Linda Costa CI 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown CI 05-NOV-99 $45,000.00 5555 Richard Jones CI 30-OCT-92 $50,000.00 $2,000.00 4444 Stephen York CM 03-JUL-97 $42,000.00 $2,000.00 2222 John Davidson IN 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams IN 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown IN 18-AUG-94 $48,000.00 $2,000.00 ------------ ----------- avg $42,500.00 maximum $2,000.00 Reports CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON REPORT COMPUTE AVG OF salary ON REPORT COMPUTE MAX OF bonus ON REPORT SELECT * FROM first_pay ORDER BY jobcode /

16 CLEAR COLUMNS COLUMN pay_id HEADING 'Employee|Id#' FORMAT A10 COLUMN name HEADING 'Employee|Name' FORMAT A20 COLUMN jobcode HEADING 'Job|Code' FORMAT A5 COLUMN startdate HEADING 'Start|Date' FORMAT A10 COLUMN salary HEADING 'Salary' FORMAT $999,999.99 COLUMN bonus HEADING 'Bonus' FORMAT $99,999.99 SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON REPORT ON jobcode SKIP 2 COMPUTE SUM OF salary ON jobcode COMPUTE SUM OF bonus ON jobcode COMPUTE SUM OF salary ON REPORT COMPUTE SUM OF bonus ON REPORT SELECT * FROM view_first_pay ORDER BY jobcode / Reports This is rptslide10 as it appears in the editor. Note that it has break on report and jobcode and that it has totals for both. On the BREAK line, the break that covers the most information (REPORT) is listed first and then the break that covers a smaller group of information (jobcode) is listed next.

17 SQL> @ rptslide10 Fri Jun 23 page 1 List of Employees Job Employee Employee Start Code Id# Name Date Salary Bonus ----- ---------- -------------------- ---------- ------------ ----------- AP 3333 Susan Ash 05-FEB-00 $25,000.00 $500.00 ***** ------------ ----------- sum $25,000.00 $500.00 CI 1111 Linda Costa 15-JAN-97 $45,000.00 $1,000.00 7777 Donald Brown 05-NOV-99 $45,000.00 5555 Richard Jones 30-OCT-92 $50,000.00 $2,000.00 ***** ------------ ----------- sum $140,000.00 $3,000.00 CM 4444 Stephen York 03-JUL-97 $42,000.00 $2,000.00 ***** ------------ ----------- sum $42,000.00 $2,000.00 Fri Jun 23 page 2 List of Employees Job Employee Employee Start Code Id# Name Date Salary Bonus ----- ---------- -------------------- ---------- ------------ ----------- IN 2222 John Davidson 25-SEP-92 $40,000.00 $1,500.00 8888 Paula Adams 12-DEC-98 $45,000.00 $2,000.00 6666 Joanne Brown 18-AUG-94 $48,000.00 $2,000.00 ***** ------------ ----------- sum $133,000.00 $5,500.00 ------------ ----------- sum $340,000.00 $11,000.00 Reports

18 SHOW ALL SQL> SHOW ALL appinfo is ON and set to "SQL*Plus" arraysize 15 autocommit OFF autoprint OFF autotrace OFF blockterminator "." (hex 2e) btitle OFF and is the 1st few characters of the next SELECT statement closecursor OFF cmdsep OFF colsep " " compatibility NATIVE concat "." (hex 2e) copycommit 0 copytypecheck is ON crt "" define "&" (hex 26) echo OFF editfile "afiedt.buf" embedded OFF escape OFF feedback ON for 6 or more rows flagger OFF flush ON heading ON headsep "|" (hex 7c) linesize 80 lno 25 long 80 longchunksize 80 maxdata 60000 newpage 1 non-blocking ON null "" numformat "" numwidth 9

19 pagesize 24 pause is OFF pno 2 recsep WRAP recsepchar " " (hex 20) release 703020200 repfooter OFF and is NULL repheader OFF and is NULL serveroutput OFF showmode OFF spool OFF sqlcase MIXED sqlcode 0 sqlcontinue "> " sqlnumber ON sqlprefix "#" (hex 23) sqlprompt "SQL> " sqlterminator ";" (hex 3b) suffix "SQL" tab ON termout ON time OFF timing OFF trimout ON trimspool OFF ttitle ON and is the following 17 characters: List of Employees underline "-" (hex 2d) user is "SCOTT" verify ON shiftinout INVISIBLE wrap : lines will be wrapped SHOW ALL

20 SQL> clear columns columns cleared SQL> clear breaks breaks cleared SQL> ttitle off SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 8 rows selected. Clearing report environment. Once you have created the report type environment using the report script you have to reverse it to return to get the ordinary results from the SELECT * FROM first_pay; type of command. As shown this can be done with clear columns and clear breaks and with turning features from the SHOW ALL list off or on as needed.

21 SQL> COLUMN salary HEADING 'Salary' FORMAT $999,999.99 SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE Salary BONUS ---- -------------------- -- --------- ------------ --------- 1111 Linda Costa CI 15-JAN-97 $45,000.00 1000 2222 John Davidson IN 25-SEP-92 $40,000.00 1500 3333 Susan Ash AP 05-FEB-00 $25,000.00 500 4444 Stephen York CM 03-JUL-97 $42,000.00 2000 5555 Richard Jones CI 30-OCT-92 $50,000.00 2000 6666 Joanne Brown IN 18-AUG-94 $48,000.00 2000 7777 Donald Brown CI 05-NOV-99 $45,000.00 8888 Paula Adams IN 12-DEC-98 $45,000.00 2000 8 rows selected. Environment With this setting I am saying that this is the format I want used for any column named salary. The table doesn’t matter, it is the column name salary that has been given a heading and formatting.

22 SQL> COLUMN pay_id HEADING 'Pay Id #' FORMAT A10 SQL> SELECT * FROM first_pay; Pay Id # NAME JO STARTDATE Salary BONUS ---------- -------------------- -- --------- ------------ --------- 1111 Linda Costa CI 15-JAN-97 $45,000.00 1000 2222 John Davidson IN 25-SEP-92 $40,000.00 1500 3333 Susan Ash AP 05-FEB-00 $25,000.00 500 4444 Stephen York CM 03-JUL-97 $42,000.00 2000 5555 Richard Jones CI 30-OCT-92 $50,000.00 2000 6666 Joanne Brown IN 18-AUG-94 $48,000.00 2000 7777 Donald Brown CI 05-NOV-99 $45,000.00 8888 Paula Adams IN 12-DEC-98 $45,000.00 2000 8 rows selected. environment In this example, I have increased the size of the column where the pay_id will be displayed. I have also changed the heading. This would apply to any table with a column/field called pay_id.


Download ppt "Reports Using SQL Script Please check speaker notes for additional information!"

Similar presentations


Ads by Google