Combined for the subtraction?

I'm having some trouble of mind block with release an integer into a clean set of rows in SQL. Essentially, I in the case of "I need to find where to get 1000 coins. I have a list of 20 suppliers all with different amounts in the warehouses. "Starting with the least expensive, how can I disseminate these." I was not able to find the best way to describe so I think just one example best costumes as below.

WITH PURCH_SOURCE AS (
select 'WICK' PURCH_SOURCE, 0.001 PRICE, 123 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CHN' PURCH_SOURCE, 0.101 PRICE, 2190 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'US' PURCH_SOURCE, 0.041 PRICE, 9 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CAN' PURCH_SOURCE, 0.011 PRICE, 32 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'WRT' PURCH_SOURCE, 0.071 PRICE, 5609 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'BELG' PURCH_SOURCE, 0.891 PRICE, 12 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual
)
select PURCH_SOURCE, PRICE, QTY_ON_HAND, QTY_TO_FULFULL from PURCH_SOURCE
order by price asc

I want the output to look like this:

WITH PURCH_SOURCE AS (
select 'WICK' PURCH_SOURCE, 0.001 PRICE, 123 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CHN' PURCH_SOURCE, 0.101 PRICE, 2190 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'US' PURCH_SOURCE, 0.041 PRICE, 9 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CAN' PURCH_SOURCE, 0.011 PRICE, 32 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'WRT' PURCH_SOURCE, 0.071 PRICE, 5609 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'BELG' PURCH_SOURCE, 0.891 PRICE, 12 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual
)
select PURCH_SOURCE, PRICE, QTY_ON_HAND, QTY_TO_FULFULL,
CASE QTY_ON_HAND 
WHEN 123 THEN 123
WHEN 32 THEN 32
WHEN 9 THEN 9
WHEN 5609 THEN 4179
ELSE 0 END  QTY_TO_TAKE
from PURCH_SOURCE
order by price asc

Please excuse the exits hard but code run corresponds to what I hope to expect.

Ideas, I've tried:

In my view, that it is can be accomplished with the WITH recursive clause or some form of LAG (QTY_TO_FULFILL - QTY_ON_HAND) and have it tumble down.

Something like below but I don't know how make the function analytical in use the 'previous' value is calculated just.

WITH PURCH_SOURCE AS (
select 'WICK' PURCH_SOURCE, 0.001 PRICE, 123 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CHN' PURCH_SOURCE, 0.101 PRICE, 2190 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'US' PURCH_SOURCE, 0.041 PRICE, 9 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'CAN' PURCH_SOURCE, 0.011 PRICE, 32 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'WRT' PURCH_SOURCE, 0.071 PRICE, 5609 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual UNION ALL
select 'BELG' PURCH_SOURCE, 0.891 PRICE, 12 QTY_ON_HAND, 4343 QTY_TO_FULFULL from dual
)
select PURCH_SOURCE, PRICE, QTY_ON_HAND, QTY_TO_FULFULL,
LAG(QTY_TO_FULFULL - QTY_ON_HAND || '   ('||QTY_TO_FULFULL ||' - '||QTY_ON_HAND||')') OVER (ORDER BY PRICE ASC) QTY_TO_TAKE
from PURCH_SOURCE
order by price asc

At this point, I've been watching this one some time and I am looking for any help or suggestion to point me in the right direction.

Params of the environment:

Oracle RAC Exadata 1/8th (2 cases)

Oracle 11.2.0.4

Enterprise Edition

Thanks for your time as always.

Hello

Developer2Be wrote:

I'm having some trouble of mind block with release an integer into a clean set of rows in SQL. Essentially, I in the case of "I need to find where to get 1000 coins. I have a list of 20 suppliers all with different amounts in the warehouses. "Starting with the least expensive, how can I disseminate these." I was not able to find the best way to describe so I think just one example best costumes as below. ...

Do you need to get 1,000 songs?  In the sample results, you seem to 4 343 parts.  Do you really have this number stored on each row in the purch_source table?  That seems like a very bad design.  As the number you get to need is not an attribute of the source, I don't think it belongs in the purch_source table at all.  What will you do if different lines have different values for qty_to_fulfull?  The query below takes the highest value.

You have 20 suppliers, or you have 6?

I think you want something like this:

SELECT qty_to_fulfull, purch_source, price, qty_on_hand

BIGGER (LESS (qty_on_hand

qty_on_hand

+ MAX (qty_to_fulfull) ON)

-SUM (qty_on_hand) (ORDER OF PRICE

purch_source

)

)

0

) AS qty_to_take

OF purch_source

ORDER BY price

purch_source

;

When 2 or more rows have exactly the same price, the above query uses the first purch_source (in alphabetical order) first, but you can use anything you want to as a tiebreaker.

Thanks for posting the sample data in a usable format; This is really useful!

You were on the right track, thinking about analytic functions for this.  You can use the OFFSET to get the total amount of 2 lines, but, in this problem, you need to get the total amount of all the lines so far, not TÉU 2; in other words, you need a running sum, what does the SUM function analytical.

Tags: Database

Similar Questions

  • Value store on button click for the subtraction of the background

    Hi people,

    I am very new to labview so I hope you'll bear with me.

    I am acquiring data on intensity of a camera permanently in a while loop, turning it into a matrix 1024 x 1 and display them in a graph.  I need a way to subtract the background of the camera signal and display the rest.  In a programming language wrote that she would go something like:

    BG = 1024 zeros

    If (Button Pushed)

    BG = current frame

    on the other

    do nothing

    new_sig = current image - bg

    I tried case structures, global variables and shift registers, but none who seem to work.  I am somehow at the end of my rope.  I would like to know if there is any other info, I can provide.

    Thanks for your help!

    Something like that?

  • Key combinations for the wireless on the Dell Latitude XT2 Tablet

    My Dell latitude XT2 Tablet for my wireless is off according to diagnosis. I don't have access to the internet through my ethernet connect.

    The question is what combination of function keys will be switchh wireless back on or what switchh on the side will turn wireless on

    Hi Allofitobeallofitobe,

    I suggest you to refer to this link below and download the Backgrounder learn more about thecombination of function keys for wireless on Dell Latitude XT2 Tablet.

     

    Thank you, and in what concerns:
    I. Suuresh Kumar - Microsoft technical support.

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • Persistent for the subtraction of the background image collection (how to get the maximum value of each pixel in a series of images IMAQ)

    Hi all

    I have a system of LabVIEW which takes advantage IMAQ tools and features of the Vision Development Module.  A useful feature that I put in place is to be able to take a snapshot of the video stream and then subtract this single image among the subsequent images.

    What I want to do is to collect a series of images instead of one, and then create a unique image of these frameworks is just the maximum value of pixels in each pixel (a bit like a display persistent).

    It would probably be very easy to be implemented by converting the image to a table and then by doing math number on the table and then turning into an image, but I hope that someone here may know how to use the IMAQ/VDM tools to do this in a way more compact.

    Thank you very much

    MK

    How about using Max comparison IMAQ operation?

  • Combination for the software package (Adobe Photoshop elements 14 and first 14 elements)

    I bought a combination of Best Buy that includes adobe photoshop elements and Premier elements (14). Do I need to download the software separately? And my access code will work with the software? Thank you

    Yep, you must download separately and use the serial number to activate.

    You can also download the Setup at the bottom of the links files:

    Photoshop Elements:


    Download Photoshop Elements | 10, 12, 14, 11, 13

  • Quick launch of key for the following image loop sequenced?

    Hello

    I want to go fast in my loop of the sequence. Is there a key combination for the next frame?

    Thank you

    CTRL + mousewheel will jump through images in all structures

  • How to perform the subtraction

    I created a mileage reimbursement form using Adobe 10 standard (Adobe X Standard).  The form has an engine Total Miles field, another field for Miles of the home port and a field that must be the difference between these two.  I do not see a calculation for the subtraction function, so my question is double.  One, how can I get the domain to perform the mathematical function and two, how do I know the field is no to enter negative numbers?  I imagine it'll be a custom script that will be loaded into the properties of the field on the tab calculation, under the area of calculation script, but I don't know what would be this script.  Any help would be appreciated.  Thanks to everyone for their help.

    Mileage Reimbursement Form Snip-it.JPG

    You can use this code as the custom calcluation code of the difference field:

    var totalMiles = + this.getField ("Total Miles Driven") .value;

    var homeMiles = + this.getField ("Home Base Miles") .value;

    var totalMiles = diff - homeMiles.

    If (diff<0) diff="">

    Event.Value = diff;

  • I'm looking for the Mac key combination which is equivalent to Alt + Tab in Windows that allows you to choose Open Windows (toggle).

    I'm looking for the Mac key combination which is equivalent to Alt + Tab in Windows that allows you to choose Open Windows (pass) but I can't find this info.  I hope that someone will tell me what combination of keys and as a bonus, where I can find it in the menu help. Thank you!

    The standard keyboard shortcuts are listed here:

    Keyboard shortcuts in Mac - Apple Support

    You can also define your own in System Preferences > keyboard

  • What is the key combination for my Mac on start-up level formats?

    I would first of all the "key start + key combinations" enter the root menu during startup-level of Formate of my Mac mini and install OS X El Capitan new.

    With what combination of keys will start in the start to format the Mac Intosh HDlevel?

    Jona li

    Installer Lion through Mavericks scratching

    Make sure that you back up your files.

    1. Restart the computer and after the buzzer, press on and hold down the COMMAND and R until the Apple logo appears. Wait for the Utility Menu to appear.
    2. Select in the main menu disk utility and click on the continue button.
    3. After disk utility charges select the volume (this is the entry indented, usually Macintosh HD) in the list to one side. Click on the Erase tab in the main window of disk utility.
    4. Define the type of Format Mac OS extended (journaled). Click on the apply button
    5. When the process is complete, quit disk utility and re-enter the Utility Menu.
    6. Select reinstall OS X and click on the continue button.

    This will install the version of Mac OS X, you had installed.

    [Permission to use any part of the foregoing has been granted by khati, exclusively, to theratter.]

  • Portege R700 - registry key for the FN + F8 key combination

    Hello

    Pressing Fn + F8 to toggle wireless on a R700 - what values of registry keys made this change? Is anyone able to tell me the path of the registry for the wireless feature on of the Portege R700?

    See you soon

    :)

    Hey Buddy,

    The exact registry key knows only Toshiba. I can tell you that the FN keys are controlled by value added and Flash Cards Support utility package.

    These two tools can be downloaded from the official website of Toshiba for the FN keys you need an additional tool and it s not just a simple registry key.

    Problems with FN keys combinations?

  • When I click on anything it either in the Outlook Express Preview window to stop answer and I have use the ctrl-alt-del combination for her all over again.

    When I click on anything it either in the Outlook Express Preview window to stop answer and I have use the ctrl-alt-del combination for her all over again. Anyone out there have the same problem? If so what did you do about it.

    Original title: I'm under Oulook Express 6 Windows XP Home SP3 environment.

    Bruce:

    The problem has been fixed (I think) but in a most unusual way. Let me explain.

    When I was closing down to give the computer a rest yesterday, there was a pop up saying that vprotector expected to close. He really wanted the process of closing until I clicked on finish the process. Curious to know what vprotector is, I searched on the net and got a lot of shots, saying: it is a virus and should be deleted pronto. So I searched the hard drive for the vprot. * and found in the folder program files for AVG!

    I then went to AVG to see if it's really a part of AVG and they said yes. If there is a problem with vprot, the toolbar is at fault and needs to be reinstalled. There was one click here to download and run the part of the program to fix the problem. However, I had WebRoot antivirus running that stopped the download of this program as a virus. Because I trust AVG, I've disabled WebRoot and reinstalled the AVG search bar.

    I haven't restarted WebRoot and computer flying along as it should, and OE is the link to IE when I click on a link as it has always done.

    Seems to me to be the faulty AVG toolbar and a cross with two virus programs upward was at fault. Thank you for your time. Your reply gave me a chance to think differently this problem could arise.

    Computers are really complicated machines and we have to be complicated to deal with their problems.

    Thanks again,

    Fred

  • If I have two modems in the same office... windows xp can combine two connections at higher speed dial... my question is can I do the same thing for the dsl if I have two lan cards?

    If I have two modems in the same office... windows xp can combine two connections at higher speed dial... my question is can I do the same thing for the dsl if I have two lan cards?

    Hi cliffwho

    You can fill with connections to the LAN. However I recommend you read this first to understand what it is that you do. I hope this helps.
  • Recovering the year max for combination when the parameters are not exists

    I try to query a table that maintains cumulative information by calendar_year and calendar_period. The problem I encounter is that if I want to ask the total of the transaction for a combination of accounting from a certain calendar_period and calendar_year, I need get the max calendar_year and calendar_period for the previous year or period for the current_year and max calendar_year by combination of accounting (Fund, org prog (, acct) which falls outside the parameters in my where clause, which for this example would be calendar_year = '15' and calendar_period = '03'. For most of all sense I'll post and excerpt from my table.

    create table

    CREATE TABLE "MAXTABLE"
       (    "CALENDAR_YEAR" NUMBER,
        "CALENDAR_PERIOD" NUMBER,
        "AMOUNT" NUMBER,
        "FUND" VARCHAR2(20 BYTE),
        "ACCOUNT" VARCHAR2(20 BYTE),
        "ORG" VARCHAR2(20 BYTE),
        "PROG" VARCHAR2(20 BYTE)
       )
    
    
    
    
    
    
    

    values in the table.

    REM INSERTING into MAXTABLE
    SET DEFINE OFF;
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (15,2,25,'202118','2071','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (15,3,150,'202118','2070','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (14,12,50,'202118','2040','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (14,3,100,'202118','2070','3662','121');
    Insert into MAXTABLE (CALENDAR_YEAR,CALENDAR_PERIOD,AMOUNT,FUND,ACCOUNT,ORG,PROG) values (13,14,50,'202118','2010','3662','121');
    
    
    
    
    
    
    

    so now I want to run a query which would show me the total amounts by combination of accounting (Fund, account, org, prog) and I'd like to get it from calendar_year '15' and calendar_period ' 03 ".

    I need essentially back on the period and the year previous years courses and get the max (calendar_year) and the max (calendar_period) for the Fund, account, org, combination of prog that is not in the calendar_year '15' and calendar_period ' 03 "

    so the results would look something like below. Also the max_period and max_year could be in the same calendar year that I ask for this combination of accounting, an example of this is the 2070 account in the result below.

    I would like to run a query similar to this one

    SELECT
       CALENDAR_YEAR,
      CALENDAR_PERIOD,
      AMOUNT,
      FUND,
      ACCOUNT,
      ORG,
      PROG
    FROM MAXTABLE
    WHERE CALENDAR_PERIOD = '03'
    AND CALENDAR_YEAR = '15';
    
    
    
    
    
    
    

    but it would give me only the amounts on the table which exist for this year and the period. I also need to get the max calendar_year and max_calendar_period before calendar_period 03 and calendar_year 15 and add those to my request.

    .expected output:

    Calendar_Year calendar_period amount Fund account org PROG
    1522520211820713662121
    15315020211820703662121
    14125020211820403662121
    13145020211820103662121

    noticed that I'm not getting the $100 for account 2070 calendar_year question 14 and calendar_period ' 03 ". This point has already had a calendar_year max 15 and period 2 so I exclude it, since is less than 15 calendar_year but it is also a lot already this combination of accounting.

    Hope it makes sense.

    Sorry, I did not single-digit into account correctly.  Two years and by the need to be wrapped with to_char as:

    TO_CHAR (yr, 'fm00') | TO_CHAR (per, 'fm00')<=>

    John

  • Does SOUL used in combination with the hierarchy of position for purchase requisitions

    Hello

    We use the hierarchy of position for purchase requisitions.

    Can we use management engine approval in combination with the hierarchy of existing post for purchase requisitions for workflow routing more flexible?

    Kind regards

    Natalia

    Hi Natalia,

    Using hierarchy Position in the SOUL is supported only in R12, please refer to the following document:

    Using SOUL, creating approval routing at a certain Position in the hierarchy of Position HR [ID 1501433.1]

    Hope this helps,

    Please, if this response has answered/solved your question/query, please mark it as correct or useful to facilitate the correct answers to the members of the community.

  • SQL query for the combination of characters

    Hi all

    I need an outlet on different combinations of characters.

    with t as)
    Select col1 ('124:369'), ('598:192') col2, col3 as ('547:541:965')
    of the double
    )
    Select * from t;

    output:
    =====
    124:369, 369:124
    598:192, 192:598
    547:541:965, 965:541:547, 541:547:965, 541:965:547, 965:547:541, 547:965:541

    Thank you and best regards,

    ajraj

    Hi, Ajraj,

    G2500 wrote:

    Hi all

    I need an outlet on different combinations of characters.

    ...

    Actaully, 'combination' means that any order, then "124:369" is the same combination as "369:124".  It looks like you want all the different Permutations, combinations of no.

    You can do this by dividing each separate points colon-delimited string (that is, splitting '124:369', ' 124 'and ' 369') and then re - combine in every possible way.  You can use CONNECT BY (without START WITH clause) for both steps.

    Here's a way to do it:

    WITH unpivoted_data AS

    (

    SELECT str

    REGEXP_SUBSTR (str,

    , '[^:]+'

    1

    LEVEL

    ), Element

    T

    UNPIVOT (str

    FOR the collar (col1, col2, col3)

    )

    CONNECT BY LEVEL<= regexp_count="" (str,="">

    AND PRIOR str = str

    AND PRIOR SYS_GUID () IS NOT NULL

    )

    got_perm AS

    (

    SELECT str

    , SUBSTR (SYS_CONNECT_BY_PATH (point, ' :')))

    2

    ) AS perm

    Of unpivoted_data

    WHERE CONNECT_BY_ISLEAF = 1

    CONNECT BY NOCYCLE str = str PRIOR

    AND point <> point PRIOR

    )

    SELECT LISTAGG (perm, ",") THE Group (ORDER BY perm) AS perm_list

    OF got_perm

    GROUP BY str

    ;

    Output:

    PERM_LIST

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

    124:369, 369:124

    541:547:965, 541:965:547, 547:541:965, 547:965:541, 965:541:547, 965:547:541

    192:598, 598:192

    Why is your data?  What is the best way to keep it?

    Relational databases work best when each column of each row contains 1 single piece of information.  It is so fundamental to the design of table that he called the first normal form.  This problem (and, I bet, many other problems) would be easier if your data is stored in a standard table.

Maybe you are looking for