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

Slides:



Advertisements
Similar presentations
Creating an Oracle Database Table Additional information is available in speaker notes!
Advertisements

PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Order Entry System Please use speaker notes for additional information!
7 Copyright © Oracle Corporation, All rights reserved. Producing Readable Output with i SQL*Plus.
Line Efficiency     Percentage Month Today’s Date
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
Using input variables Please use speaker notes for additional information!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
SQL Query Extras MIS 433. Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered.
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Building Self-Updating Excel Workbooks John Filce and Ward Headstrom Institutional Research & Planning Humboldt State University.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Dr. Chen, Business Database Systems (Oracle) Instruction on Creating sql files and SPOOL Jason C.H. Chen, Ph.D. Professor of MIS School of Business, Gonzaga.
ISYS 365 – SQL*Plus Environment. 2 Agenda What is SQL*Plus? Command Line Editor Useful SQL*Plus Commands Useful System Tables What is PL/SQL? PL/SQL Constructs.
XML and DTD Please you speaker notes for additional information!
1 Reports. 2 Objectives  Use concatenation in a query  Change column headings and formats  Add a title to a report  Group data in a report  Include.
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Manipulating data within PL/SQL Please use speaker notes for additional information!
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
A Guide to SQL, Seventh Edition. Objectives Understand how to use functions in queries Use the UPPER and LOWER functions with character data Use the ROUND.
8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.
Functions Please use speaker notes for additional information!
Exceptions in PL/SQL Please use speaker notes for additional information!
When I want to work with SQL, I start off as if I am doing a regular query.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
Chapter 14 Formatting Readable Output. Chapter Objectives  Add a column heading with a line break to a report  Format the appearance of numeric data.
More on Primary and Foreign Keys Please see speaker notes for additional information!
Chapter Twelve Report Writing Objectives: -Writing reports -Page set up -Page layout.
Introduction to PL/SQL As usual, use speaker notes for additional information!
PHP with MYSQL Please use speaker notes for additional information!
Chapter 21: Report writing1 Chapter Twenty One Producing Readable Output Objectives: Writing reports Page set up Page layout Queries with input.
More SQL functions As usual,there is additional information in the speaker notes!
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
Jan 2016 Solar Lunar Data.
A Guide to SQL, Seventh Edition
Chapter Twenty Producing Readable Output
Introduction to Procedures
Q1 Jan Feb Mar ENTER TEXT HERE Notes
MySQL - Creating donorof database offline
Average Monthly Temperature and Rainfall
2017 Jan Sun Mon Tue Wed Thu Fri Sat
Introduction to Views and Reports
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Step 3 Step 2 Step 1 Put your text here Put your text here
Jan Sun Mon Tue Wed Thu Fri Sat
ORACLE.
More and Still More on Procedures and Functions
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE

Producing Readable Output with iSQL*Plus
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

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

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.

rptslide1 Empl Employee Start Id# Name Jo Date Salary Bonus Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC 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 means to execute - the code is named rptslide1

SQL> SELECT * FROM donor; Employee IDNO Name STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected. SQL> SELECT * FROM first_pay; Empl Employee Start Id# Name Jo Date Salary Bonus Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC rows selected. Reports Once the report has been executed, whatever table is used in the a SELECT will try to fit the table layout.

SQL> clear columns columns cleared SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC rows selected. Reports Columns have been cleared so the column settings in the script are not used.

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). rptslide2 Empl Employee Start Id# Name Jo Date Salary Bonus Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC rows rptslide2 runs the script which includes the SELECT so the output is produced.

Report rptslide3 Thu Jun 22 page 1 List of Employees Empl Employee Start Id# Name Jo Date Salary Bonus Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC 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 SET LINESIZE can be used to establish the length of the line. TTITLE establishes a title for the report.

Report Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus Linda Costa CI 15-JAN-97 $45, $1, John Davidson IN 25-SEP-92 $40, $1, Susan Ash AP 05-FEB-00 $25, $ Stephen York CM 03-JUL-97 $42, $2, Richard Jones CI 30-OCT-92 $50, $2, Joanne Brown IN 18-AUG-94 $48, $2, Donald Brown CI 05-NOV-99 $45, Paula Adams IN 12-DEC-98 $45, $2, rows selected. SQL> edit rptslide4 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, COLUMN bonus HEADING 'Bonus' FORMAT $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.

Reports Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus Susan Ash AP 05-FEB-00 $25, $ Linda Costa CI 15-JAN-97 $45, $1, Donald Brown 05-NOV-99 $45, Richard Jones 30-OCT-92 $50, $2, Stephen York CM 03-JUL-97 $42, $2, John Davidson IN 25-SEP-92 $40, $1, Paula Adams 12-DEC-98 $45, $2, Joanne Brown 18-AUG-94 $48, $2, 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, COLUMN bonus HEADING 'Bonus' FORMAT $99, SET LINESIZE 80 TTITLE 'List of Employees' BREAK ON jobcode SELECT * FROM first_pay ORDER BY jobcode / SQL> edit rptslide5 rptslide5 The table has to be ordered by jobcode if you want to break on jobcode.

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, COLUMN bonus HEADING 'Bonus' FORMAT $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

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

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, COLUMN bonus HEADING 'Bonus' FORMAT $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.

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, $ ***** sum $25, $ CI 1111 Linda Costa 15-JAN-97 $45, $1, Donald Brown 05-NOV-99 $45, Richard Jones 30-OCT-92 $50, $2, ***** sum $140, $3, CM 4444 Stephen York 03-JUL-97 $42, $2, ***** sum $42, $2, 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, $1, Paula Adams 12-DEC-98 $45, $2, Joanne Brown 18-AUG-94 $48, $2, ***** sum $133, $5, rows selected. Note that jobcode is now in the first column.

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, COLUMN bonus HEADING 'Bonus' FORMAT $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 rptslide8 Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus Susan Ash AP 05-FEB-00 $25, $ Linda Costa CI 15-JAN-97 $45, $1, Donald Brown CI 05-NOV-99 $45, Richard Jones CI 30-OCT-92 $50, $2, Stephen York CM 03-JUL-97 $42, $2, John Davidson IN 25-SEP-92 $40, $1, Paula Adams IN 12-DEC-98 $45, $2, Joanne Brown IN 18-AUG-94 $48, $2, sum $340, $11, rows selected. BREAK ON REPORT generates final totals.

Thu Jun 22 page 1 List of Employees Employee Employee Job Start Id# Name Code Date Salary Bonus Susan Ash AP 05-FEB-00 $25, $ Linda Costa CI 15-JAN-97 $45, $1, Donald Brown CI 05-NOV-99 $45, Richard Jones CI 30-OCT-92 $50, $2, Stephen York CM 03-JUL-97 $42, $2, John Davidson IN 25-SEP-92 $40, $1, Paula Adams IN 12-DEC-98 $45, $2, Joanne Brown IN 18-AUG-94 $48, $2, avg $42, maximum $2, 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, COLUMN bonus HEADING 'Bonus' FORMAT $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 /

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, COLUMN bonus HEADING 'Bonus' FORMAT $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.

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, $ ***** sum $25, $ CI 1111 Linda Costa 15-JAN-97 $45, $1, Donald Brown 05-NOV-99 $45, Richard Jones 30-OCT-92 $50, $2, ***** sum $140, $3, CM 4444 Stephen York 03-JUL-97 $42, $2, ***** sum $42, $2, 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, $1, Paula Adams 12-DEC-98 $45, $2, Joanne Brown 18-AUG-94 $48, $2, ***** sum $133, $5, sum $340, $11, Reports

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 newpage 1 non-blocking ON null "" numformat "" numwidth 9

pagesize 24 pause is OFF pno 2 recsep WRAP recsepchar " " (hex 20) release 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

SQL> clear columns columns cleared SQL> clear breaks breaks cleared SQL> ttitle off SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC 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.

SQL> COLUMN salary HEADING 'Salary' FORMAT $999, SQL> SELECT * FROM first_pay; PAY_ NAME JO STARTDATE Salary BONUS Linda Costa CI 15-JAN-97 $45, John Davidson IN 25-SEP-92 $40, Susan Ash AP 05-FEB-00 $25, Stephen York CM 03-JUL-97 $42, Richard Jones CI 30-OCT-92 $50, Joanne Brown IN 18-AUG-94 $48, Donald Brown CI 05-NOV-99 $45, Paula Adams IN 12-DEC-98 $45, 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.

SQL> COLUMN pay_id HEADING 'Pay Id #' FORMAT A10 SQL> SELECT * FROM first_pay; Pay Id # NAME JO STARTDATE Salary BONUS Linda Costa CI 15-JAN-97 $45, John Davidson IN 25-SEP-92 $40, Susan Ash AP 05-FEB-00 $25, Stephen York CM 03-JUL-97 $42, Richard Jones CI 30-OCT-92 $50, Joanne Brown IN 18-AUG-94 $48, Donald Brown CI 05-NOV-99 $45, Paula Adams IN 12-DEC-98 $45, 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.