0

I need to convert this query form PostgreSQL in Eloquent but I failed:

select
    invoice_item_id
from
(
    select
    invoice_item_id,
    status,
    row_number() over (partition by invoice_item_id order by id) as rank,
    lead(invoice_item_id) over (partition by invoice_item_id) as next
    from payment_invoice_item_status as piis
    where invoice_item_id > 6000000
    and status in (3191,3238)
)d
where status=3191 and rank=1 and next isnull
order by invoice_item_id

I saw fromSub was supported in Laravel 5 but in Laravel 8 there is no such thing. I tried different ways to convert this but non work. How can I convert it?

4

1 回答 1

0

我找到了

    $sub = DB::table('payment_invoice_item_status')
        ->selectRaw('invoice_item_id, 
                        status, 
                        row_number() over (partition by invoice_item_id order by id) as rank,
                        lead(invoice_item_id) over (partition by invoice_item_id) as next')
        ->where('invoice_item_id', '>', 5000000)
        ->whereIn('status', [3191, 3238]);

    $query = DB::connection('pgsql2')->table(DB::raw("({$sub->toSql()})d"))
        ->mergeBindings($sub)
        ->where('status', 3191)
        ->where('rank', 1)
        ->where('next', null)
        ->get();
    dd($query);
于 2020-12-28T14:16:15.620 回答