Nested subquery, using items from other part of query

Hi All,
I have a tricky problem and I will try explain as best I can:
DB version is 11.2.0.3.0
create table product_list (product_id number,
                project_name varchar2(30))
create table products (product_id number,
                project_desc varchar2(30))
create table total_counts (product_id number,
                total_count number,
                project_name varchar2(30))
create table fixed_counts (product_id number,
                fixed_count number,
                project_name varchar2(30).
                    fixed_date date)
create table product_rating (product_id number,
                rating number,
                quarter_last_updated number)
create table quarters (quarter_id number,
                quarter_end_date date)
insert into product_list values (1, 'Prod 1');
insert into product_list values (2, 'Prod 2');
insert into product_list values (3, 'Prod 3');
insert into products values (1, 'Prod 1');
insert into products values (2, 'Prod 2');
insert into products values (3, 'Prod 3');
insert into total_counts values (1, 2000, 'Prod 1');
insert into total_counts values (2, 1000, 'Prod 2');
insert into total_counts values (3, 500, 'Prod 3');
insert into fixed_counts values (1, 1, 'Prod 1', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (1, 3, 'Prod 1', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (2, 50, 'Prod 2', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (2, 2, 'Prod 2', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (2, 3, 'Prod 2', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (2, 3, 'Prod 2', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (3, 8, 'Prod 3', to_date('01/01/2013','DD/MM/YYYY'));
insert into fixed_counts values (3, 3, 'Prod 3', to_date('01/03/2013','DD/MM/YYYY'));
insert into product_rating values (1, 1, 1);
insert into product_rating values (3, 2, 2);
insert into quarters values (1, to_date('01/10/2012','DD/MM/YYYY'));
insert into quarters values (2, to_date('01/02/2013','DD/MM/YYYY'));My current query effectively joins 2 tables 'TOTAL_COUNTS' and 'FIXED_COUNTS'. The FIXED_COUNTS table is updated daily when one of the products has a fix made against it and the query outputs a list of all Products and their percentage "fixed" rate (used in an APEX Interactive report).
select A.PRODUCT, A.TOTAL, B.FIXED, a.PROD_ID, round((FIXED/TOTAL) * 100,  2) percent
from (
select sum(a.total_count) TOTAL, a.product_id PROD_ID, d.Product_desc PRODUCT
from total_counts a, product_list c, products d
where a.product_id = c.product_id
and lower(a.project_name) = lower(c.project_name)
and c.product_id = d.product_id
group by a.product_id, d.product_desc
) A
JOIN
select sum(b.fixed_count) FIXED, b.product_id PROD_ID, d.Product_desc PRODUCT
from fixed_counts b, product_list c, products d
where b.product_id = c.product_id
and lower(b.project_name) = lower(c.project_name)
and c.product_id = d.product_id
group by b.product_id, d.product_desc
) B
on A.PROD_ID = B.PROD_ID and A.PRODUCT = B.PRODUCT This gives me an output like:
PRODUCT     TOTAL     FIXED     PROD_ID     percent
Prod 1     2000     4     1     0.2
Prod 2     1000     58     2     5.8
Prod 2      500     11     3     2.2The query works fine and does exactly the job I require. However, I now have a requirement that when a product gets a "fixed" percentage rate over 1% the product gets a point added against it in a "product_rating" table and i have to recalculate the fixed percentage from the date the point was added (if no point added we take all "fixed").
I have added 2 new tables for this: "product_rating" and "quarters". The "product_rating" table contains the Product_id and the points it has accumulated so far. It is updated once a quarter. The "quarters" table just holds the dates for us to use for the new calculations.
So effectively I need to replace this:
sum(b.fixed_count) FIXEDwith something like this:
if a product has a point against it in the "product_rating" table then we just show fixes from the date the point was added i.e.
select sum(b.fixed_count) from fixed_counts b, product_rating c, quarters d
where b.product_id = c.product_id and c.quarter_last_updated = d.quarter_id
and b.product_id = PROD_ID
and b.fixed_date > (select quarter_end_date from quarters where quarter_id = the_last_quarter_updated)Based on the values in the tables above the percentages for 'Prod 3' should now be 0.6. This is because it received a product_rating on quarter 2 so the new calculation is based on all "fixes" after this date (only the last one on 01-MAR-13').
I have tried numerous ways of embedding this in the current query but I cannot get it to work. Sorry for not adding the complete table info but the total_counts and fixed_counts tables have much more columns that I removed from the query so it would be easier to view. Thanks in advance for your help. If I can add anymore info please ask.
Tom
Edited by: TomH on May 14, 2013 8:54 AM
Edited by: TomH on May 14, 2013 8:58 AM

TomH wrote:
of course youre right, thanks.
I have added some table info and data that will hopefully make it a bit easier to understand.Okay, thanks ... in the future though please try and run the commands yourself to ensure they work. What you provided is close, but it didn't run straight away without some modifications.
I'll start with expressing my concerns for your data model, it seems .... wrong, for lack of a better word. Even given that you said you stripped out a bunch of stuff to make the example. Do you have anyone you work with that can review the model for you?
As for the query, this isn't pretty (I'm in a bit of a rush here) so you can probably clean it up considerably. I just tried to demonstrate the basics of what you're going to have to do.
ME_XE? with base_data as
  2  (
  3     select
  4        b.fixed_date,
  5        b.fixed_count,
  6        b.product_id ,
  7        d.project_desc
  8     from fixed_counts b, product_list c, products d
  9     where b.product_id = c.product_id
10     and lower(b.project_name) = lower(c.project_name)
11     and c.product_id = d.product_id
12  ),
13     final_base_data as
14  (
15     select
16        b.product_id      PROD_ID,
17        b.project_desc    PRODUCT,
18        sum
19        (
20           case
21              when b.fixed_date >= stuff.quarter_end_date or stuff.quarter_end_date is null
22              then
23                 b.fixed_count
24              else
25                 0
26           end
27        )  as FIXED
28     from base_data b
29     left outer join
30     (
31        select
32           pr.product_id,
33           qr.quarter_end_date
34        from product_rating pr , quarters qr
35        where pr.quarter_last_updated = qr.quarter_id
36     )  stuff
37     on (stuff.product_id = b.product_id)
38     group by b.product_id, b.project_desc
39  )
40  select A.PRODUCT, A.TOTAL, B.FIXED, a.PROD_ID, round((b.FIXED/a.TOTAL) * 100,  2) percent
41  from
42  (
43     select sum(a.total_count) TOTAL, a.product_id PROD_ID, d.project_desc PRODUCT
44     from total_counts a, product_list c, products d
45     where a.product_id = c.product_id
46     and lower(a.project_name) = lower(c.project_name)
47     and c.product_id = d.product_id
48     group by a.product_id, d.project_desc
49  ) A
50  JOIN final_base_data B
51  on A.PROD_ID = B.PROD_ID and A.PRODUCT = B.PRODUCT
52  ;
PRODUCT                                     TOTAL              FIXED            PROD_ID            PERCENT
Prod 1                                       2000                  4                  1                 .2
Prod 2                                       1000                 58                  2                5.8
Prod 3                                        500                  3                  3                 .6
3 rows selected.Cheers,

Similar Messages

  • Not receiving email notifications of iCal event invitations from other parties.

    I am not receiving emails for iCal event invitations from other parties, but the invitations ARE in my iCal Inbox. I don't use iCal often, so I never proactively go in to check my Inbox there, but rather have always depended on receiving the email notification of an event invite to know to add it to my iCal. I can SEND invitations to others successfully with other parties receiving MY email notifications, I'm just not receiving them via email.
    I've now missed two very important meetings because I never received the email invitations to add an event, which were the only forms of correspondence for both. Can anyone help me? Thank you!

    Hello mcdannej,
    While a Verizon employee may occasionally post here, this community is meant mainly for peer-to-peer support. If you need to talk to a Verizon representative, you should contact customer service directly through our Support Page or Contact Us.

  • How do use home sharing to import items from other iTunes library?

    Okay - the help section in iTunes is not helpful.  I am trying to import items from my girlfriend's library and vice versa.  We have home sharing turned on.  I can see her library and she can see mine.  According to the HELP section, I should be able to choose a cateogry i.e. MUSIC.  Then there is supposed to be a "Show" menu at the bottom of the iTunes window and I should choose "items not in my library" then select the items I want to import and click import.
    PROBLEM:  I can choose a category but there is no SHOW Menu at the bottom of the iTunes window which allows me to select items and import them into my library.
    Suggestions???

    Well I guess I am not sure now that you asked. 
    In ITunes - under "Preferences" I have Sharing turned on so it says sharing my library on local network.
    AND
    In ITunes - under "File" I have Home Sharing turned on.
    Does this answer your question?

  • Using APIs from other shema

    I need some special grant to use portal apis from other shema.
    I trying to use wwctx_api.get_user_id API of this way
    declare
    x number;
    begin
    x := portal30.wwctx_api.get_user_id();
    end;
    but dont work, because return this error
    ORA-06510: PL/SQL: unhandled user-defined exception
    ORA-06512: at "PORTAL30.WWCTX_SSO", line 562
    ORA-06510: PL/SQL: unhandled user-defined exception
    ORA-06502: PL/SQL: numeric or value error
    ORA-06512: at "PORTAL30.WWCTX_SSO", line 597
    ORA-06512: at "PORTAL30.WWCTX_API", line 82
    ORA-06512: at line 4
    null

    You need to run provsyns.sql. See the PDK on Portal Studio (PDK-PLSQL) for the download. It's also available on the iAS CD.

  • Get strange audio from other parts of the video when I am using transitions

    In most cases when I add a transition, I get a strange piece of audio inserted during the transition. I know I can go in and manually edit this out, but is there some sort of fix to keep this from happening in the first place?

    Hello, Rick,
    Welcome to the discussions.
    Yes, that is a problem that happens sometimes, especially with the Cross-Dissolve transition. I use that one a lot, and run into this with many of my clips. I have 'fixed' it with a variety of methods including muting, putting music over it, adding audio clips recorded from the beginning of the next clip....for this one I have to delete the transition, crop the next clip, remove it to the clips pane, add back the transition and record the audio from the deleted clip using WiretapPro. Works great and actually is faster than it seems.
    You might be able to get some correction by trashing iMovie's preference file; Close iMovie. Go to YourUserName/Library/Preferences/com.apple.imovie.plist
    If you also have iMovie 8, you will see its plist as com.apple.imovie7.plist, so be sure to trash the correct one.
    Drag the plist file to the trash. Reopen iMovie and see if it works properly now.

  • Use Package from other database

    Hi all,
    I want to use a function from a package which is stored on another database in the mapping of my datawarehouse.
    How can I do that? I do not want to import the function, because then I do not get the changes which could be done to the package on the other database.
    Thanks in advance
    Yvonne

    If I understand it correctly I should create the package with the same name to
    the public transformations. Either to the public or to the transformations local to your project, yes.
    There I then create a transformation in the package. What should I do with the
    implementation? Leave it like the default is?You can leave it empty, as long as the number, order and type of parameters matches that of the actual packaged function in the database you will be calling.
    How I does the OWB now know where he can find the package?That's the trickier part. You need a database link (connector) between your target and the database with the package. Then you can create a synonym, using the database link, in the target schema pointing to the package in the remote database.
    Now, if you named the newly added package in OWB the same as the synonym used to access the package in the remote database AND you named the function in the new package the same as the real function, you should be able to use the transformation in a mapping, deploy it and execute it, without actually deploying the transformation itself.
    Hmm, reading that back, it sounds rather convoluted to me.
    Anyway...I hope it helps.

  • Error while Driving a view using parameters from other view

    Hi,
    I have created a report with streamlist and Barchart view.Driving option is used in streamlist to drive the barchart. The driving works well untill i dont use any filters in the Streamlist.Once i use parameters in streamlist , the driving works only for the default values set.When Second set of values are chosed for the parameter,the values are displayed correctly , but once they are selected for driving,the current values are replaced by the results of the default parameter settings.
    I have already done the tutorials for driving and it worked well untill i use filters in the main view,from where the driving is done.
    I would really appreciate if someone could give a solution for this problem.
    Regards,
    Lathika

    Hi,
    Login as i.e. sys as sysdba. Standard password is change_on_install.
    Or you can login in using user that has CREATE VIEW WITH ADMIN OPTION and then grant CREATE VIEW privilege to user to want.
    Peter D.

  • HP LJ 600 M602 uses paper from other tray

    I have a problem with a new HP LJ 600 M602 printer.
    I think it's best to discribe an example of the problem.
    For example i start a printjob of 1000 pages from tray 1 which holds 500 sheets. Now it should just say tray 1 is empty when it hits the 500 pages printed but it wil automatically continue to print from tray 2. This should not be happening since i turned off the option "Use other tray" on the printer it's self and the web interface. It is also turned off in the printer settings on the computer in word that is used for the printing.
    The actual configuration of the printer is:
    Tray 1: Manual
    Tray 2: blank paper
    Tray 3: paper with corporate logo1
    Tray 4: paper with corporate logo2
    And all have the option "use other tray" turned off.
    Could this be a driver problem or am i missing a setting somewhere?
    Sorry if my english is not 100% it's not my native language, and thanks in advance if you can help  me!

    Hello Jackkemkens
    The product you have is a commercial printer. I suggest posting in the forum for HP Business Support for a better chance at finding a solution.
    You may find the Commercial Laserjet board here.
    http://h30499.www3.hp.com/t5/Printers-LaserJet/bd-p/bsc-413
    Don't forgot to say thanks by giving "Kudos" to those that help solve your problems.
    When a solution is found please mark the post that solves your issue.

  • Error using views from other schema

    Hello,
    i recently had problems using the execute immediate statement in Object Types.
    The SQL statement tried to select from some views which were owned by an other schema.
    Although we had public synonyms on these views and the same dynamic sql could be used out of a stored procedure the usage in the object type raised a "table or view does not exist exception".
    using the same views in the same schema where the object types belonged to, was ok.
    example:
    schema: scott
    view: my_view
    schema: dave
    sql: "select from scott.my.view ....."
    works well from a stored procedure, but nor if used in an object type
    create the view in schema "dave" an then
    sql: "select from my.view ....."
    works fine.
    So is someone aware of this behaviour?
    Is this a bug or a feature?
    regards
    peter

    Hello Geoff,
    here is the sample code.
    If the table my_table belongs to the same
    schema runningthe code the code runs with
    no exception.
    But if the table belongs to an other schema
    (lets say SCOTT),
    only having global synonyms and the rigths to
    select on it, the code will raise an exception "table or view does not exist"
    even if we use SCOTT.my_table in the
    select.
    create type query_o as object(
    dummy integer,
    static function get_query return
    varchar2;
    create or replace type body query_o as
    static function get_query return varchar2
    as
    query varchar2(4000) :=
    'select ' | |
    ' f.amount_month ' | |
    'into :r1 ' | |
    'from ' | |
    ' my_table f ' | |
    'where id=:p1 ';
    begin
    return query;
    end;
    end;
    create type consumer_o as object(
    amount_month number,
    member function find(p_id in integer)
    return consumer_o
    create type body consumer_o as
    member function find(p_id in integer)
    return consumer_o
    as
    l_amount number;
    begin
    execute immediate query_o.get_query()
    using out l_amount,
    in p_id;
    return consumer_o( l_amount );
    end;
    end;
    kind regards
    peetr

  • Use pages from other templates...not just the one you chose.

    Is there a way to choose a page from another template besides the one I started with. For example if I choose the "School Report" template, but want to also use a page from the "Lesson Plan" template, how can I do this. If you click add new page, it only gives you the initial template pages. I tried opening a new template and copying and pasting the page I wanted, but it does not work.

    My guess is that it's because the objects are floating ones.
    If you switch them as inline one, you will be able to copy the page, sometimes after inserting section breaks.
    Yvan KOENIG (from FRANCE mardi 10 février 2009 21:19:34)

  • How to use objects from other SWCs in my BPM Integration Scenario

    Dear all,
    I am experiencing some difficulties with the following. I have created a BPM Integration Scenario in a SWC. It contains a tranformation step in which a message from that SWC is split and mapped to two IDOCs. These IDOCs are imported in anothern SWC which I use for common objects. I have created Abstract Message Interfaces based on these IDOCs in this "COMMON" SWC, but the probIem is that I cannot use them in my scenario in the other SWC. Even though I have a usage dependency (Installation Time) created to the "COMMON" SWC.
    Any suggestions would be much appreciated!!
    Thanks in advance.
    Will happily provide points for good answers.
    Auke

    Thus, I would have to copy/import all objects to the same SWC?
    Or create a separate SWC for BPM Integration scenarios?
    I have tried to create the Abstract Message Interface in the SWC and reference it to the IDOC structure in the "COMMON" SWC. This results in a reference to an invalid object during activation. Even though the defined usage dependency.
    What's the standard solution?
    Cheers,
    Auke
    Message was edited by:
            A. Schotanus

  • Use values from other Excel template on BPC server in BPC report

    Hi Guru's,
    I'm struggling with the following.
    In a BPC report I want to make the rowkeyrange dynamic and centrally adjustable with help of another static excel template placed on the BPC server.
    I've tried the following formula in the rowkeyrange cells:
    =EVSVR()&"/"&EVAST()&"/"&EVAPP()&"/eExcel/"&[ROWKEYRANGE.xlt]Sheet1!A1
    but it keeps reffering to my local file...
    Anyone with ideas how to solve this?
    Regards,
    Vincent Beumer

    I believe that to reference the dynamic template file you can use a relative path from the location of the report file in the user's BPC folder. So something like ./wizard/tempate_file.xlt
    I agree that it would be nice to be able to reference a file that is actually stored on the BPC server, so that it is pulled down to the client only when needed. Maybe we can convince SAP to deliver a function like this
    Ethan

  • If the app I develop for AppStore uses artwork from other sources, and the app is non-commercial, does it have a chance to be approved ?

    My question is stated above, if possible, can anyone answer it ? Thanks a lot in advance.

    I cannot give you a legally backed opinion but I would imagine that if, for example, you were using icons, provided you had permission to use them your App would be acceptable.
    From what I see, there are three distinct types of icons.  Freeware, Licenced ware and original art.  For licenced ware and original art you would certainly need the agreement of the copywrite holder or artist.   For freeware, it would be wise to seek permission in case the situation changes..
    The same would go for pictorial representations.   Unless copywrite has been sold, the artist holds the copywrite and you would need permission to use it.
    I cannot see how the approval of the app would be affected because it would be your responsibility, as the developer, to have obtained all necessary permissions.
    Hope this is of some help.

  • Avchd using audio from other clips

    Hi
    I just reloaded a project from a client to do some adjustments. all files are AVCHD put in 3 different folders. "Media/Footage/Card1/Private (Card2/Private)... you get it.
    Suddenly my audio both in timeline and reference monitor switched source to other clips. I've tried to restart, I tried to use prelude to ingest using prores conversion. if I play it from quicktime everything works fine. this is only a problem with some clip not all. I didn't rename in the folder structure only in the project bins.
    Does anybody know if this is a general problem for CC 2014. I never had this problem with CC version.
    Let me know if anybody struggles with the same issues.
    regards
    Morten

    Hi Morten,
    When you had initially put the project together, did you Import the AVCHD clips with "File > Import" or Media Browser? The latter is preferred and alleviates a lot of discomfort later on. Can't guarantee this is the solution, but doesn't hurt.
    Thanks
    Jeff Pulera
    Safe Harbor Computers

  • Purchasing items from other countries?

    is there any way to purchase something from an itunes store in another country?

    StarDeb55 wrote:
    The other huge hang up is copyright holders do not like cross-border sales. They can't stop you from ordering a CD as an import or going to another country & buying the CD, but they can & do most certainly stop cross-border sales on the web.
    It's not so much that labels dislike cross-border sales - that just adds some complications when calculating album & publishing royalties but it's expected.
    Moreso it has to do with the simple fact of national economies and people's disposable income, to give them an incentive to buy in the home market as opposed to abroad.
    Japan is well known for this. The cost of a CD relative to a person's disposable income is pretty high. So to dissuade the consumer there from buying the US version of a record (which is considerably less expensive) in the US market, the label's local office will typically add two to three bonus tracks to the release to essentially make it "worth their while" to buy in the home market, justifying the increased cost.
    Other markets, like the UK (where the pound-sterling has historically been stronger than the dollar) also do this, but to a lesser degree than Japan. Again, it is to motivate the consumer to contribute to their economy rather than abroad.
    I believe it's the same economic theory when it comes to the digital marketplace (with or without the bonus tracks), but I'll ask a couple of our B.A. VP's here to make sure.
    Steve

Maybe you are looking for