1

我正在构建一个佣金计算器,但我不确定如何进行。我有这样的情况:客户 123 的 saleRep A 在前 2 年获得 2%,然后是 0.5%。我已经知道如何获取客户的年龄。

日期范围是可变的。这意味着一个 salesRep/Customer 组合可以在 1 年、2 年、任何时候拆分,但可能在当年拆分。

那么,我该如何查询和存储呢?我的佣金表目前如下,我需要更改吗?

    CREATE TABLE [dbo].[NCL_Commissions](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ProductType] [varchar](255) NULL,
    [LowCost] [int] NULL,
    [HighCost] [int] NULL,
    [CustCode] [varchar](30) NULL,
    [SalesRep] [varchar](10) NULL,
    [Commission] [float] NULL,
    [MinAge] [smallint] NULL,
    [MaxAge] [smallint] NULL,
 CONSTRAINT [PK_NCL_Commissions] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

当我被告知所有佣金都相同时,我是这样构建它的:

  DECLARE @custCreationDate datetime

    SET @custCreationDate = (   SELECT TOP 1 cast(INVDate as datetime)
                                FROM InvoiceHeader
                                WHERE companycode = @custCode
                                order by recid asc      )

--  PRINT 'Line 54: @custCreationDate: ' + cast(@custCreationDate as varchar)

    --If the customer has existed for less than a year
    IF @custCreationDate > DateAdd(yy, -1, @now)
        BEGIN
            SET @result = 2.00  --Customers in existance for less than a year yeild 2% commission
--          PRINT 'Line 60 - @result: ' + cast(@result as varchar)
            GOTO Exit_Function
        END
    ELSE
        BEGIN
            SET @result = 0.50  --Customers in existance longer yeild 0.5 % commission.
--          PRINT 'Line 66 - @result: ' + cast(@result as varchar)
            GOTO Exit_Function
        END

样本数据(部分问题是寻找有关如何存储的建议)

销售代表 A 的客户 123 在前 2 年获得 1%,比 0.5%
销售代表 B 的客户 456 在前 1 年获得 2%,比 0.75%

在任何给定时间,我都必须能够根据客户的当前年龄获得正确的百分比。因此,如果客户 A 是在 2012 年 6 月 1 日创建的,则佣金为 2%。如果客户是在 2008 年 9 月 5 日创建的,则佣金为 0.5%。

解决方案基于 Gordon Linoff 的回答:

SET @custAgeInMonths = datediff(month, @custCreationDate, @invDate)

    --First look for a customer specific record
    SELECT @result = C.Commission
    FROM NCL_Commissions C
    WHERE C.CustCode = @custCode
    AND C.SalesRep = @salesRep
    AND ProductType in ('L')
    AND @custAgeInMonths BETWEEN C.MinAgeMonths AND C.MaxAgeMonths
4

2 回答 2

1

这似乎是一个连接。

假设您有一个客户表,上面有客户的开始日期和佣金表。您可以计算客户的当前年龄并加入佣金表。这假设客户记录具有销售代表:

select <whatever>
from (select c.*, datediff(day, c.joindate, getdate())/365.25 as AgeYears
      from customer c
     ) c left outer join
     NCL_Commissions com
     on c.CustCode = com.CustCode and
        c.ProductType= com.ProductType and
        c.SalesRep = com.SalesRep and
        c.AgeYears between MinAge and c.MaxAge

鉴于所有连接条件,我会注意不匹配(因此使用左外连接)。

于 2012-08-10T15:30:34.083 回答
0

1)你需要一个有佣金括号的表格(我推荐按月)

CREATE TABLE [dbo].[CommissionsBrakets](
[ID] [int] NOT NULL,
[Commission] [Decimal](4, 4) NULL,
Month [smaillint])

用刹车填满它

1    2%  1 /month 1 2%
1    2%  2 /month 2 2%
....
1  0.5% 24 /month 24 (2 years) 0.5%

2)您需要一个与客户+salesrep+comisionBracket 相关的表格

CREATE TABLE [dbo].[NCL_Commissions](
[ID] [int] IDENTITY(1,1) NOT NULL,   
[CustCode] [varchar](30) NULL,
[SalesRep] [varchar](10) NULL,
[CommissionID] [int] NULL)

然后你可以加入

declare @mothDiff int = DATEDIFF(m, @custCreationDate, GETDATE())


Select b.Commission from NCL_Commissions C inner join CommissionsBrakets B 
on c.CommissionID  =   B.ID 
where 
B.month = case 
    when @mothDiff > ((select max(Month) from CommissionsBrakets CB where CB.id = B.ID   ))
    then (select max(Month) from CommissionsBrakets CB where CB.id = B.ID   )
    else @mothDiff
    end
于 2012-08-10T15:50:53.730 回答