如何根据日期范围/年龄进行存储和查询?

时间:2012-08-10 15:07:49

标签: sql sql-server database database-design

我正在建立佣金计算器,我不知道如何继续。我有这样的情况:SaleRep A对于客户123在前2年获得2%,然后是.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

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

salesrep的客户123前两年获得1%,而不是.5% salesrep B的客户456在前1年获得2%,而不是0.75%

在任何时候,我必须能够根据客户的当前年龄获得正确的百分比。因此,如果客户A是在2012年6月1日创建的,则佣金为2%。如果客户是在2008年9月5日创建的,则佣金为.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

2 个答案:

答案 0 :(得分: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

考虑到所有的连接标准,我会注意不匹配(因此使用左外连接)。

答案 1 :(得分: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)你需要一张与costumer + 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