1

当我使用这个查询时,

select 
   receipt_num, trx_num, 
   (case when receipt_amount > 5000 then '1' else 'null') as stamp_value,receipt_amount 
from ra_customer_all where receipt_amount > 5000;

它给出这样的输出:

receipt_num             trx_num           stamp_value     receipt_amount
   23679                sf35fd                 1              5400
   23679                sdf2424                1              5400 
   23679                rer434                 1              5400
  987444                dgd343                 1              98432
    7610                sdf23                  1              6756
    7610                dfg242                 1              6756

但我希望输出看起来像这样:

receipt_num        trx_num        stamp_value      receipt_amount
  23679            sf35fd             1                 5400
  23679            sdf2424            null              5400
  23679            rer434             null              5400
 987444            dgd343             1                 98432
   7610            sdf23              1                 6756
   7610            dfg242             null              6756

对于每个收据编号 > 5000,邮票值应仅打印一次。

(*一张收据可能包含一个或多个 trx_num*)

请帮我解决一下这个。

select 
acra.attribute6 office_code,
acra.attribute5 collection_number,
acra.receipt_number instrument_number,
 acra.receipt_date collection_date,
 acra.amount collected_amount,
ac.customer_name,
rcta.trx_number ,
(case  row_number() over (partition by acra.receipt_number order by rcta.trx_number)  when acra.amount > 5000 then '1'  else 'NULL' end) stamp_value,
from
ar_cash_receipts_all acra,
ar_customers ac,
ra_customer_trx_all rcta,
ar_receivable_applications_all araa
where
acra.pay_from_customer=ac.customer_id and
acra.cash_receipt_id = araa.cash_receipt_id and
araa.applied_customer_trx_id=rcta.customer_trx_id
and acra.amount > 5000

好的,我更新了我的加入查询,我在其中添加了分区,但作为缺少的关键字给出了错误。有人可以编辑它以获得所需的输出

4

3 回答 3

3

所以你想让 stamp_value 为组中的第一行为 1 并且为所有后续行为 NULL ?使用分区方式:

select 
   receipt_num, trx_num, 
   (case row_number() over (partition by receipt_num order by trx_num) 
     when 1 then 1
     else NULL
     end) stamp_value,
   receipt_amount
from ra_customer_all
where receipt_amount > 5000

这会将第一行的 stamp_value 设置为 1(使用 trx_num 进行排序),并将所有后续行设置为 NULL。

于 2012-07-06T10:26:25.240 回答
1

试试这个

 select receipt_num,trx_num, result= 
case when receipt_amount >500 then 1 else null  end,receipt_amount from ra_customer_all

演示 http://sqlfiddle.com/#!3/f29a6/1

http://sqlfiddle.com/#!3/f29a6/2

于 2012-07-06T10:28:22.340 回答
0

您可以先在您的选择语句中包含receipt_amount 以进行检查吗?可能是您对receipt_amount 的期望不正确,并且脚本行为正确。

此外,您的 null 不应在 case 语句中使用单引号,您将在结果集中获得一个字符串而不是 null 值,正如您所期望的那样。

于 2012-07-06T10:09:26.520 回答