Generating Statistics from a CSV File

You can create CSV files containing raw data, import the data into Oracle, then generate statistics using the library of functions described in PL/SQL Statisics Library

The source code for all the programs referenced below is in the Demo Oracle Application
Download Apps Demo
Download Demo App


You can view the source code in the GitHub repository:
GitHub Repo
GitHub Repository

Create a CSV File

In this example, the PL/SQL examination results for an elite group of programmers, which may include a few cats, are stored in a CSV file.

  • The CSV file contains header records which identify each stats project, followed by data for that project.
  • The header record must start with the word "PROJECT" and be followed by a description.
  • The data records consist of a description for the data value, followed by its numeric value.
  • You can have data for multiple projects in the CSV file.

PROJECT,PL/SQL Exam Results
Fred,90
Jim,93
Ann,97
Ian,98
Steve F,100
Bruce Scott,99
Tiger,95
Mary,91
Connor McD,100
Seyi,96
Harsh,98
Peter,89
Doris,85
Winston,92
Charlie,91
Morse,90
              

Import CSV Data into Oracle

Next we will import the data into Oracle tables STATS_PROJECT which identifies each project using a unique PROJECT_ID, and STATS_DATA which contains its data.

  1. Copy the CSV file to DATA_HOME/received
  2. Run script APP_HOME/com/import_stats
GitHub Repo
import_stats.bat


The import_stats script will process the CSV files in the Received directory:
  • If the data is valid it will be loaded into the Oracle tables.
  • A new Project ID is generated to identify the imported stats data, and this is displayed.
  • The statistics are calculated from the imported data.
  • A CSV file containing the statistics is created; DATA_HOME/data_out/stats_[ProjectID]_YYYYMMDD_HHMMSS.csv

FREEPDB1\APPSDEMO>import_stats

FREEPDB1\APPSDEMO>ECHO OFF
Enter the password for DEMO_CONNECT: *******
CSV FILE FOUND: D:\demo\oracle-appsdemo-main\data\RECEIVED\stats_demo_exam.csv
        1 file(s) copied.

SQL*Plus: Release 23.26.1.0.0 - Production on Fri Apr 17 16:02:55 2026
Version 23.26.1.0.0

Copyright (c) 1982, 2025, Oracle.  All rights reserved.

Last Successful login time: Fri Apr 17 2026 15:59:34 +01:00

Connected to:
Oracle AI Database 26ai Free Release 23.26.1.0.0 - Develop, Learn, and Run for Free
Version 23.26.1.0.0

old   2:   v_filename plsql_constants.filenamelength_t := '&1';
new   2:   v_filename plsql_constants.filenamelength_t := 'stats_demo_exam.csv';
Data Import from file: stats_demo_exam.csv
Stats data imported OK for Project ID=4
Statistics exported to file: stats_4_20260417_160455.csv

PL/SQL procedure successfully completed.

Disconnected from Oracle AI Database 26ai Free Release 23.26.1.0.0 - Develop, Learn, and Run for Free
Version 23.26.1.0.0

FREEPDB1\APPSDEMO>
              

Run SQL Report to Display Project Stats Data

Run the SQL report /sql/stats_data to display your project data, Project ID is 4

GitHub Repo
stats_data.sql


                                                    Bond and Pollard Limited                                                     
                                                    ========================                                                     
Project Statistics Data                                                                                           Page:          1
                                                                                                                                  
                                                                                                                                  
Project ID Description          Data ID Data Project ID Data Description                          Value Check                       
---------- -------------------- ------- --------------- -------------------- -------------------------- ----------------------------
         4 PL/SQL Exam Results       29               4 Fred                              90.0000000000                             
         4 PL/SQL Exam Results       30               4 Jim                               93.0000000000                             
         4 PL/SQL Exam Results       31               4 Ann                               97.0000000000                             
         4 PL/SQL Exam Results       32               4 Ian                               98.0000000000                             
         4 PL/SQL Exam Results       33               4 Steve F                          100.0000000000                             
         4 PL/SQL Exam Results       34               4 Bruce Scott                       99.0000000000                             
         4 PL/SQL Exam Results       35               4 Tiger                             95.0000000000                             
         4 PL/SQL Exam Results       36               4 Mary                              91.0000000000                             
         4 PL/SQL Exam Results       37               4 Connor McD                       100.0000000000                             
         4 PL/SQL Exam Results       38               4 Seyi                              96.0000000000                             
         4 PL/SQL Exam Results       39               4 Harsh                             98.0000000000                             
         4 PL/SQL Exam Results       40               4 Peter                             89.0000000000                             
         4 PL/SQL Exam Results       41               4 Doris                             85.0000000000                             
         4 PL/SQL Exam Results       42               4 Winston                           92.0000000000                             
         4 PL/SQL Exam Results       43               4 Charlie                           91.0000000000                             
         4 PL/SQL Exam Results       44               4 Morse                             90.0000000000                             

16 rows selected. 
              

Generate the Statistics Report

Run the SQL report /sql/stats_project to display the statistics for your project: Project ID is 4.

GitHub Repo
stats_project.sql


----------------------------------------------------------
FREQUENCY TABLE
----------------------------------------------------------
KEY=85.0000000000 Frequency=1
KEY=89.0000000000 Frequency=1
KEY=90.0000000000 Frequency=2
KEY=91.0000000000 Frequency=2
KEY=92.0000000000 Frequency=1
KEY=93.0000000000 Frequency=1
KEY=95.0000000000 Frequency=1
KEY=96.0000000000 Frequency=1
KEY=97.0000000000 Frequency=1
KEY=98.0000000000 Frequency=2
KEY=99.0000000000 Frequency=1
KEY=100.0000000000 Frequency=2
----------------------------------------------------------
STATISTICS
Sum=1,504.0000000000
N Total=16
Distinct N=12
Mean=94.0000000000
Median=94.0000000000
Mode 1 = 90.0000000000
Mode 2 = 91.0000000000
Mode 3 = 98.0000000000
Mode 4 = 100.0000000000
Lowest=85.0000000000
Highest=100.0000000000
Range=15.0000000000
Variance Population=19.0000000000
Variance Sample=20.2666666667
Standard Deviation Population=4.3588989435
Standard Deviation Sample=4.5018514710
Interquartile Range=7.2500000000
Percentile Discrete ( 0.67)=97.0000000000
Percentile Continuous ( 0.67)=97.0500000000
PERCENTILES
PCT_DISC ( 0.67)=97.0000000000
PCT_CONT ( 0.67)=97.0500000000
              


Export Stats to a CSV file

Run the SQL report /sql/stats_project_csv to display the statistics, and export the results to CSV files.

GitHub Repo
stats_project_csv.sql


Previous Blog HOME