如何只为SQL查询中的第一行填充列中的数据

时间:2019-04-28 13:25:36

标签: sql oracle

我必须编写如下查询,但是它应该只为选择性列中的第一行填充数据。第二行至最后一行的列值应为空白。但是其他列值应该存在。

SELECT CLAIMID as "Claim Id", PAID_IN_INSTALMENTS as "Instalments", 
       F_CALCULATE_TOTAL_PAID(CLAIMID) as "Total Amount Paid"
  FROM PT_CLAIMS WHERE CLAIMID = '123456'

在此查询中,仅应为第一行填充“已支付总额”列。请不要,我不能更改功能F_CALCULATE_TOTAL_PAID。输出应如下所示:

Claim Id   |  Instalments   |   Total Amount Paid
-----------|----------------|---------------------
123456     |   500.00       |     1800.00
-----------|----------------|---------------------
123456     |   300.00       |                     
-----------|----------------|---------------------
123456     |   600.00       |                     
-----------|----------------|---------------------
123456     |   400.00       |                     

1 个答案:

答案 0 :(得分:0)

SQL表表示无序集。除非有列指定顺序,否则没有“第一行”之类的东西。

让我假设您有这样的专栏。在这种情况下,您可以使用窗口功能:

SELECT CLAIMID as "Claim Id",
       PAID_IN_INSTALMENTS as "Instalments", 
       (CASE WHEN 1 = ROW_NUMBER() OVER (PARTITION BY CLIAMID ORDER BY <ordering column>)
             THEN SUM(PAID_IN_INSTALMENTS) OVER ()
        END)  as "Total Amount Paid",
FROM PT_CLAIMS
WHERE CLAIMID = '123456'
ORDER BY <ordering column>;

这不使用函数F_CALCULATE_TOTAL_PAID(CLAIMID),因为它似乎只是PAID_IN_INSTALMENTS的总和,并且有一个内置函数可以做到这一点。如果该函数有更多逻辑,则可以(当然)可以使用它代替窗口函数:

       (CASE WHEN 1 = ROW_NUMBER() OVER (PARTITION BY CLIAMID ORDER BY <ordering column>)
             THEN `F_CALCULATE_TOTAL_PAID(CLAIMID)
        END)  as "Total Amount Paid",