【解決方法】mysqlで、最初の列と2番目の列から1行を1行追加したい


以下のようなデータがあります。

id	merchant_enrolled_program_id	point	current_balance	Expected RESULT
7499475	23140	-111634	288636	288636
7499601	23140	-266593	288636	22043
7500024	23140	1500000	288636	1522043
7500059	23140	-74314	288636	1447729
7500191	23140	-14049	288636	1433680

最初の current_balance (288636) と 2 番目の行のポイント (-266593) を取得したい場合、期待される結果は 2 行目の current_balance 列に入力されます

私も期待される結果を与えたので。

私が試したこと:

select 
coalesce(at.current_balance+(select * from (select sum(a1.point) from akshaya_temp a1
where a1.id <at.id and a1.merchant_enrolled_program_id=at.merchant_enrolled_program_id order by a1.id limit 1,1)as x where x.row_number <= 2)
,at.current_balance) into v_current_balance 
from akshaya_temp at where at.id=v_ID and at.merchant_enrolled_program_id= v_merchant_enrolled_program_id;

解決策 1

ここを参照してください: MySQL | LEAD() と LAG() 関数 – GeeksforGeeks[^]

コメント

タイトルとURLをコピーしました