T-SQL Calculate Percentage

喜夏-厌秋 提交于 2020-01-06 18:07:27

问题


I have a SQL Server (2000) database with customers (health club).

The customers join on a yearly basis. I want to be able to calculate the percentage of customers who renew there membership (over time and an the past current year). The Active Status field indicates whether they are still a member.
The JoinDate doesn't change. When a member renews there is an ActiveDate and also a DueDate (when a customer is due to renew), the DueDate is 1 year ahead of the Active Date. What I am looking to do is try to get some sense of the % percentage of the customers that renew, broken down by past years if possible or overall. Any ideas. Thanks. The fields in the customer table contain:

    CustomerID    JOIN Date    Cancel Date   ActiveStatus ActiveDate   DueDate

     12345        10/01/2011   NULL          Yes         10/01/2012  10/01/2013

     12346        1/1/2010     12/31/2011    No          1/1/2010     1/1/2011

回答1:


Ok, if ActiveDate is after Join_Date we know customer renew the membership. To get percentage of customers who renew the mebership we need to get:

number of customers who have ActiveDate > JOIN_Date (customers who renew membership)

number of customers who renew membership multiply with 100

this result divide with sum number of all customers

select 
(select COUNT(*)  from Customer where ActiveDate > JOIN_Date) * 100 / COUNT(*)
from Customer



回答2:


You can calculate the number of renewals per customer like this..

select
    *,
    datediff(yyyy, joindate, 
         case activestatus when 'yes' then  getdate() else CancelDate end
    )
from yourtable


来源:https://stackoverflow.com/questions/13111153/t-sql-calculate-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!