Query in one step

I have available quantity of some product on my stock in different warehouses.
Now, i have to take some quantity from stock.
I have to take it from warehouse based on priority order.
Every product has some packing unit, for example 10 pieces in one package.
If there is quantity less than packing unit in some warehouse i have to take it on the end.
I should not take all quantity from one warehouse but 10% from every warehouse until I take all required quantity.
I think that the following example is clear enough.
Can someone create a query in one step? Or at least with the less possible steps. I have tried with recursion but with no success so far. If possible without cursor.
CREATE TABLE #test(whId INT, availQ INT, priority tinyint)
INSERT INTO #test(whId, availQ, priority) VALUES
(1, 100, 1),(2, 5, 1),(3, 40, 2),(4, 30, 2),(5, 60, 2),(6, 20, 3),(7,10,3)
--I need to take quantity=250, 10% on each step, order by priority and quantity
--taking by packing unit=10, if doesn't exist enywhere more than packing unit than take what exists
--step 1: priority 1(first take from the highest priority warehouse): 10%=33, qRemain=qRequired=250
UPDATE #test SET availQ=67 WHERE whId=1
--(whId=2 has quantity less than packing unti, so leave it untill the end)
--step 2, stil priority 1, qRemain=250-33=217:
UPDATE #test SET availQ=34 WHERE whId=1
--step 3: stil priority 1, qRemain=217-33=184:
UPDATE #test SET availQ=1 WHERE whId=1
--step 4: stil priority 1, qRemain=184-33=151:
--now there doesn't exist record with quantity greater than packing unit and priority 1, so take what is left(order by quantity):
UPDATE #test SET availQ=0 WHERE whId=2
UPDATE #test SET availQ=0 WHERE whId=1
--step 5: go to priority 2(since priority 1 is already all used), qRemain=151-1-5=145:
UPDATE #test SET availQ=27 WHERE whId=5
UPDATE #test SET availQ=7 WHERE whId=3
--step 6: still priority 2(no row with availQ>qPack), qRemain=145-33-33=79:
UPDATE #test SET availQ=0 WHERE whId=4
UPDATE #test SET availQ=0 WHERE whId=5
UPDATE #test SET availQ=0 WHERE whId=3
--step 7: go to priority 3, qRemain=79-30-27-7=15
--priority 3 are two warehouses with availQ lower than packing unit, so, take the row with greater Q:
UPDATE #test SET availQ=5 WHERE whId=6
DELETE FROM #test WHERE availQ=0
--at the end I have in my stock available quantity=5 on warehouse id =6 and 10 on warehouse id=7
SELECT * FROM #test
DROP TABLE #test

I have difficulties to understand some of the basics of the problem. How can 10% of 250 be 33?
I seems to me that you at first should check if the total for the current priority is less than required quantity, and this case you can empty all these warehouses in one go.
It's difficult to say whether this can be done in a single statement and in that case whether it should be done. It seems that the windows function in SQL 2012 could come in handy. Without them, it may be faster with a cursor.
A recursive query may be possible. However, for a query like this it may be a cursor in disguise and not particular faster, only more difficult to understand and maintain.
But if you need to do this for multiple products at one occasion, it is definitely feasible to do this with set-based iteration and allocate warehouses for all products in breadth.
Since you discuss read-committed snapshot: with a single query you would see the same data all the way through the query, as long as you don't use scalar-defined functions with data access. If you are really dependent on the data not changing, you should
use true snapshot isolation. But that assumes that you want to perform your computations on the fixed state when the operation starts, and I am not sure that is what you want. Let's say that in your example that when you start the operation that there
10 units in warehouse 7, but before you come to check that warehouse someone else takes those 10 units (or for that matter deposits 50 new units).
Let's study the possibilities here:
1) True snapsnot: you would read 10 for warehouse 7, but when you try to update, you get an update conflict and the operation fails.
2) Read-committed snapshot, using a single query: You would read 10 for warehouse 7, take those units and then set the quantity for the warehouse to -10. (Or even worse, set it to 0, having taken 10 units that does not exist).
3) Read-commited without snapshot: when the query reaches warehouse 7, it would read 0, and ignore the warehouse. Or you would read 10 and when you come back to update, the 10 are gone.
4) You run the transaction with REPEATABLE READ: Someone may steal those 10 units in wh7 before you get there, but that's alright. But once you have read them, they are yours - unless someone else also read them, but have not yet updated the quantity. In
this case, you will get a deadlock.
5) You use the hint UPDLOCK when you read the data. This the same as REPEATABLE READ, however other processes cannot take out an UPDLOCK on the row. (They can read the row with a shared lock.) This prevents deadlocks.
Here I assuming that you first assmeble what to take from the warehouses in a temp table and then update the warehouses as well your picking-lists. If you update directly and use the OUTPUT clause to get the data for the picking-lists, this alter the equation
somewhat: UPDATE should take out an UPDATE lock for you. But the situations still requires careful analysis, and particularly you need to take care that you don't use the snapshot when you shouldn't.
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • Outbound Delivery Document Flow (One-Step procedure) Goods Receipt

    I am trying to extract the Goods Receipt status of Outbound Deliveries which are posted using One-Step procedure.  I can do this one delivery at a time using VL03N.
    Some sites (plants) are configured with One-Step procedure and others are configured as Two-Step procedure.
    Document flow of an Outbound Delivery is extracted from table VBFA.
    For the two-step delivery process (Movement type 641) the Goods Receipt is recorded in table VBFA (Subs.doc.categ. / VBTYP_N = 'i').  Therefore, I can easily extract the goods receipts for these deliveries. 
    However, for the one-step procedure (Movement type 647) the Goods Receipt does not appear to be recorded in table VBFA.  Does anyone know which table the Goods Receipt is stored for one-step procedure?
    Using transaction VL03N we can view the document flow.  It appears that for One-Step procedure the Goods Receipt is held at item level.  By highlighting a delivery line in VL03N and viewing the document flow we can see the goods receipt for One-Step deliveries (therefore, the data must be held somewhere).
    I understand that by using One-step procedure the goods receipt is posted automatically, so we can assume that there should never be outstanding Goods Receipts for One-Step sites.  However, I have been tasked with the job of reconciling all deliveries for the past 12 months.  I need to prove that a goods receipt has been posted for every single delivery.
    Any advice is much appreciated.

    Thankyou for the advice.   I will proceed with your recommendation.
    However, the query is inefficient since the link between LIKP and MKPF does not use the primary key (Article Doc. / MBLNR).  We have almost 7 million records in MKPF table. 
    Is there any intermediate tables I can include between LIKP and MKPF so that only primary keys are used for the joins?

  • Transport abap query from one client to another--v--urgent---v----urgent

    hi,
    i have an abap query in one client and want to copy to another client.can i do this if so what are the steps involved .i tried the options like in query go to environment and then to transport .i also tried to do by running the programe RSAQR3TR.but it was transporting in the same client .i want to transfer into another client.plz help me out asap .

    hi,
    Create a request related to that Table Definition and add data content in the request like
    R3TR-TABL-<table name>
    R3TR-TABU-<table name>
    R3TR-TABD-<table name>
    and release the request
    Take the DATA files and CO files related to that request in a Pen drive and put the same into the new server DATA and CO files and import using STMS.
    For new program, it gets transported along with the program.
    For changed program (which has already been transported once), you need to include texts in transport request - just go to Selection texts, save them in the transport request and you're all set.
    whenever you assign a request number to the program
    and also the selection texts willbe stored under that request number
    so when u release the program
    the selection texts will also go
    after releasing the request if you change only the seleciton texts
    only the changes done to selection texts will reach the target
    and the rest of the things remain same
    so when ever you release a request all the things under that request
    will go

  • Can i save a query in one package and then can i move it into another .....

    can i (in bw 3.5)save a query in one package and then, can i move it into another package? if yes what is the procedure? if no, why? if yes , after moving can i see that query in one package or both? plzzzzzzzz its urgent....

    Hi Neeraja,
    Yes, you can save a query in one package and then move it into another package and transport the query with new package.
    There are two ways...
    1. You can change  the package assignment from one to another with the same request and transport the objects with request assigned to the new package.
    2. You can create a new request and assign the new request to new package and transport the new request with new package assignment.
    For first one, follow below process...
    Changing the assignment:
           1.      Choose  Package in the toolbar of screen area Collected Objects in RSA1 --> Transport Connection.
    The Select Objects for Changing package dialog box appears.
           2.      Select the objects whose package assignment you want to change.
    This procedure is especially useful for changing all collected objects, or a large number of them. For this reason, all indicators are set by default. Remove the indicators for individual objects where necessary.
    Note: You can only use  Details to display the details for an individual object when this is the only object that is selected.
           3.      Choose  Copy. The Change Object Directory Entry dialog box appears.
           4.      Enter the new package.
    For second one, you need to add the query in the transport organiser and assign new request and transport package and then go for transport. But this will create two requests on the same query. So, don't go for this. First step is preferable.
    Hope this helps u...
    Regards,
    KK.

  • One step approval workflow

    Hi All,
    I have added a step in one step approval workflow to check shop on behalf.
    When a person shop's on behalf of a manager, and 1 step approval is triggered, i want the approver to be the manager.
    But what is happening is that Manager's Manager is getting the workitem!
    My query is that :if A shop's for B, the approver should be B for the shopping cart. How can i achieve this????
    Anybody knows what needs to be done in this scenario?
    Thanks & Regards,
    San

    Hi San,
    In addition to Usman's views also look at the below mentioned points :
    1. In your org. structure if you have named somebody else as the head of the department for that org. unit, it will go to that person for approval.
    2. And there is no point in taking approval from the same guy for whom you are shopping on behalf. In such a case that guy can himself create a shopping cart.
    Looks like there is some mis / lack of understanding about the correct client requirements.
    But there are few work arounds you define this guy as the substitute for his manager.Then either of them can approve the shopping cart.
    Or.. don't make any body as H.O.D of org. unit and define the approval agent in the work flow builder using SWDD txn and let the approver will be taken from there.
    Hope this makes you more clear and helps in resolving your issue. Clarifications are welcome.
    Award points for helpful answers.
    Rgds,
    Teja

  • Copy BEx Query from one SAP BI system to another SAP BI system?

    Experts please help me to Copy a BEx Query from one SAP BI system to another SAP BI system?
    Thanks in Advance.......

    HI,
    Just assume like below,
    you are creating your query in Development server(BI),
    Before creating query, you go to SE09(t code), here you create a workbench request by clicking the create button.
    Give some description here to be identified by you in future.
    Now a requested will be generated(ex: DW5K50098) with prefix of your server name.
    now you go to query designer, you create query, when you save the query, it will ask you request, now you click button 'own request'. you choose request(create by you in SE09).
    it will be saved under this request, now you will have one more request under your request in SE09
    parent request(DW5K50098) and child request(DW5K50099).
    First you click child request and relese(click lorry button in se09),second you click parent request and lorry button.
    next steps will be related to your organization, you can ask your team leader, he will be taking care of it.
    Regards,
    Ranganath.

  • OVD One step configuration for AD

    Hi all,
    Please help me to sort out the problem in configuring the one step configuration for AD using OVD.
    I had followed the document of Oracle® Virtual Directory Product Manual Release 10g (10.1.4.2.0).
    Link: http://download.oracle.com/docs/html/E10286_01/ovdm.htm#CACCGAAF
    But i am not able to complete the configuration.I was able to follow the steps upto 5.
    In step6 by clicking the button Verify Directory Servers it is taking more time to validate. Where i can check logs for this.
    I was able to ping and i was able to bind to Active Directory. But this step is not able to complete. It is telling "*Querying XX.XX.XX.XXX:389*" for namespaces. I had waited for two hours no message.
    What may be wrong in configuration. I had specified the following values in the cloumns:
    Domain DNSName: company.com [Active Directory Root DN]
    Server DNS/IP:

    Hey friend,
    You are absolutely right. The issue is with DNS.
    Without specifying DNS in the entry, it is able to validate the Active Directory.
    Now, how can i resolve DNS issue in my system. Can you help me to sort out in solving this issue.
    Thanks in Advance,
    Sandy D

  • One step DVD unable to capture video

    I have made many movies using one step dvd in the past. For some reason, now I get a message "Nothing Captured" "No video has been captured" when I try to create a new dvd from my video camera. I used the same camera and connection cords on another apple and it worked fine. It worked fine on this computer in the past. Any ideas?
    Thanks

    Bengt;
    Good ideas but I have checked each of those items. My camera is set to play, the tape works and I am using my firewire cable. The strange thing is I did everything exactly the same on a different apple computer and it copied the tape to the DVD with no problem.
    Thank you for thinking about my problem.

  • One Step DVD Capture Video TIme

    I've just recently run into a problem with One Step DVD and would like to know if anyone else has had the same issue. I normally don't use One Step DVD but my father uses it because it requires little if any computer skills when it comes to iMovie or iDVD.
    He recently came to me with a problem that has cropped up with the One Step option. When he goes to use it the the very first step in the process is capturing the Video from the camera. The camera rewinds the tape as is the norm and then the computer starts to capture the video with no problem. The problem is with the capture time/available time that shows up on the screen. For some reason the available time consistantly shows approximately nine minutes when it is a full hour long tape. I've tried finding if there is some setting that is throwing it off in the preferences and can't seem to find anything related to it. I've also tried dumping the preferences with no result. I've also tried a number of different video tapes with no change. Recently I took it home and tried it on my system where it gave me a available capture time of 17 minutes even though there is an hour of footage. Today I took MY video camera up to my folks house and hooked it up and gave it a shot and of course it gave me an available capture time of 9 minutes. So I'm at a loss here folks. Any help would be appreciated. I personally think it's the prog but I'm open to suggestions. Below are the cameras and systems I have tried this on. Remember I've tried both cameras with each computer with the result of the available capture time not matching the amount of footage on the tape.
    My Father's setup
    Mac Mini 1.6 Ghz
    1.6 Gb Ram
    OS 10.4.8
    Video Camera: Canon ZR45
    My Setup
    Dual 2.0 Ghz PowerMac G5
    2.5 Gb ram
    OS 10.4.8
    Video Camera: Sony TRV315

    Hi Wayne,
    I honestly have no explanation why iDVD / OneStep is showing only 17 mins. left of capture unless you don't have enough available HD space to capture to.
    3 suggestions only:
    1. Make certain you have plenty of available HD space for this process. I recommend no less than 20GB of available HD space for each hour of video.
    2. Before launching iDVD/OneStep, drag and drop the plist for iDVD to the trash. Looks like this:
    com.apple.iDVD.plist
    3. After trashing the plist for iDVD, connect your mindv camcorder via firewire and relaunch iDVD while the camera is powered on. In most cases, iLife apps will default to the correct settings on its own (provided your mac sees the connection under system profiler).
    Good luck & hope this helps
    SDMacuser

  • One Step DVD From VCR Patched Through Mini-DV

    I guess the One Step method only works with Device Control inputs?
    I've got my VCR patched through my MiniDV camera, and a Firewire cable to the Mac. When I choose One Step DVD, it looks for a tape in my camera. But I can capture in FCP with this same setup.
    I guess iDVD 5.0 won't let me capture this way? I don't see any preferences I can change.
    Also, is there a maximum length of capture in iDVD 5.0?
    Thanks!

    I guess the One Step method only works with Device Control inputs?
    Yep - OneStep will not work witout timecode.
    from http://docs.info.apple.com/article.html?artnum=300735
    Note: OneStep DVD will only capture from recorded tape on a DV camcorder or deck. OneStep DVD will not work with analog-to-DV converters. It will also not work if you are using your camcorder or deck as a pass-through device to digitize from an analog souce (such as VHS or Hi-8). In order to function, OneStep DVD reads the timecode from your DV tape and it relies on DV device control to communicate with the camera or deck.
    G5dual2GHz, 550TiBook, 9600>G3Upgraded   Mac OS X (10.4.3)   1TB+ Storage

  • One Step Workflow Activation for Shopping cart in SRM 7.0

    Dear Experts,
    On working to EXtended Classic Scenario, I need to configure one step approval for shopping cart in SRM 7.0.
    Currently it is providing me Auto Approval  by default and based on it PO is being created. I need one-level manager approval  shopping cart.
    I am working in Demo System of Extended Classic Scenario, I need only one step approval SC without any condition.
    Kindly tell me the configuration steps in SRM and please tell me one more thing is there  any need of  BADI or other coding for one-step approval?
    Thanks in Advance,
    Regards
    Pawan Keshwani
    Edited by: pawankeshwani on May 25, 2011 6:22 PM

    Hi Ashutosh,
    Thanks for quick reply.
    As per your comment when i am tring to activate /SAPSRM/C_SC_600_001_SP04  by t-code SCPR20, But It shows me a information pop up Maintain the client role and then Activation is Cancelled.
    Kindly tell me what i am missing or any other prerequisite step to do for this BC Set Activation.
    Thanks
    Pawan Keshwani

  • Open file & close folder that it's in - in one step

    I want to be able to double click a file to open it and then the folder this file is in closes so that it is no longer open on my desktop. Is there a one step way to do this?

    Yes.
    Hold down the Option key while double-clicking the file.
    This also works when opening sub-folders - the double-clicked folder will open and its enclosing folder will close.

  • Trouble positioning a stepper motor accurately (to one step)

    I'm using a PCI-7344 with an MD7506 motor driver from RS. I'm testing angle sensors whose voltage output is proportional to (you've guessed it...) angle.
    I'm stepping the motor round by one step, checking that it's got there by reading the encoder, measuring the sensor voltage and repeating.
    My problem is that the motor sometimes 'hunts' around the target position. In closed loop (with an encoder associated with a motor) this results in sometimes not being able to reach the target exactly with the pull-in moves.
    Using open-loop and reading the encoder directly I get better results because I can loop more times, but the motor will still 'hunt' around a target point, changing direction a few times as it over
    shoots by a step.
    As there are a million MAX settings for this I won't list them all here, but I'm checking for a move complete when the InPos flag is set and I have a deadband of 0 steps too. I'm only rotating at 120 step/sec with a 10ms pause between steps.
    Do I have to accept that stepper motors are only accurate to +/- 1 step or am I doing something wrong?
    Thanks,
    Mike
    Mike Evans
    TRW Conekt
    N.I. Alliance Member, UK
    http://www.Conekt.net

    Mike,
    Maybe other people will have better suggestions, but in my case I can advise to either:
    1. increase the number of pull-in moves,
    2. increase the microstepping of your drive,
    2a... together with a higher precision encoder...
    Other than that, the accuracy is advertised to +/- 1 step, so maybe you would like to consider a stepper with more steps/rev.
    Good luck!, I hope other people will give other suggestions.
    Nestor.
    Nestor
    National Instruments

  • XMLP print - two steps or one step?

    When we were in 11.5.9 + XMLP 4.5 (with XMLP and CM integrated configuration), to print concurrent reports via XMLP always two steps. namely, the first step produces XML file and the second step is running XML Publisher Report to feed the XML file to the template and print pdf output.
    After we upgraded EBS from 11.5.9 to 11.5.10.2, the XMLP has also been upgraded to 5.0. Apperas to be (my observation) that SRS enabled reports(such as "Printed Purchase Order(Portait)", "Invoice Print Batch of Invoices", "Invoice Print Preview Report", "Invoice Print Selected Invoices", etc) can been printed in one step (no need to run the second step - run XMLP, OPP takes care of it).
    However, for non-SRS programs, such as "Print purchase order", "Print Statement", "Dunning Letter Print", it still need two steps. Oracle support says, it is bug to specific report (she gave Dunning Letter Print as example) and not related to SRS.
    Just wonder if any one in this forum has similar circumstances and has got any workaround.
    Any help and information appreciated.

    You are correct in your assessment. We created a program that submits the desired program then passes the request id, template, etc. to the xml report publisher concurrent program.
    Brett

  • How to upload all songs from iTunes Match to the mac in one step

    how to upload all songs from Match to the Mac in one step?

    You have to subscribe to iTunes Match to do that.  See http://www.apple.com/itunes/itunes-match/.

Maybe you are looking for

  • How to test to see if the cursor is on the last row in a jTable?

    I like to know how to test the cursor position in jTable.

  • Build internet application I e-copy

    hi all when can i find e-copy of buid internet application I and II material? thank you

  • Are Analytic Workspaces suitable for very large data sets?

    Hi all, I have made many different tests with analytic workspaces and i have used the different features (compression,composites...). The results especially for maintenance are disappointing. I have a star schema with 6 dimensions. The fact table has

  • Installing Lexmark drivers

    Hello, I've had my MBA for two weeks so I'm a newbie, struggling with the learning curve but getting there slowly ! Experienced Windows user. When I plug in my Lexmark X4950 via USB it prints, but won't scan and it won't print via wifi. I've download

  • Old purchased song authorization

    Recently, re-installed OS Lion. All data was restore succesfully, except one thing. I cannot play a song that using my family video in iMovie, because the song isn't authorized by my computer....??? I purchased the song when I was on previous address