How to Combine two queries into One

Hi Gurus,
SQL> select * from v$version;
BANNER
Oracle Database 10g Release 10.2.0.4.0 - 64bit Production
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL>
I have following two queries. How I can convert these two queries into one with the same result ?
Q1
SELECT id,vdate,vendorname,venddate,vid
FROM VENDOR
WHERE processid=32
AND venddate is null
AND vid in(select vid from view_vid where type='PDX')
AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=55)
AND (id,vdate) in (select id,vidate from market_data where marketed_date is null)
AND id not in (select id from location)
Q2
SELECT id,vdate,vendorname,venddate,vid
FROM VENDOR
WHERE processid=20
AND venddate is null
AND vid in(select vid from view_vid where type='PBX')
AND (id,vdate) not in (select id,vdate from vkfl_is where task_id=40)
AND (id,vdate) in (select id,vidate from market_data where region_date is null)
AND id not in (select id from location)
I can UNION these two queries, but, is there any way I can write a single query with which gives me same result as above two queries?
Any help would be highly appreciated
Thanks in advance

Hi,
You can do something like this, which will be more efficient than a UNION:
SELECT  id, vdate, vendorname, venddate, vid
FROM  vendor
WHERE  (   (    -- Conditions that apply only to q1
                           processid = 32
            AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PDX')
            AND (id,vdate) NOT IN ( SELECT id, vdate
                                    FROM   vkfl_is
                                    WHERE  task_id = 55
            AND (id,vdate) IN ( SELECT id, vidate
                                FROM   market_data
                                WHERE  marketed_date IS NULL
        OR  (  -- Conditions that apply only to q2
                 processid = 20
            AND  vid       IN (SELECT vid FROM view_vid WHERE type = 'PBX')
            AND (id,vdate) NOT IN ( SELECT id, vdate
                                    FROM   vkfl_is
                                    WHERE  task_id = 40
            AND (id,vdate) IN ( SELECT  id, vidate
                                FROM  market_data
                                WHERE  region_date IS NULL
               -- The remaining conditions apply to both q1 and q2
AND     venddate   IS NULL
AND     id         NOT IN (SELECT id FROM location)
I hope this answers your question.
If not, post  a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
See the forum FAQ: https://forums.oracle.com/message/9362002

Similar Messages

  • How to combine two queries in one EXCEL

    hi i got one requirement
    i got one excel sheet  from user which contains two  report s(hoe we know whether it is worknook or not) of  2011 data and now user wants 2012 data .now i icluded jan 2012 to dec 2012 in two reports and sent to user.But user is asking these two reports want to see in one sheet.
    so how can i proceed: if i go for work  book  how can i include these two reports in workbook and here my doubt is if i create a workbook based on  these two querie which name i have to give user.Please help me out on the same.
    Regards,
    Madhu.

    Hi Madhu
    Run one query. Click on a empty space in the excel
    From the BEx Design Toolbar -
    >Insert Analysis Grid -
    > Right click on the Design Item
    Edit the DataProvider.....You can ran same or different query with selection. 
    Save the result
    Check this link for detail....I am also searching for something with screenshot
    Regards
    Anindya
    Edited by: Anindya Bose on Feb 14, 2012 3:04 AM

  • How to combine two albums into one

    I imported into iTunes two Shostakovich compositions so that I could burn a CD-R that contained these two works. The CD burning went fine, but iTunes insists on separating the two compositions into two albums in the display of all albums. I have made sure that the album titles and the artists displayed under the album icons are identical, but still the two will not meld into one. Will someone help me with this? I am using iTunes 9.2.1(4).

      On the "Get Information" windows (reached with right clicks) of the two separated albums, one album was marked as "no" on the "part of a compilation" option and the other was marked "yes". I changed the "no" to a "yes" and they merged. I'm not sure if I got this from the site you gave me or if I just stumbled on to it. Either way, I thank you for your kind responses.

  • Combining two queries into one query if possible

    Hi there. I would like, if possible, to somehow combine the queries 1) and 2) into a single query.
    1) select distinct user_id from system_user_sessions;
    This query returns all unique users from the indicated table.
    2) select count(session_id) from system_user_sessions where user_id = "each user_id returned from 1)";
    This query will return, for each distinct user_id, the number of sessions involving that user. In other words I would like to return the user_id of each user together with the number of session_ids involving that user.
    any ideas? Joe

    I assume you are looking for something like this:
    select count(session_id)
      from system_user_sessions
    where user_id in ( select distinct user_id
                          from system_user_sessions
                      );HTH
    Ghulam

  • How do I combine two regents into one in Logic Pro Arrange?

    how do I combine two regents into one in Logic Pro Arrange?

    Thanks for your input kcstudio. I would still be left with 2 regions although one after the other, on one track.
    I am trying to save steps of the way I am combining the info of 2 seperate regions into one: bouncing the 2 tracks then re-importing that audio back into the project. It would save a lot of time if you could just drag one region on top of the other
                                W.W.

  • HT4759 How do I combine two accounts into one

    How can I combine two accounts into one account

    If you are talking about iCloud accounts, you can't.
    Or you could tell us what type of accounts these are?

  • Combining two messages into one

    Hello,
    I have a mapping issue, I have a business process in which I have to combine two messages into one.
    To do so, I have defined a message mapping in which there are two source message types, and one target message type.
    When testing the message mapping in the IR, it works fine, but when I define and test an interface mapping with that same message mapping, I receive this error message:
    "Cannot produce target element /ns0:Messages/ns0Message1/MY_IDOC_TYPE/MY_SEGMENT. Check xml instance is valid for source xsd and target-field mapping fulfills requirements of target xsd."
    What am I doing wrong ?
    How am I supposed to combine two messages into one ?
    Thanks,
    Elad.

    Hi Elad,
    Just see if you have included both you source message interfaces and one target nessage interface correctly in while doing your interface mapping.....
    The most common mistake made when we do scenarios which include combination of two source message types into one target type are:
    - we forget to include both the source message interfaces.
    - or we interchange between one of the source message interface with the target.
    Just see if either of the 2 is aplicable in your case and just activate all components as and when you create them..
    Regards,
    abhy

  • How to combine mutiple tracks into one album in itunes 12

    How to combine mutiple tracks into one album in itunes 12

    Make sure they all have the same Album Artist and Album Name.

  • HT4059 I'm trying to organize my PDFs in iBooks on my ipad2 and I can't figure out how to combine multiple PDFs into one. Any thoughts?

    I'm trying to organize my PDFs in iBooks on my ipad2 and can't figure out how to combine multiple PDFs into one. Any thoughts?

    I'm trying to organize my PDFs in iBooks on my ipad2 and can't figure out how to combine multiple PDFs into one. Any thoughts?

  • How to combine many rows into one row

    Hi all,
    I have a question regarding to how to combine many rows into one row?
    My result set is like that:
    ITEM_NO NAME1
    11 abc
    11 cde
    11 fg
    Want to combine them into
    ITEM_NO NAME1
    11 abc;cde;fg
    would anybody can tell me how to do that? Thanks
    Ray

    You can check this --
    satyaki>
    satyaki>
    satyaki>create table t
      2  as
      3      select 11 ITEM_NO, 'abc' NAME1 from dual
      4      union all
      5      select 11 ITEM_NO, 'cde' NAME1 from dual
      6      union all
      7      select 11 ITEM_NO, 'fg' NAME1 from dual;
    Table created.
    satyaki>
    satyaki>
    satyaki>
    satyaki>set lin 10
    satyaki>
    satyaki>desc t;
    Name              Null?    Type
    ITEM_NO                    NUMBER
    NAME1                      VARCHAR2(3)
    satyaki>
    satyaki>
    satyaki>set lin 1000
    satyaki>
    satyaki>
    satyaki>
    satyaki>SELECT ITEM_NO,
      2         LTRIM(MAX(SYS_CONNECT_BY_PATH(NAME1,';'))
      3         KEEP (DENSE_RANK LAST ORDER BY curr),';') AS NAME1_DET
      4  FROM   (SELECT ITEM_NO,
      5                 NAME1,
      6                 ROW_NUMBER() OVER (PARTITION BY ITEM_NO ORDER BY NAME1) AS curr,
      7                 ROW_NUMBER() OVER (PARTITION BY ITEM_NO ORDER BY NAME1) -1 AS prev
      8          FROM   t)
      9  GROUP BY ITEM_NO
    10  CONNECT BY prev = PRIOR curr AND ITEM_NO = PRIOR ITEM_NO
    11  START WITH curr = 1;
       ITEM_NO  NAME1_DET
            11  abc;cde;fgRegards.
    Satyaki De.

  • Question - how do I combine two users into one?

    I have migrated a lot of data to a MacBook Pro using Migration Assistant, in the process two users have been created on the Mac. How do I combine the two users into one?

    You don't. Just log in with one, choose Go to Folder from the Finder's Go menu, provide /Users/ as the path. and drag the folder there; you'll need to provide an administrator password at some point.
    (108352)

  • Combining two queries in one..

    Hi,
    Can someone tell me how to combine follwoing two queries in one so that it will increase performance..
    SELECT single PLNNR from AFKO
           into T1
           WHERE AUFNR = '007200000100'.
    SELECT single STATU from PLKO
           into T2
           WHERE PLNNR = T1.

    using subqueries it is possible
    select single statu from plko into t2 where plnnr in (select single plnnr from afko where aufnr eq '007200000100').
    read about subqueries at this link...
    http://help.sap.com/saphelp_erp2004/helpdata/en/dc/dc7614099b11d295320000e8353423/frameset.htm
    regards,
    PJ

  • How to combine .wmv files into one file???

    Hello Apple World,
    Can someone tell me a software that will allow me to combine ".wmv" files into one? I have several files that I need to combine then add music to. VERY simple with several software packages frow Windows pc's, but I'm struggling with this since getting my first mac! PLEASE help.

    Hi:
    How can I edit MPEG or convert DVD or MPEG to DV
    http://www.sjoki.uta.fi/%7Eshmhav/SVCDon_a_Macintosh.html#edit_convertMPEG
    Matti says it better than any of us:
    how/where you can join clips with Streamclip
    Also MPEG Streamclip guide explains it.
    MPEG Streamclip can join similar MPEG files: The joined files must have the same PIDs, the same start codes, and the same audio/video properties (that is, they must come from the same source or channel).
    Using "Convert to MPEG" before joining the files can be helpful, because it changes PIDs and start codes to a default value. The preferred method to join streams is to Copy one stream in MPEG Streamclip, open another stream and Paste it there. This method checks that the joined streams indeed are compatible. Another method is to put the MPEGs in the same folder, and rename them so that they sort as desired in list view. Then select them via MPEG Streamclip's "File/Open Files..." dialog box (Shift- or Command-click to select multiple MPEGs). Then choose "Edit/Fix Timecode Breaks". After this MPEG Streamclip should report the combined length of all MPEGs (check the Log Window if you want to know whether any timecode breaks were found). Then choose "File/Convert to MPEG... or /Save As..." to save them in a single file. If the video transition between two files looks bad, you can use the Cut command to improve it. You can join very different and incompatible MPEGs with this latter method so the end result is not guaranteed to work.
    MPEG Streamclip can also edit MPEG files. Just set In/Out points (with I/O keys), Cut unwanted material off and choose "File/Convert to MPEG...". You can also Cut/Copy selections to other parts of the same stream or open another compatible stream and Paste it there. Shift-dragging the playhead can also be used to define a selection. Option + arrow keys jump to the beginning/end or In/Out points. You can use the Trim-command to more closely see a part of the video before cutting, then choose Revert Trimming to see all material. JKL-keys can be used to enable fast forward or reverse playing. The scroll wheel works also; with the Option key it scrolls 1 second per click. See the manual for more details on how to jump in single frame, GOP, 1 second, 10 second or 1 minute chunks when searching a specific spot in the video.
    Editing and trimming can be accurate only if In and Out are both on keyframes because MPEG Streamclip edits to the GOP, not to the frame. For DVD and (XS)VCD the maximum GOP size is PAL 15 and NTSC 18 frames, so the editing accuracy can be up to about 0.6 seconds. You can advance to the previous or next keyframe (i.e. I-frame, the beginning of each GOP or Group Of Pictures) with the Up/Down arrow keys -- you can also use the Edit/Go to Keyframe command to see where the In and Out points really are when editing (the In point is included in the selection, the Out point is not included). Shift + Up/Down arrow keys allow fine-tuning the selection to the GOP.
    Sue

  • Combining two tables into one result

    Hi all, I am fairly inexperienced with Crystal but have gained good results from the reports I have created, I am fluent in SQL and Basic to an extent.  I am interigating a Pronto database and I want concatenate two tables into one report.
    Table One: Customer Orders
    Fields:  Customer No, Invoice Number, Invoice date, Invoice Amount, Cost Amount.
    Table Two: Archived Customer Orders
    Fields:  Customer No, Invoice Number, Invoice date, Invoice Amount, Cost Amount.
    I want to complete a report that will tell me the sales for a given period and the outstanding orders for each customer total for all fields in each table combined into one figure.
    Any help would be greatly appreciated.
    Thanks in advance

    Hi Grant,
    As you said you are goot at writing SQL, in Crystal write a SQL statement in Add Command to join both table using Union / Union all
    If you use Union then it will filter duplicate records and pull the data from database, If you use Union All then it will include all your duplicate records.  Please use Union / Union All as required.
    Once you get all data into the report create a parameter for date range and generate the summaries as required.
    Thanks,
    Sastry

  • How to turn two users into one?

    Hey there!
    As I selled my MBP last month I copied all its datas to my iMac via migration assistant and created a new user for it. So I have now two accounts with different photos, documents etc.
    My problem now is how to turn these two users into one?
    I want all my photos appear in Aperture of my first user, I want all my documents appear in "Documents" of the first user and so on..
    Is it able? If yes, how could I realize it?
    Thanks for your support!
    René
    PS: 2 Users on iMac -> 1 User on iMac.

    Thanks for your help at first.
    But you usually can't "merge" two "normal" user accounts successfully.  If both accounts have data for the same application, it will be very difficult or impossible to merge them by copying files & folders.
    I cannot merge them.. So there is the question how to copy all medias from Aperture to my other Aperture medias?
    Can I export my whole medias and import in at the other users Aperture? And how? #
    Edit: I dont want to have two different libraries at all.. I want them to be together in just one..

Maybe you are looking for