Solved

PL/SQL correcting record count

Posted on 2008-10-01
5
786 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

691 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