Takes patience and knowledge, but please help me with this function

I know I am asking for a lot, but I've been sitting over this for hours and can't crack it.
I got these two tables
create table orders create table products
(order_no number, (item_no number (4)
item_no number(4), tem_name varchar2(50)
order qta number, price number,
order_date date supplier_id char(3));
delivered date)
I am trying to create a function that would give me owed amount for delayed delivery per order. Companies have 30 days to deliver the goods, otherwise they pay penalty 1% of order price for each day of delay.
I cooked up this and it's giving me results only in case there is one row per order in orders table. Like this:
ORDER_NO ITEM_NO ORDER_QTA ORDER_DATE RECEIVED
1002 2 90 29.01.10 01.05.10
but the moment there is more than one row in orders (like below) I execute the function and get the error message - ORA-01422: exact fetch returns more than requested number of rows
ORDER_NO ITEM_NO ORDER_QTA ORDER_DATE RECEIVED
1000 26 82 30.03.10 NULL
1000 14 35 30.03.10 NULL
I've written this and it contains a GROUP BY clause and sums the rows. I don't know why I am still getting the error message.PLS HELP!!
THANKS,
JANA
create or replace function debts (aorder_no in number)
return number is
amount number;
rec date;
begin
select received into rec from orders where order_no=aorder_no;
if rec is not null then
select (to_date(received,'DD-MM-YYYY')-to_date(order_date,'DD-MM-YYYY')-30)* sum((o.order_qta*p.price)*0.01)
into amount
from orders o, products p
where p.item_no = o.item_no
and o.order_no=aorder_no
group by (to_date(received,'DD-MM-YYYY')-to_date(order_date,'DD-MM-YYYY')-30);
return (amount);
end if;
if rec is null then
select (to_date(sysdate,'DD-MM-YYYY')-to_date(order_date,'DD-MM-YYYY')-30)* sum((o.order_qta*p.price)*0.01)
into amount
from orders o, products p
where p.item_no = o.item_no
and o.order_no=aorder_no
group by (to_date(sysdate,'DD-MM-YYYY')-to_date(order_date,'DD-MM-YYYY')-30);
return (amount);
end if;
end;
/

Sorry,
I' m new to this forum and to PL/SQL as well, so I am probably not posting these questions correctly . I am using 10g Ex edition. Here is some sample data.
when I created the function, it was complied ok.
I execute it on order_no 1001 and everything went fine, but if I execute it on order_no 1000, I get an error message. I think it has something to do with the SQL grouping in my statment, but I'm too unexperienced to figure it out on my own.
insert into orders values (     1000     ,     26     ,     25     ,'     30-Mar-10     ,     4-Apr-10     );
insert into orders values (     1000     ,     14     ,     31     ,'     30-Mar-10     ,     1-May-10     );
insert into orders values (     1001     ,     9     ,     39     ,'     28-Feb-10     ,     null     );
insert into orders values (     1002     ,     2     ,     95     ,'     29-Jan-10     ,     10-Dec-09     );
insert into orders values (     1003     ,     23     ,     2     ,'     6-Apr-10     ,     null     );
insert into orders values (     1004     ,     16     ,     70     ,'     8-Jun-04     ,     null     );
insert into orders values (     1005     ,     4     ,     76     ,'     6-May-10     ,     null     );
insert into orders values (     1005     ,     19     ,     86     ,'     6-May-10     ,     null     );
insert into orders values (     1006     ,     29     ,     86     ,'     8-Apr-10     ,     null     );
insert into orders values (     1006     ,     30     ,     55     ,'     8-Apr-10     ,     10-Mar-10     );
insert into orders values (     1007     ,     22     ,     54     ,'     6-Apr-10     ,     null     );
insert into orders values (     1008     ,     9     ,     54     ,'     23-Jan-10     ,     null     );
insert into orders values (     1009     ,     16     ,     22     ,'     5-Apr-10     ,     null     );
insert into orders values (     1010     ,     7     ,     53     ,'     2-Mar-10     ,     null     );
insert into products values (     1     ,'     product_name1'     ,     61000     ,'     EU1'     );
insert into products values (     2     ,'     product_name2'     ,     46000     ,'     EU1'     );
insert into products values (     3     ,'     product_name3'     ,     80000     ,'     EU2'     );
insert into products values (     4     ,'     product_name4'     ,     15000     ,'     EU3'     );
insert into products values (     5     ,'     product_name5'     ,     10000     ,'     EU2'     );
insert into products values (     6     ,'     product_name6'     ,     12000     ,'     EU1'     );
insert into products values (     7     ,'     product_name7'     ,     73000     ,'     EU3'     );
insert into products values (     8     ,'     product_name8'     ,     59000     ,'     EU2'     );
insert into products values (     9     ,'     product_name9'     ,     31000     ,'     EU3'     );
insert into products values (     10     ,'     product_name10'     ,     80000     ,'     EU1'     );
insert into products values (     11     ,'     product_name11'     ,     66000     ,'     EU2'     );
insert into products values (     12     ,'     product_name12'     ,     62000     ,'     US1'     );
insert into products values (     13     ,'     product_name13'     ,     75000     ,'     EU1'     );
insert into products values (     14     ,'     product_name14'     ,     32000     ,'     EU2'     );
insert into products values (     15     ,'     product_name15'     ,     44000     ,'     US1'     );
insert into products values (     16     ,'     product_name16'     ,     68000     ,'     EU1'     );
insert into products values (     17     ,'     product_name17'     ,     28000     ,'     US1'     );
insert into products values (     18     ,'     product_name18'     ,     10000     ,'     EU2'     );
insert into products values (     19     ,'     product_name19'     ,     69000     ,'     US2'     );
insert into products values (     20     ,'     product_name20'     ,     24000     ,'     US1'     );
insert into products values (     21     ,'     product_name1'     ,     40000     ,'     US3'     );
insert into products values (     22     ,'     product_name2'     ,     95000     ,'     US3'     );
insert into products values (     23     ,'     product_name3'     ,     31000     ,'     US1'     );
insert into products values (     24     ,'     product_name4'     ,     100000     ,'     US2'     );
insert into products values (     25     ,'     product_name5'     ,     64000     ,'     US2'     );
insert into products values (     26     ,'     product_name6'     ,     91000     ,'     US1'     );
insert into products values (     27     ,'     product_name7'     ,     65000     ,'     US3'     );
insert into products values (     28     ,'     product_name8'     ,     86000     ,'     US2'     );
insert into products values (     29     ,'     product_name9'     ,     62000     ,'     US3'     );
insert into products values (     30     ,'     product_name10'     ,     13000     ,'     US3'     );

Similar Messages

  • I bought a movie the movie Godzilla 2014 recently but it didn't show up in my library. I went check the itunes store and it wants me to buy it again. Please help me with this problem.

    I just bought this movie "Godzilla 2014" but it won't show in my Movie Library. I closed my Itunes and put it back on again but still won't show up. I checked my purchased list and it shows that I recently bought the movie but when I checked the itunes store it wants to buy the movie again. Please help me with this right away Apple.

    Apple Store Customer Service at 1-800-676-2775 or visit online Help for more information.
    To contact product and tech support: Contacting Apple for support and service - this includes
    international calling numbers..
    For Mac App Store: Apple - Support - Mac App Store.
    For iTunes: Apple - Support - iTunes.

  • I have unlocked my iphone and when tried to upgrade it, i got an error, so i restored it.But i was not able to activate my iphone 3gs now...it is showing a message like ' sim card not found'..please help me with this regard

    I have unlocked my iphone and when tried to upgrade it, i got an error, so i restored it.But i was not able to activate my iphone 3gs now...it is showing a message like ' sim card not found'..please help me with this regard

    Try popping out the SIM card and turn off the phone.
    Pop in the SIM card once again and turn on the phone, make sure that the SIM card is placed and seated perfectly in the tray!
    Tell me how did you unlock your phone!?

  • HT201487 i have a problem with xcode programming app when i lunch to it it asking me to enter my password and when i enter it the program show that it is wrong but am 100% sure it's wright so please help me with this issue thanks

    i have a problem with xcode programming app when i lunch to it it asking me to enter my password and when i enter it the program show that it is wrong but am 100% sure it's wright so please help me with this issue thanks

    That's not very intuitive
    Check your mail server setup (mail>preferences>accounts>) choose your MobileMe account and select Outgoing Mail Server (SMTP) dropdown box, select Edit SMTP server list, verify that each instance of the me server has a password, if there are more than one and you only have one account then delete the one without a password.

  • I bought(own) and gifted(friend) an app which i was charged 2 different prices please help me with this!!!

    I bought(own) and gifted(friend) an app which i was charged 2 different prices please help me with this!!!

    Apple does not sell an unlocked iPhone. Some cell carriers offer the option to purchase an unlocked phone at a significantly higher price. Your friend could go to Hong Kong and buy an unlocked phone from an authorized carrier but if he has any problems with the phone, he will need to take it back to the country where he purchased it.

  • Hello, Honestly I just updated my 4s and my iPad 3 to iOS 6 and when try to press on the Music app or the iTunes app it says "cannot connect to iTunes Store" Could you please help me with this thank you so much, Charbel from Lebanon

    Hello, Honestly I just updated my 4s and my iPad 3 to iOS 6 and when try to press on the Music app or the iTunes app it says "cannot connect to iTunes Store" Could you please help me with this thank you so much, Charbel from Lebanon

    See these previous discussions help.
    App Store Updates (but only Updates)...: Apple Support Communities
    Apps suddenly don't update: Apple Support Communities

  • Hi, last month i bought the ms bundle knowing it would help me with my office works..but didnt seem to work as i thought please help me with this, i didnt pay 51 singapore dollar for nothing please get me back asap

    Hi, last month i bought the ms bundle knowing it would help me with my office works..but didnt seem to work as i thought please help me with this, i didnt pay 51 singapore dollar for nothing please get me back asap

    Does the iPod connect to other networks?
    If you restored to factory settings/new iPod and still does not connect to any network that indicates a hardware problem.
    If you only can't connect to your network that indicates a problem with your network.
    Make an appointment at the Genius Bar of an Apple store..
    Apple Retail Store - Genius Bar

  • I have a red vertical line on the right side of my screen and it will not go away tried restarting my computer and that didn't help at all, please help me with this problem it is kind of annoying

    I have a red vertical line on the right side of my screen and it will not go away tried restarting my computer and that didn't help at all, please help me with this problem it is kind of annoying

    If it's a thin 1 pixel wide vertical line it's highly likely it could be caused by a defective LCD.
    One quick basic way to check is to note the position of the line. Then go to System Preferences, Displays, and lower the resolution. If ther line moves it's on the video and the logic board or video card if fitted is likely to be defective. If the line stays in the same position it's likely to be an LCD fault. Either way to have it repaired you'll need to visit an Apple store or AASP.
    The worst offender for this problem is the Late 2006 17" iMac. FOC replacement of the LCD used to be covered by a quality program, but it's now ended.
    Steve

  • I recently upgraded to IOS 10.9.5 and now I can't export anything from final cut Pro X. Could somebody please help me with this?

    I recently upgraded to IOS 10.9.5 and now I can't export anything from final cut Pro X. Could somebody please help me with this?

    SSign in to the App Store using the Apple ID that was used to purchase the software.

  • HT201318 I tried to downgrade my iCloud and when i did it never refunded me can you please help me with this thank you James

    I tried to downgrade my iCloud and when i did it never refunded me can you please help me with this thank you James
    <Personal Information Edited by Host>

    How did you try to downgrade
    (Don't post your telephone number in public places, unless you need more useless phone calls) I will ask for it to be removed.

  • I brought a iTunes card and the the code on the back is not there it has faded away can u please help me with this problem

    i brought a iTunes card and the the code on the back is not there it has faded away can u please help me with this problem

    Hi Daniel ...
    Try here > iTunes Store: Invalid, inactive, or illegible codes

  • HT1296 i cannot transfer some videos to my iphone 3gs. the file is in MPEG4 format. i can view in itunes but cannot be transfered to iphone. Please help me with this problem.

    i cannot transfer some videos to my iphone 3gs. the file is in MPEG4 format. i can view in itunes but cannot be transfered to iphone. Please help me with this problem.

    In iTunes, Advanced>Create iPod or iPhone version

  • I've bought the first season of Death Note on itunes, and i've was never able to download the 11th episode (Assault), it gives me error -50 each time I try. Can anyone please help me with this?

    the title pretty much has my question i guess. it's my first post so i'm not sur if you'll see all of it. I'll just copy paste it anyhow.
    i've bought the first season of Death Note on itunes, and i've was never able to download the 11th episode (Assault), it gives me error -50 each time I try. Can anyone please help me with this?

    Perhaps try the "Error -50," "-5000," "8003," "8008," or "-42023" section in the Specific Conditions and Alert Messages: (Mac OS X / Windows) section of the following document:
    iTunes: Advanced iTunes Store troubleshooting

  • Hi Everyone...Please help me with this query...!

    Please Help me with this doubt as I am unable to understand the logic...behind this code. It's a begineer level code but I need to understand the logic so that I am able to grasp knowledge. Thank you all for being supportive. Please help me.
    //Assume class Demo is inherited from class SuperDemo...in other words class Demo extends SuperDemo
    //Volume is a method declared in SuperDemo which returns an integer value
    //weight is an integer vairable declared in the subclass which is Demo
    class Example
         public static void main(String qw[])
              Demo ob1=new Demo(3,5,7,9);
    //Calling Constructor of Demo which takes in 4 int parameters
              SuperDemo ob2=new SuperDemo();
              int vol;
              vol=ob1.volume();
              System.out.println("Volume of ob1 is " + vol);
              System.out.println("Weight of ob1 is " + ob1.weight);
              System.out.println();
              ob2=ob1;
    }Can someone please make me understand --- how is this possible and why !
    If the above statement is valid then why not this one "System.out.println(ob2.weight);"
    Thanks Thanks Thanks!

    u see the line wherein I am referencing the objectof
    a subclass to the superclass...right? then why we
    can't do System.out.println(ob2.weight)?You need to distinguish two things:
    - the type of the object, which determines with the
    object can do
    - the type of the reference to the object, which
    determines what you see that you can do
    Both don't necessarily have to match. When you use a
    SuperDemo reference (obj2) to access a Demo instance
    (obj1), for all you know, the instance behind obj2 is
    of type SuperDemo. That it's in reality of type Demo
    is unknown to you. And usually, you don't care -
    that's what polymorphism is about.
    So you have a reference obj2 of type SuperDemo.
    SuperDemo objects don't have weight, so you don't see
    it - even though it is there.So from ur explanation wat I understand is - Even though u reference a subclass object to a superclass, u will only be able to access the members which are declared in the superclass thru the same...right
    ?

  • Query Issue in Creating a View....Please help me with this query..

    I would like to create a view on this table with four columns
    1. PN#_EXP_DATE
    2. PN#
    3. PN#_EFF_DATE
    4. PN#
    P_S_C     A_C     P     EXP_DT
    21698     13921     1     5/29/2009 3:15:41 PM     
    21698     13921     1     5/29/2009 3:54:57 PM     
    21698     1716656     4     5/29/2009 3:15:41 PM     
    21698     3217     3     5/29/2009 3:15:40 PM     
    21698     3217     3     5/29/2009 3:54:57 PM     
    21698     60559     2     5/29/2009 3:15:41 PM     
    21698     60559     2     5/29/2009 3:54:57 PM     
    I have a trigger on A, which inserts the DML records into B. (Table A and B structure is the almost the same,
                                       where table B will have one extra column exp_dt)
    NOw Table B can have records with same P_S_C and A_C columns and exp_dt will capture the history.
    for example: from the above sample data, let us take first two records..
    P_S_C     A_C     P     EXP_DT
    21698     13921     1     5/29/2009 3:15:41 PM     --- Record 1
    21698     13921     1     5/29/2009 3:54:57 PM     --- Record 2
    from this..
    Note: 1. Table A and Table C can be joined using A.P_S_C and C.R_C to get the start_date.
    2. PN# comes from table D. It contains numbers for the resp. weeks.
    3. PN#_EFF_DATE is the previous immediate previous date for that record in history table (Table B).
    I wanted the data like..
    ---- this is for the second record in the above..
    PN#_EXP_DATE PN# PN#_EFF_DATE PN#
    5/29/2009 3:54:57 PM     214 5/29/2009 3:15:41 PM     214
    ---- this is for the first record in the above..
    PN#_EXP_DATE PN# PN#_EFF_DATE PN#
    5/29/2009 3:54:41 PM     214 ( for this we should query the table C to get the
                        start_date, for this combinatation of P_S_C and A_C in table B
                             where B.P_S_C = C.A_C
                             and that value should be the EFF_DT for this record)
    Please help me with this....
    Thanks in advance..

    Hi All,
    select d.P# as "PN#_EXP_DATE", exp_date
    from daily_prd d, cbs1_assoc_hist b
    where to_char(d.day_date,'MM/DD/YYYY') = to_char(b.exp_date,'MM/DD/YYYY')
    and d.period_type = 'TIMEREPORT';
    This above query gives the output as follows:
    pn#_exp_date exp_date
    214     5/29/2009 3:15:40 PM
    214     5/29/2009 3:15:41 PM
    214          5/29/2009 3:15:41 PM
    214          5/29/2009 3:15:41 PM
    214          5/29/2009 3:54:57 PM
    214          5/29/2009 3:54:57 PM
    214          5/29/2009 3:54:57 PM
    This below is the data from history table (Table B).
    P_S_C     A_C PLACE EXP_DATE
    21698          3217          3     5/29/2009 3:15:40 PM     
    21698          13921          1     5/29/2009 3:15:41 PM     
    21698          1716656          4     5/29/2009 3:15:41 PM     
    21698          60559          2     5/29/2009 3:15:41 PM     
    21698          13921          1     5/29/2009 3:54:57 PM     
    21698          3217          3     5/29/2009 3:54:57 PM     
    21698          60559          2     5/29/2009 3:54:57 PM     
    I got the pn#_exp_date from the Table 'D', for the given exp_date from Table B.
    My question is again....
    CASE - 1
    from the given records above, I need to look for exp_date for the given same P_S_C and A_C.
    in this case we can take the example...
    P_S_C     A_C PLACE EXP_DATE
    21698          3217          3     5/29/2009 3:15:40 PM
    21698          3217          3     5/29/2009 3:54:57 PM
    In this case, the
    pn#_exp_date exp_date     pn#_eff_date eff_date
    214     5/29/2009 3:15:57 PM     < PN# corresponding to the     5/29/2009 3:15:40 PM
                        eff_date .>
                        <Basically the eff_date is
                        nothing but the exp_date only.
                        we should take the immediate before one.
    In this case, our eff_date is '5/29/2009 3:15:40 PM'.
    but how to get this.
    CASE - 2
    from the above sample data, consider this
    P_S_C     A_C PLACE EXP_DATE
    21698     1716656     4     5/29/2009 3:15:41 PM
    In this case, there is only one record for the P_S_C and A_C combination.
    The expected result :
    pn#_exp_date exp_date               pn#_eff_date eff_date
    214     5/29/2009 3:15:41 PM     < PN# corresponding to the     5/29/2009 3:15:40 PM
                        eff_date .>
                   <Basically the eff_date is
                   nothing but the exp_date only.
                        we should take the immediate before one.
    In this case to get the eff_date, we should query the Table 'C', to get the eff_date, which is START_DT column in this table.
    for this join B.P_S_C and C.R_C.
    Hope I am clear now..
    Thanks in advance.....

Maybe you are looking for

  • How to change the view criteria at run time for af:query

    Hi, I've a usecase where I need to change the view criteria of the af:query at run time. Use case: ======= 1) Consider a check box (Show Emps Under Dept 10) in the query panel when user selects and clicks 'Search' button should show the employees und

  • System Measurement - Error while exporting to LAW file

    Hi all, We have the SAP ERP 6.0. We started the system measurement on our ECC landscape with the aim to consolidate the results with LAW in Solution Manager. We performed the measurement in our production server and we select System Measurement - Exp

  • Fork and Muliple Conditions

    Hi Experts, What is the difference between the Fork and Muliple Conditions? Thanks.

  • Counter Output with NI cDAQ 9188

    I am trying to generate pulses with the counters included in the NI 9188 cDAQ chassis. I tried first to do that with NI 9411 module but this task didn't figure out in MAX. I'm trying now to perform that with the PFI 0 BNC connexion in the chassis but

  • Production date to be entered for existing materials

    Hi all, Is there any standard tcode/procedure for entering the Production date/SLED for the existing materials whose GR is already done.The client don't want to cancel the old GR and do a fresh entry.' Please do suggest me a better solution to the ab