当我使用此查询时,
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
好吧,我更新了我的连接查询,其中我添加了分区,但错误是错误的关键字。可以有人编辑这个以获得所需的输出
答案 0 :(得分:3)
因此,您希望组中第一行的stamp_value为1,后续所有行为NULL?使用PARTITION BY:
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。
答案 1 :(得分:1)
试试这个
select receipt_num,trx_num, result=
case when receipt_amount >500 then 1 else null end,receipt_amount from ra_customer_all
答案 2 :(得分:0)
您是否可以在select语句中包含receipt_amount以进行检查?可能是您对receipt_amount的期望不正确,并且脚本行为正确。
另外,你的null不应该在case语句中用单引号,你将在结果集中得到一个字符串而不是null值,就像你期望的那样。