Skip to content
Advertisement

SQL Count depending on certain conditions

I have two tables.

One have userid and email (users table). The other have payments information (payments table) from the userid in users.

I want to count the PaidMonths taking in consideration the following rules:

  • If ValuePaid < 10 PaidMonths should be = 0.23 (even if in the column the value seen is any other mumber).
  • If Refund=1 the PaidMonths should be = 0.

Based on this when i join both tables by userid, and sum the PaidMonths based in the previousrules, i expect to see as result:

Can you help me to achieve this in the most elegant way? Should a temporary table be used?

Advertisement

Answer

The following gives your desired results, using apply with case expression to map your values:

See Working Fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement