0

I run two different queries. This one works well

with t(n) as(
    values
           (1),
           (1),
           (1),
           (2),
           (10)
)
select
       width_bucket(n,array[1,3,6,15]) g,
       count(*)
from t
group by g
order by g

but when I run this (real) query, it reports error on bucket

with data as (
    select
           vendor_id,
           count(distinct pi.id) as cnt
    from payment_invoice_items as pii
    join payment_invoices as pi
        on pii.invoice_id = pi.id
        and pii.deleted_at isnull
    group by vendor_id
)
select
       width_bucket(data.cnt, array[1,10,20,30,40,100,200,400]) as grp,
       count(*)
from data
group by grp
order by grp 

The data.cnt is a one column of integer data. why does this report error?

[42883] ERROR: function width_bucket(bigint, integer[]) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

Thanks

1
  • 1
    The data.cnt is a one column of integer data No, COUNT returns a BIGINT, so you will have to explicitly cast it to INTEGER in the width_bucket function. Commented Oct 20, 2020 at 9:07

1 Answer 1

2

The COUNT function returns type BIGINT, not INTEGER. Hence the error on that type:

[42883] ERROR: function width_bucket(bigint, integer[]) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

You will need to cast COUNT's result explicitly to INTEGER in order to use it in your function:

with data as (
    select
           vendor_id,
           count(distinct pi.id) as cnt
    from payment_invoice_items as pii
    join payment_invoices as pi
        on pii.invoice_id = pi.id
        and pii.deleted_at isnull
    group by vendor_id
)
select
       width_bucket(data.cnt::INTEGER, array[1,10,20,30,40,100,200,400]) as grp,
       count(*)
from data
group by grp
order by grp 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.