?
Solved

PL/SQL correcting record count

Posted on 2008-10-01
5
Medium Priority
?
790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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…
Via a live example, show how to take different types of Oracle backups using RMAN.

764 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