领先/落后Oracle

时间:2019-12-05 07:18:45

标签: sql oracle window-functions

我有一个字段(曲目更改) 那只会给我第一行的值,但想将其传递给下一行...

具有超前/滞后功能-不起作用 enter image description here

2 个答案:

答案 0 :(得分:0)

假设26列中should be值的来源是BC003,则可以使用Lead,如下所示:

With tab (BC003) as( Select 1 from dual
               UNION ALL
             Select 18 from dual
             UNION ALL
             Select 26 from dual)
Select BC003, NVL(Lead(BC003) over (Order by 1),BC003) "Shoud be"
from tab;

输出:

SQL> /

     BC003   Shoud be
---------- ----------
         1         18
        18         26
        26         26    

答案 1 :(得分:0)

Oracle设置

SELECT t.*,
       MIN( track_change ) OVER ( PARTITION BY id, bc003 ) AS track_change_new
FROM   test_data t

查询1

您可以使用分析功能:

LAG

查询2

您可以将IGNORE NULLSSELECT t.*, COALESCE( track_change, LAG( track_change ) IGNORE NULLS OVER ( PARTITION BY id, bc003 ORDER BY object ) ) AS track_change_new FROM test_data t 子句一起使用:

<?php
if (extension_loaded('soap'))
{
    // Request parameters :
    // Exemple is a Nav Code unit GetSalesPrices, method GetPrice(CodPCustomerNo, CodPItemNo)
    $NavUsername = "superUser";
    $NavAccessKey = "passworddrowssap";
    $CodeunitMethod = "CallMethod";
    $params = array(
        "employeeNo" => "CUSTOMER_1",
        "leaveType" => "ITEM_1",
    );
    // SOAP request header
    $url = "http://DESKTOP-H5GFAKH:7047/DynamicsNAV100/WS/MyCompany/Codeunit/webportals";

    $options = array(
        'authentication' => SOAP_AUTHENTICATION_BASIC,
        'login' => $NavUsername,
        'password' => $NavAccessKey,
        'trace' => 1,
        'exception' => 0,
    );
    try
    {
        $client = new SoapClient(trim($url), $options);

        $soap_response = $client->__soapCall($CodeunitMethod, array('parameters' => $params));
        echo "SOAP REQUEST SUCESS :";
        var_dump($soap_response);
    }
    catch (SoapFault $soapFault)
    {
        echo "SOAP REQUEST FAILED :<br>";
        var_dump($soapFault);
        echo "Request :<br>" . htmlentities($soap_client->__getLastRequest()) . "<br>";
        echo "Response :<br>" . htmlentities($soap_client->__getLastResponse()) . "<br>";
    }
}
else
{
    echo "Php SOAP extention is not available. Please enable/install it to handle SOAP communication.";
}
?>

输出

两者输出相同:

 ID | OBJECT | BC003 | TRACK_CHANGE | TRACK_CHANGE_NEW
--: | -----: | ----: | -----------: | ---------------:
121 |     12 |     1 |           15 |               15
121 |     13 |     1 |         null |               15
121 |     14 |     1 |         null |               15
121 |     15 |     1 |         null |               15
121 |     16 |     1 |         null |               15
121 |     12 |    18 |           22 |               22
121 |     13 |    18 |         null |               22
121 |     14 |    18 |         null |               22
121 |     15 |    18 |         null |               22
121 |     16 |    18 |         null |               22

db <>提琴here