PLSQL过程根据以前的记录值填充数量

时间:2016-12-14 10:01:57

标签: oracle stored-procedures plsql

我有一张下表

- ITEM_NUMBER QTY_FROM QTY_TO
 - ABC           10
 - ABC           20
 - ABC           30
 - DEF           100
 - DEF           250
 - DEF           400

我需要编写一个plsql程序,该程序将基于QTY_to下一行的qty_from列填充相同的项目编号。 例如,如果项目编号为ABCqty_from在第一行为10,在下一行为20 .... id期望该过程查看第二行...并填充qty_from在第一个为19(next_row_qty_from - 1)。

2 个答案:

答案 0 :(得分:1)

select ITEM_NUMBER, QTY_FROM, 
       lead(QTY_FROM, 1) over (partition by ITEM_NUMBER, order by QTY_FROM) - 1 as QTY_TO 
  from table;

作为lead功能的第三个参数,您可以添加默认值。当给定的ITEM_NUMBER不存在下一行时将使用此选项,因此如果您- ABC 30lead(QTY_FROM, 1, QTY_FROM) 29 QTY_TO,则会<?php require 'vendor/autoload.php'; use net\authorize\api\contract\v1 as AnetAPI; use net\authorize\api\controller as AnetController; define("AUTHORIZENET_LOG_FILE", "phplog"); function authorizeCreditCard($amount){ // Common setup for API credentials $merchantAuthentication = new AnetAPI\MerchantAuthenticationType(); $merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID); $merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY); $refId = 'ref' . time(); // Create the payment data for a credit card $creditCard = new AnetAPI\CreditCardType(); $creditCard->setCardNumber("4111111111111111"); $creditCard->setExpirationDate("1226"); $creditCard->setCardCode("123"); $paymentOne = new AnetAPI\PaymentType(); $paymentOne->setCreditCard($creditCard); //create a transaction $transactionRequestType = new AnetAPI\TransactionRequestType(); $transactionRequestType->setTransactionType( "authOnlyTransaction"); $transactionRequestType->setAmount($amount); $transactionRequestType->setPayment($paymentOne); $request = new AnetAPI\CreateTransactionRequest(); $request->setMerchantAuthentication($merchantAuthentication); $request->setRefId( $refId); $request->setTransactionRequest( $transactionRequestType); $controller = new AnetController\CreateTransactionController($request); $response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX); if ($response != null) { if($response->getMessages()->getResultCode() == \SampleCode\Constants::RESPONSE_OK) { $tresponse = $response->getTransactionResponse(); if ($tresponse != null && $tresponse->getMessages() != null) { echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n"; echo " Successfully created a transaction with Auth code : " . $tresponse->getAuthCode() . "\n"; echo " TRANS ID : " . $tresponse->getTransId() . "\n"; echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n"; echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n"; } else { echo "Transaction Failed \n"; if($tresponse->getErrors() != null) { echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n"; echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; } } } else { echo "Transaction Failed \n"; $tresponse = $response->getTransactionResponse(); if($tresponse != null && $tresponse->getErrors() != null) { echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n"; echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n"; } else { echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n"; echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n"; } } } else { echo "No response returned \n"; } return $response; } if(!defined('DONT_RUN_SAMPLES')) authorizeCreditCard( 23.32); ?> 在你的问题中定义边缘情况。

答案 1 :(得分:0)

无需手续,只需简单更新:

update t 
  set qty_to = 
    (select min(qty_from)-1 from t t2 
      where item_number = t.item_number and qty_from > t.qty_from)

结果:

SQL> select * from t order by item_number, qty_from;

ITEM_NUMBER QTY_FROM  QTY_TO
----------- -------- -------
ABC               10      19
ABC               20      29
ABC               30 
DEF              100     249
DEF              250     399
DEF              400 

6 rows selected