Solved

PL/SQL correcting record count

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

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example, show how to take different types of Oracle backups using RMAN.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

18 Experts available now in Live!

Get 1:1 Help Now