1

I have a table transactions:

client_id   Amount   payment ref   contract type
1           300      MTV           1
1           300      MTV           1
1           300      MTV           1
1           150      ML            1
2           150      ML            2
2           150      ML            2
2           150      ML            2
2           150      ML            2

If a client is on contract 1, then their total payments should be 3 x MTV(300) and 3 x ML(150). If they are on contract 2, then they pay 6 x ML(150).

I am trying to figure out how to get the total number of payments remaining for each client based on their contract type. For example client_id = 1:

client_id   type   Total Paid   Payments remaining
1           MTV    900          0
1           ML     150          2

I can do the first three columns using this:

SELECT   `client_id`,
         `payment ref` AS `type`,
         SUM(`Amount`) as `Total Paid` 
FROM     transactions
WHERE    client_id = 1
GROUP BY type;

How do I add on the payments remaining column?

CASE WHEN `contract type` = 1 THEN (3 - COUNT(MTV)... ? 
1
  • How do you know, what contract a client has? Commented Aug 6, 2017 at 16:31

3 Answers 3

2

You were pretty close, there was an issue with the GROUP BY clause not including client_id and payment ref:

SELECT 
client_id, 
payment ref AS type, 
SUM(Amount) as 'Total Paid',
CASE 
WHEN contract type = 1 THEN (3 - COUNT(*))
WHEN contract type = 2 THEN (6 -  COUNT(*))
END as 'Payments remaining'
FROM transactions 
WHERE client_id = 1 
GROUP BY client_id, 
payment ref;
Sign up to request clarification or add additional context in comments.

Comments

1

could use case when and group by

  select 
      client_id
      , payment
      , case 
          when `contract type` = 1 and `payment ref` = 'MTV' then  3*300 
          when `contract type` = 1 and `payment ref` = 'ML' then  3*150
          when  `contract type` = 2 then 3*150 
        end as  to_pay
      , sum(Amount) payed
      , case 
          when `contract type` = 1 and `payment ref` = 'MTV' then  3*300/SUM(Amount) 
          when `contract type` = 1 and `payment ref` = 'ML' then  3*150/sum(Amount)
          when  `contract type` = 2 then 3*150 / sum(Amount)
        end number_of
        end as  `Payments remaining`
  from my_table 
  group by client_id, `contract type`, `payment ref`

Comments

1

Try the below query

    select *
       ,case contract_type 
            when 1 then 3-paidCount  
            when 2 then 6-paidCount  
       End               
    from (
        select client_id  , payment_ref ,  contract_type,sum(Amount)TotalPaid ,count(*) paidCount
        FROM     transactions
        group by client_id  , payment_ref ,  contract_type
      ) t

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.