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
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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now


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

Technology Partners: 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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 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