简体   繁体   中英

SQL - How to count number of distinct values (payments), after sum of rows where they have another column value (Due Date) in common

My 'deals_payments' table is:

Due Date    Payment     ID
1-Mar-19    1,000.00    123
1-Apr-19    1,000.00    123
1-May-19    1,000.00    123
1-Jun-19    1,000.00    123
1-Jul-19    1,000.00    123
1-Aug-19    1,000.00    123
1-Jun-19    500.00      456
1-Jul-19    500.00      456
1-Aug-19    500.00      456

I have the SQL code:

select 
   count(*), payment

from (select deals_payments.*,
             (row_number() over (order by due_date) -
              row_number() over (partition by payment order by due_date)
             ) as grp
      from deals_payments
       where id = 123
           ) deals_payments
group by grp, payment
order by grp

which gives me what I want - the number of payments on each distinct amount - (here I only asked for ID 123):

COUNT(*)    PAYMENT
6           1000.00

But now I need the sum of payments of the two ID's (123 and 456), where the due dates are the same, and count the number of payments on each distinct amount, as:

COUNT(*)    PAYMENT
3           1000.00
3           1500.00

I tried the below but it gives me the 'missing right parenthesis' error. What is wrong??

    select 
      count(*), 
      (select 
          sum(total) total
       from (select distinct 
                due_date,
                (select 
                   sum(payment) 
                from deals_payments 
                where  (due_date = a.due_date)) as total
             from deals_payments a 
             where a.id in (123, 456) 
             and payment > 0)

        group by due_date
        order by due_date)  b

    from (select deals_payments.*,
                     (row_number() over (order by due_date) -
                              row_number() over (partition by payment order by due_date)
                             ) as grp
                      from deals_payments
                       where id = 123
                           ) deals_payments
                group by grp, payment
                order by grp

Taking your earlier comments into consideration, I agree that the SQL can be simplified to get the intended result. My understanding is that the expected output is the frequency of the total payment of a subset of IDs on any given date.

select count(*) as PaymentFrequency, TotalPaidOnDueDate from
(
    select due_date, sum(payment) as TotalPaidOnDueDate from #deals_payments
    where ID in (123, 456)
    group by due_date
) a
group by a.TotalPaidOnDueDate

Here is a sql fiddle I used to verify: http://sqlfiddle.com/#!18/6b04f/1

This seems really strange. I don't understand why your logic is so complicated.

How about this?

select id, count(*), max(payment)
from (select dp.*,
             count(*) over (partition by due_date) as cnt
      from deal_payments dp
      where dp.id in (123, 456)
     ) dp
where cnt = 2
group by id;

An interesting question. Could this do the trick???

select payment, count(*)
  from deals_payments 
 where due_date in 
(select due_date
  from deals_payments
  group by due_date
  having count(*) > 1)
 group by payment;

You can add a filter by id if you want, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM