Solved

PL/SQL correcting record count

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now