Solved

PL/SQL correcting record count

Posted on 2008-10-01
5
774 Views
Last Modified: 2013-12-18
I have the listed below  code that I have modified with this site's help.  The method of counting the number of rows works well for other sql queries, but when I try to use it with a union option, the record count is incorrect.  How do I fix?


Set Pages 0 Lin 132 Trims On Feed Off Ver Off Head Off Emb On Term Off
Col File_Name Noprint New_Value File_Name

SELECT DISTINCT 'USR' || '.txt' AS file_name
FROM DUAL;

Col Num_Cnt Noprint New_Value Num_Cnt
Spo &&File_Name
Prompt SYSTEM_ROLE|INSTITUTION_ROLE|EXTERNAL_PERSON_KEY|USER_ID|PASSWD|FIRSTNAME|MIDDLENAME|LASTNAME|EMAIL

SELECT   'NONE'
      || '|'
      || 'Student'
      || '|'
      || b.person_uid
      || '|'
      || b.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address),
      ROWNUM num_cnt
FROM person b, enrollment c
WHERE     b.person_uid = c.person_uid
      AND c.academic_period = '200910'
      AND c.academic_period NOT LIKE '%1'
      AND c.ar_chrgs_acceptance_ind IN ('Y', 'C')
UNION ALL
SELECT   'NONE'
      || '|'
      || CASE
            WHEN (employee_class) IN ('10', '20') THEN 'Staff'
            WHEN (employee_class) IN ('30', '32', '36', '50') THEN 'Faculty'
            ELSE 'UNKNOWN'
         END
      || '|'
      || a.person_uid
      || '|'
      || a.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address), ROWNUM num_cnt
FROM faculty a, person b, employee c
WHERE     a.academic_period IN ('200910')
      AND a.person_uid = b.person_uid
      AND b.person_uid = c.person_uid
      AND a.faculty_member_status = 'AC'
      AND c.employee_status = 'A';

SELECT   '***FileFooter'
      || '|'
      || '&&Num_Cnt'
      || '|'
      || TO_CHAR (SYSDATE, 'Hh24:Mi:Ss')
      || ' '
      || TO_CHAR (SYSDATE, 'Mm/Dd/Yyyy')
FROM DUAL;

Spo Off
0
Comment
Question by:Jack Seaman
  • 3
  • 2
5 Comments
 

Author Comment

by:Jack Seaman
ID: 22617719
If I were to remove the UNION statement, is there a way I could pass the num_cnt from the first select and add it to the second?
0
 
LVL 10

Expert Comment

by:dbmullen
ID: 22620605
It appears you're building a PIPE delimited file by appending all the columns together, my guess is you're going to read that file into EXCEL..  there are 100's of ways to do this.  TOAD does great at it.  and ROWNUM is not always "sorted" the way you want.

but just wrap the entire thing and select from that.

select column1, rownum
from (
SELECT   'NONE'
      || '|'
      || 'Student'
      || '|'
      || b.person_uid
      || '|'
      || b.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column_1
--,      ROWNUM num_cnt
FROM person b, enrollment c
WHERE     b.person_uid = c.person_uid
      AND c.academic_period = '200910'
      AND c.academic_period NOT LIKE '%1'
      AND c.ar_chrgs_acceptance_ind IN ('Y', 'C')
UNION ALL
SELECT   'NONE'
      || '|'
      || CASE
            WHEN (employee_class) IN ('10', '20') THEN 'Staff'
            WHEN (employee_class) IN ('30', '32', '36', '50') THEN 'Faculty'
            ELSE 'UNKNOWN'
         END
      || '|'
      || a.person_uid
      || '|'
      || a.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column1
--, ROWNUM num_cnt
FROM faculty a, person b, employee c
WHERE     a.academic_period IN ('200910')
      AND a.person_uid = b.person_uid
      AND b.person_uid = c.person_uid
      AND a.faculty_member_status = 'AC'
      AND c.employee_status = 'A')
)

Open in new window

0
 

Author Comment

by:Jack Seaman
ID: 22623421
Tried this and got error sql statement not properly ended.
0
 
LVL 10

Accepted Solution

by:
dbmullen earned 500 total points
ID: 22623911
sorry, type-o
select column1, rownum
from (
SELECT   'NONE'
      || '|'
      || 'Student'
      || '|'
      || b.person_uid
      || '|'
      || b.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column1
--,      ROWNUM num_cnt
FROM person b, enrollment c
WHERE     b.person_uid = c.person_uid
      AND c.academic_period = '200910'
      AND c.academic_period NOT LIKE '%1'
      AND c.ar_chrgs_acceptance_ind IN ('Y', 'C')
UNION ALL
SELECT   'NONE'
      || '|'
      || CASE
            WHEN (employee_class) IN ('10', '20') THEN 'Staff'
            WHEN (employee_class) IN ('30', '32', '36', '50') THEN 'Faculty'
            ELSE 'UNKNOWN'
         END
      || '|'
      || a.person_uid
      || '|'
      || a.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column1
--, ROWNUM num_cnt
FROM faculty a, person b, employee c
WHERE     a.academic_period IN ('200910')
      AND a.person_uid = b.person_uid
      AND b.person_uid = c.person_uid
      AND a.faculty_member_status = 'AC'
      AND c.employee_status = 'A')
)

Open in new window

0
 

Author Comment

by:Jack Seaman
ID: 22627807
I was able get it to run.  See code below.  But there are two errors.  First, the final row count is not being passed to the variable &&Num_Cnt in the final select statement.  Second, in the output file, each record contains the record number.  This should not be there.  I've included the complete code.


Set Pages 0 Lin 132 Trims On Feed Off Ver Off Head Off Emb On Term Off
Col File_Name Noprint New_Value File_Name

SELECT DISTINCT 'USR' || '.txt' AS file_name
FROM DUAL;

Col Num_Cnt Noprint New_Value Num_Cnt
Spo &&File_Name
Prompt SYSTEM_ROLE|INSTITUTION_ROLE|EXTERNAL_PERSON_KEY|USER_ID|PASSWD|FIRSTNAME|MIDDLENAME|LASTNAME|EMAIL

select column1, rownum
from (
SELECT   'NONE'
      || '|'
      || 'Student'
      || '|'
      || b.person_uid
      || '|'
      || b.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column1
--,      ROWNUM num_cnt
FROM person b, enrollment c
WHERE     b.person_uid = c.person_uid
      AND c.academic_period = '200910'
      AND c.academic_period NOT LIKE '%1'
      AND c.ar_chrgs_acceptance_ind IN ('Y', 'C')
UNION ALL
SELECT   'NONE'
      || '|'
      || CASE
            WHEN (employee_class) IN ('10', '20') THEN 'Staff'
            WHEN (employee_class) IN ('30', '32', '36', '50') THEN 'Faculty'
            ELSE 'UNKNOWN'
         END
      || '|'
      || a.person_uid
      || '|'
      || a.id
      || '|'
      || (TO_CHAR (b.birth_date, 'MMDDYY'))
      || '|'
      || b.first_name
      || '|'
      || b.middle_initial
      || '|'
      || b.last_name
      || '|'
      || LOWER (b.email_address) column1
--, ROWNUM num_cnt
FROM faculty a, person b, employee c
WHERE     a.academic_period IN ('200910')
      AND a.person_uid = b.person_uid
      AND b.person_uid = c.person_uid
      AND a.faculty_member_status = 'AC'
      AND c.employee_status = 'A');


 

SELECT   '***FileFooter'
      || '|'
      || '&&Num_Cnt'
      || '|'
      || TO_CHAR (SYSDATE, 'Hh24:Mi:Ss')
      || ' '
      || TO_CHAR (SYSDATE, 'Mm/Dd/Yyyy')
FROM DUAL;

Spo Off
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question