Query with almost the same query as a subquery, can it be optimized?

I have a query using almost a duplicate of itself (table 1) as a subquery, and I wonder if it can be rewritten better, like she just doesn't seem the right way to do it, but everything I try I can't get the same number of lines back.

If anyone can see a better way, it would be a great help.

Thank you very much
SELECT t.pol_number||t.check_digit
FROM   transactions t, policies p, funds f
WHERE f.fnd_code = t.trn_fnd_code
AND     t.trn_pol_number = p.pol_number
AND     p.pol_status IN (1,2)
AND     t.trn_status = 'NEW'
AND     t.trn_trn_number IN
   (SELECT t2.trn_trn_number
    FROM   transactions t2, policies p2
    WHERE t2.trn_pol_number = p2.pol_number 
    AND     p2.pol_status IN (1,2)
    AND     t2.trn_status = 'NEW'
    AND     t2.trn_fnd_code = :PARAMETER)
Surely it is not the best way to write it as you use the same table twice and restrictions?

Published by: Scott Hillier on March 16, 2010 15:11

It looks like you want all the lines of the siblings who do not personally meet the restriction on trn_fnd_code. You can use analytical functions;

not tested

select *
  from (select pol_number || check_digit col1,
               max(case
                      when t.trn_fnd_code = :parameter then
                       1
                      else
                       0
                    end) over(partition by trn_trn_number) max_trn_number
          from transactions t, policies p, funds f
         where f.fnd_code = t.trn_fnd_code
           and p.pol_status in (1, 2)
           and t.trn_status = 'NEW')
 where max_trn_number = 1;

Tags: Database

Similar Questions

  • I forgot my password for apple and have permission to stage the 2 and broken my old phone and do not have the same number of what I can do my account is already waiting to be reset, but it takes apple long to to deal with the what else is there to do?

    I forgot my password for apple and have permission to stage the 2 and broken my old phone and do not have the same number of what I can do my account is already waiting to be reset, but it takes apple long to to deal with the what else is there to do?

    You know the e-mail password what do you use? I lost over $ 30 on my old Apple ID because I couldn't get into the email and apple would not transfer my money to my new account.

  • On a tablet with W10, I put separate profiles for myself & wife but Firefox always opens in my profile, it goes the same for Thunderbird. How can I change this?

    I tried the "firefox.exe profilemanager" method (& similar to Thunderbird) but no improvement. " Help please!

    They should open the profile of the current user logged in.
    You are two with the same login?

    You can create other user profiles in the same folder of system user.

    https://support.Mozilla.org/en-us/KB/Profile-Manager-create-and-remove-Firefox-profiles

    https://support.Mozilla.org/en-us/KB/recovering-important-data-from-an-old-profile

    https://support.Mozilla.org/en-us/KB/back-and-restore-information-Firefox-profiles

  • I want to restrict a user to enter in the same interval of time or with in the same interval

    Hi all

    I am pleased to wish you a happy and prosperous new year.

    Q: in my application I need to provide a validation the user to not enter the same interval of time or in the same interval of time

    To make a unique combination of the user and the time interval that I can use a Unique Index and then I can get the unique combination of the intervals of this user. But I want to restrict the user even not to enter with in the same time interval.

    Scenario: Let's assume we have 10 records of 10 different users with different time intervals, one of them

    syam user with time interval entry 10:00 to 12:30, then the following entry syam user of time intervals should not be 10:00 to 12:30 and should not be in the same interval from 10:00 to 11:30 or 11:00 to 12: 10.

    How can I do this, I need to provide a validation.

    Thanks in advance

    -I borrowed the table of Bruno

    INSERT INTO MaTable VALUES ('tom', TO_DATE ('0701151330', 'DDMMYYHH24MI'), TO_DATE ('0701151740', 'DDMMYYHH24MI'));
    COMMIT;

    CREATE MATERIALIZED VIEW LOG ON mytable WITH SEQUENCE, ROWID, PRIMARY KEY including the NEW VALUES;

    Create materialized view mv_overlap_chk
    cool off quickly on commit WITH ROWID
    as
    Select t1.rowid rid1, t2.rowid rid2
    FROM MaTable t1
    mytable t2
    where t1.usr = t2.usr
    AND t1.rowid<>t2.rowid
    AND (t1.from_dte BETWEEN t2.from_dte AND t2.to_dte
    OR t2.from_dte BETWEEN t1.from_dte AND t1.to_dte
    )
    /

    ALTER TABLE mv_overlap_chk add CONSTRAINT CHK_OVERLAP CHECK(1=0);

    -trial:

    INSERT INTO MaTable VALUES ('tom', TO_DATE ('070120151630', 'DDMMYYYYHH24MI'), TO_DATE ('070120151840', 'DDMMYYYYHH24MI'));

    1 line of creation.

    SQL > commit;
    Commit
    *
    ERROR on line 1:
    ORA-12008: error path refresh materialized view
    ORA-02290: check constraint (JSU. CHK_OVERLAP) violated

    Therefore, constraint will be checked on commit.

  • I've recently updated to Muse CC (2014.2), but this morning, it appeared in my menu cloud creative as needing to be updated again. What is a minor update with exactly the same name?

    I've recently updated to Muse CC (2014.2), but this morning, it appeared in my menu cloud creative as needing to be updated again. What is a minor update with exactly the same name?

    The update is for a few bug fixes and will show 2014.2 in CC as the update includes the fix: Release notes | Adobe Muse CC

    For more exact version information, you can check on muse Muse in menu options.

    Thank you

    Sanjit

  • When I opened my firefox on my mac - it opens to a blank screen. I tried to remove and redownloading but the same thing happens. How can I fix?

    When I opened my firefox on my mac - it opens to a blank screen. I tried to remove and redownloading but the same thing happens. How can I fix?

    Ahh. I thought you meant Subject: vacuum

    Start Firefox in Safe Mode {web link} by holding down the < shift >
    (Mac options)
    key and then from Firefox. Is always the problem?

    Start your computer in safe mode with network. Then launch Firefox.
    Try the sites secure web. Is always the problem?

    Start the computer in Mode safe;
    Free online encyclopedia

  • Periodicall I received a message that amother computer were found at the same IP address? How can I find what is happening?

    Not always; but sometimes I get a message that "another computer was found with the same IP address.  How can I determine the cause of that?

    Technically, your post is in the wrong forum (you are in the MSE Forum).

    But see if this thread in Forum Windows answers your question: http://answers.microsoft.com/en-us/windows/forum/windows_7-security/ip-ddress/a33dc30c-a1c9-4b5d-a37a-85fb857e702c

    Kind regards...

  • Have Vista & attempted to open several pdf, got the same error msg: Photo Gallery can't open this photo or video. This file format is not supported or you do not have the photo gallery was last updated. »

    I have a laptop with Vista & have tried opening several pdf for about a month, got the same error msg: "Photo Gallery can't open this photo or video. This file format is not supported or you do not have the photo gallery was last updated. »

    Can I still use the Windows Photo Gallery to open the .jpg images that are saved on the disk "c";  In addition, a month ago, I was able to open PDFs with success.

    Pls help!

    PDF files must be associated with a like Adobe Reader or Foxit Reader PDF reader.

    Articles expain how to change a file association.

    Windows Vista - change which programs Windows uses by default
    http://Windows.Microsoft.com/en-us/Windows-Vista/change-which-programs-Windows-uses-by-default

    Windows Vista - how to associate a file Extension
    Type with a program under Vista
    http://www.Vistax64.com/tutorials/69758-default-programs.html
    (see "method 2")

  • I can't print the same document twice (no I can print the same document 2 veces)

    Hi all

    I have no problem printing of any document on other programs. for example, I can print all copies I want to the Explorer internet, word, etc, but when I want to print a PDF file that I can print only one copy (with all the pages I want, but only the first 'pack')

    I need usually 2 or 3 copies of the same pdf file and I can't print 1 every time.

    Can you help me? Thank you!!

    -------------------------

    Hola a todos,

    No tengo problemas al print any document in other programs. For example, I can print all form that quiera en internet explorer, word, etc. pero cuando quiero print a solo PDF puedeo print una copia (con todas las pages that quiera, pero solo el primer 'pack')

    Usually I need 2 o 3 form the same pdf y solo puedo print 1 cada vez.

    Podriais help me? Gracias!

    What Adobe Reader version you use Reader X or Reader 9.4.1

  • My iTunes is a collection of songs with the same artist, the same album artist and the same album separately. How can I fix it?

    Can someone tell me how to solve this problem? I put a lot of effort in organizing my collection and I hate to see 15 sections 'Lil Wayne' in my artist entry or right of the complete discography of Paul McCartney by Paul McCartney with 1 song. I tried to edit all the files together, intentionally misspelling the name and spelling then it right once again, but it do not work and that the use of work well, but it doesn't seem to work.

    If iTunes displays multiple instances of an artist or an album then generally what works is to select all the associated tracks and use read information to add say end X for each of the areas that the slopes have in common:

    • For an album. Album, artist of the Album and artist (if the artist is the same for all tracks) *.
    • For an artist; Album artist (artist and unless there is the comments/featured artists listed that should not be changed)

    Apply the change which merges things together, then remove the excess characters. Occasionally, it can help to close and reopen iTunes between two renaming operations. Part of a compilation should also be set in a consistent manner.

    * If the titles must be synchronized to a non - iOS device, there should be a common artist and/or album must be defined as a Compilation.

    Use the view of songs and display fields, Album, genre, Album, Album artist, genre artist Album, artist and genre artist side by side to see you whether or not it is necessary to change the artist and if sorting values could be the cause of additional problems. See grouping in albums, titles for help if necessary.

    Another tip duplicates stubborn reality. At one point, I had three batches of various artists in the view of artists in my iTunes game library that wouldn't meet usual backward X treatment. What I found worked was adding the final X, but with each group, iTunes wanted to separate start typing a value and let iTunes autocomplete of the Var... for various artists. Collection of AutoComplete lists seemed to work when the full value of collage/montage does not.

    TT2

  • Shouldn't be using WITH return the same results as if you would first put the results in a table?

    First of all, here is my version info:

    BANNER

    --------------------------------------------------------------------------------

    Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production

    PL/SQL Release 11.1.0.7.0 - Production

    CORE Production 11.1.0.7.0

    AMT for HP - UX: 11.1.0.7.0 - Production Version

    NLSRTL Version 11.1.0.7.0 - Production

    I just re-read the documentation of the subquery factoring clause of select again and I saw no restriction that applies.

    Can someone help me understand why I get different results?  I would like to be able to use the statement that creates spades3, but for some reason it does not work.  However, when I break up and store the last subquery TMP in a table (MAT1), I am able to get the expected results in MAT2.

    Sorry if the example seems a bit esoteric.  I tried to put something together to help illustrate another problem, so it was more convenient to use the same instructions to illustrate this problem.

    drop table mat1;
    create table mat1 as
    with skus as (
      select level as sku_id
      from dual
      connect by level <= 1000
      ),
      tran_dates as (
      select to_date('20130731', 'yyyymmdd') + level as tran_date
      from dual
      connect by level <= 31
      ),
      sku_dates as (
      select s.sku_id,
      t.tran_date,
      case when dbms_random.value * 5 < 4
      then 0
      else 1
      end as has_changes,
      round(dbms_random.value * 10000, 2) as unit_cost
      from skus s
      inner join tran_dates t
      on 1 = 1
      )
    select d.sku_id,
      d.tran_date,
      d.unit_cost
      from sku_dates d
      where d.has_changes = 1
    ;
    
    
    drop table mat2;
    create table mat2 as
    select m.sku_id,
      m.tran_date,
      m.unit_cost,
      min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
      from mat1 m
    ;
    
    
    drop table mat3;
    create table mat3 as
    with skus as (
      select level as sku_id
      from dual
      connect by level <= 1000
      ),
      tran_dates as (
      select to_date('20130731', 'yyyymmdd') + level as tran_date
      from dual
      connect by level <= 31
      ),
      sku_dates as (
      select s.sku_id,
      t.tran_date,
      case when dbms_random.value * 5 < 4
      then 0
      else 1
      end as has_changes,
      round(dbms_random.value * 10000, 2) as unit_cost
      from skus s
      inner join tran_dates t
      on 1 = 1
      ),
      tmp as (
      select d.sku_id,
      d.tran_date,
      d.unit_cost
      from sku_dates d
      where d.has_changes = 1
      )
    select m.sku_id,
      m.tran_date,
      m.unit_cost,
      min(m.tran_date) over (partition by m.sku_id order by m.tran_date rows between 1 following and 1 following) as next_tran_date
      from tmp m
    ;
    
    
    select count(*) from mat2;
    select count(*) from mat3;
    
    
      from tmp m
    ;
    

    select count(*) from mat2;
    select count(*) from mat3;
    

    Select count (*) from mat2;

    COUNT (*)

    ----------

    31000

    Executed in 0,046 seconds

    Select count (*) in spades3;

    COUNT (*)

    ----------

    0

    Executed by 0,031 seconds

    Hello

    Khaled says:

    The problem is with

    -case when dbms_random.value * 5<>

    You can change this

    dbms_random. Value (1,100) * 5<>

    and test

    I don't know that I agree 100% with your police department, work there.

    dbms_random. Value, no arguments, returns a random number between 0 (inclusive) and 1 (exclusive), so

    dbms_random. Value * 5< 4 ="" will="" be="" true="" about="" 80%="" of="" the="">

    dbms_randon. Value (1, 100) returns a random number between 1 (included) and 100 (exclusive), so

    dbms_random. Value (1, 100) * 5< 4="" will="" be="" true="" exactly="" 0%="" of="" the="" time,="" which="" is="" not="" what="" op="" wants="" at="">

    The problem here apparently revolves around the optimizer not really call dbms_random over and over again in sku_dates of the subquery.  Add ROWNUM in the subquery seems to force Oracle to assess dbms_random.value immediately.  I don't really understand myself, but adding 1 element in the SELECT of the sku_dates subquery clause could solve the problem:

    create table spades3 as

    with references like)

    Select the level as sku_id

    of the double

    connect by level<=>

    ),

    tran_dates like)

    Select the level + to_date ('20130731', 'YYYYMMDD') as tran_date

    of the double

    connect by level<=>

    ),

    sku_dates like)

    Select s.sku_id,

    t.tran_date,

    -case when dbms_random.value * 5<>

    then 0

    1 other

    end as has_changes,

    Round (dbms_random.value * 10000, 2) as unit_cost

    ROWNUM AS r-<=== new="" column="" added="" here="" new="" column="" added="">

    s SKUs

    inner join tran_dates t

    1 = 1

    ),

    tmp as)

    Select d.sku_id,

    d.tran_date,

    d.unit_cost

    of sku_dates d

    where d.has_changes = 1

    )

    Select m.sku_id,

    m.tran_date,

    m.unit_cost,

    min (m.tran_date) over (partition by order of m.sku_id by m.tran_date rows between 1 next and 1 suite) as next_tran_date

    of tmp m

    ;

    You do not have to refer to this column anywhere; just having him in the subquery is enough.

    If someone can't understand why, I know it.

  • ITunes has the same band appears twice, how can I merge them?

    My favorite band was divided by itunes for some reason any two identical bands.  One of them has 10 albums, and the other has one on his own.  The weird part is some of the songs from the album one with the ten other albums where they should be, while the rest are in this band twice on their own.

    How can I merge these songs in the rest of the band while the album is finished?

    What works in general is to select all the associated tracks and use read information to add say leaking X for each of the areas that the slopes have in common, for example, an album; Album, Album artist, and artist (if the same for all the tracks), or for an artist; Album artist (and an artist , as applicable). Apply the change which merges things together, then remove the excess characters. See grouping in albums, titles for help if necessary.

    TT2

  • Hello, I can not put my project on the same file as before. Can someone help me?

    Hi all

    I have problems with saving my project. Whenever I try to register, I have this error message. My project is not new (2weeks) and I never had this message before. Basically, he says that it is impossible to open the file with write permission. The file may be locked or that I don't have permission to write to the server. And I have to save my project as a new location.

    Capture.PNG

    I checked with the technical team and I have full access (in writing and all). All links to my project files are in the same folder.

    I'm trying to save my project in another place and I still have the same error message.

    I uninstalled first Pro CC and reinstalled and it doesn't change, I still got the same message.

    I search the web and I have not found an answer.

    I use Windows 7 Pro, Intel I7, 20 GB of RAM and a Quadro 410.

    Does anyone have an idea on how I can save my file without this message?

    Thank you

    PS: Sorry for my English.

    Your department might have to examine how the body communicates with the server, or how the server see the Agency.  Could not see the link, it's that you access and if it is blocked.  HE could have instead an exception, or create a new rule.  It could also be the firewall?

  • message reads: your computer must be restarted to complete a previous update of Firefox. Do you want to restart now? After each restart, the same message appears and I can't open Firefox

    My office uses Windows 7 Professional, as shown above, that I need advice how to open Firefox. I tried to uninstall Firefox, but the same message, which is stated above and multiple reboots fail

    I had the same problem after trying an update of Firefox 7.x to version 9.0.1.
    I deleted the file named "firefox.exe.moz - upgrade" from the folder C:\Program Files (x 86) \Mozilla Firefox. After that, I was able to uninstall Firefox from the control panel. After the reboot, I was able to install Firefox 9.0.1 and run it without error.
    My OS is Windows 7 Professional 64-bit. I was installing the UK English version (I got the U.S. English version above).

  • I have two files of the same name in the same directory. If it can happen?

    I've attached a screenshot of this. This isn't a photoshop, I always have that. There are two folders named my videos in the same directory T. However when I look through the window run, typing T:\, it showed my videos and videos such as fall down in this directory...

    One of the record is fair 'videos' but configured (in desktop.ini) appears as "my videos."

Maybe you are looking for