Posted on 2004-09-15
Last Modified: 2010-05-02
I have created a query for data extraction ; I save it in the excel format and email it to user.


select distinct mkt.last ||', '|| mkt.first mktname, ||'-'|| phys.last ||', '|| phys.first physname,
     phys.zip1 ||', '|| phys.state zipst, phon.phonea ||'-'|| phoneb ||'-'|| phon.phonec physphone,
     invt.manufacturer_id ||'-'|| mfg,
     count(distinct ship.to_id) pats,   sum(shipi.quantity) qty

from shipments ship,
     patient_therapies pthx,
     patients_table pt,
     marketing mkt,
     shipment_items shipi,
     prescriptions_table presc,
     inventory invt,
     manufacturers man,
     physicians phys,
     phones phon

where ship.completed_date between '01-JAN-2004' AND '31-JAN-2004'
and ship.to_type = 'P'
and ship.to_id = pthx.patient_id
and ship.to_id =
and pt.marketing_id =
and pthx.stop_date is null
and pthx.therapy_type in (select therapy_type from therapy_types
                          where therapy_class in 'HARMONE')
and = shipi.shipment_id
and shipi.inventory_id =
and invt.tdrug_abbrev is not null
and invt.manufacturer_id =
and (shipi.rx_prescription_id = presc.prescription_id
    and shipi.rx_svcbr_id = presc.svcbr_id
    and shipi.rx_refill_no = presc.refill_no
    and ship.to_id = presc.patient_id)
and presc.physician_id =
and ( = phon.name_id(+)
    and phon.name_type = 'D'
    and phon.phone_seq = 1)
      and = 230323

group by mkt.last ||', '|| mkt.first, ||'-'|| phys.last ||', '|| phys.first,
     phys.zip1 ||', '|| phys.state, phon.phonea ||'-'|| phoneb ||'-'|| phon.phonec,
     invt.manufacturer_id ||'-'||

Sample data:

MKTNAME      PHYSNAME      MFG-id and name        PATS              QTY
SCOTT, LELA      323-MATHEW      25-ABC                    1                    7
SCOTT, LELA      323-MATHEW      34-EFH                   2                    17
SCOTT, LELA      323-MATHEW      67-XYZ                   1                    14

So one row is getting repeated thrice because there are 3 entries for mfg-id and name. But I want the following to be displayed in the foll. Format. The mfg id and its corresponding name may vary every time I run the sql.  i MEAN THE mfg id and name field can be abc, efg, xyz this week and next week it could be all of the above 3 and it could be some other ones like hjk, nmk,
Or it could not have any of the abc, efg, xyz and could be only hjk, nmkCan u please tell me how to get the data out in the foll. Format??.. Please paste the code for doing it in plain sql. and also in plsql; plain sql code more important. Can this be THANKS EXPERTS!!!

MKTNAME      PHYSNAME      25-ABC      34-EFH      67-XYZ
SCOTT, LELA      323-MATHEW      7      17      14

Question by:Sara_j_11
  • 3
  • 3

Author Comment

ID: 12069803
i MEAN THE mafg id and name field can be abc, efg, xyz this week and next week it could be all of the above 3 and it could be some other ones like hjk, nmk,
Or it could not have any of the abc, efg, xyz and could be only hjk, nmk

Expert Comment

ID: 12077208
How are you executing the query and creating the file?  VB and ADO?  If so then the easiest solution is to forget about modifying the complicated SQL, take the results as they are now, and format the results the way that you want them as you insert into the Excel file.

Oracle does not make pivoting tables easy.  If you are determined to do this with SQL then look at and search for pivot.

Author Comment

ID: 12077913
I am using the toad editor to execute the sql and save it in the excel format. Any comments on doing it in sql would be greatly apprecited
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.


Accepted Solution

Prestaul earned 500 total points
ID: 12078204

Author Comment

ID: 12088729
I still dont know how to handle my case since mine is fully dynamic and the examples are for known columns..

Expert Comment

ID: 12089106
Sorry.  I think that you should process the data from your original query before writing the Excel file.  Good luck.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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