0

首先,我将解释正在捕获的内容。用户具有与其帐户相关联的会员级别(青铜、黄金、钻石等)。需要运行一项夜间作业来计算一年前从今天开始的订单。如果给定用户的订单总额超过或低于一定数量,他们的级别就会升级或降级。存储级别信息的表不会有太大变化,但最小和最大数量阈值可能会随着时间的推移而变化。这是表格的样子:

CREATE TABLE [dbo].[MemberAdvantageLevels] (
[Id] int NOT NULL IDENTITY(1,1) ,
[Name] varchar(255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[MinAmount] int NOT NULL ,
[MaxAmount] int NOT NULL ,
CONSTRAINT [PK__MemberAd__3214EC070D9DF1C7] PRIMARY KEY ([Id])
)
ON [PRIMARY]
GO

我写了一个查询,将按用户对今年迄今为止的订单进行分组。查询包括他们当前的成员级别。

SELECT
Sum(dbo.tbh_Orders.SubTotal) AS OrderTotals,
Count(dbo.UserProfile.UserId) AS UserOrders,
dbo.UserProfile.UserId,
dbo.UserProfile.UserName,
dbo.UserProfile.Email,
dbo.MemberAdvantageLevels.Name,
dbo.MemberAdvantageLevels.MinAmount,
dbo.MemberAdvantageLevels.MaxAmount,
dbo.UserMemberAdvantageLevels.LevelAchievmentDate,
dbo.UserMemberAdvantageLevels.LevelAchiementAmount,
dbo.UserMemberAdvantageLevels.IsCurrent as IsCurrentLevel,
dbo.MemberAdvantageLevels.Id as MemberLevelId,


FROM
dbo.tbh_Orders
INNER JOIN dbo.tbh_OrderStatuses ON dbo.tbh_Orders.StatusID = dbo.tbh_OrderStatuses.OrderStatusID
INNER JOIN dbo.UserProfile ON dbo.tbh_Orders.CustomerID = dbo.UserProfile.UserId
INNER JOIN dbo.UserMemberAdvantageLevels ON dbo.UserProfile.UserId = dbo.UserMemberAdvantageLevels.UserId
INNER JOIN dbo.MemberAdvantageLevels ON dbo.UserMemberAdvantageLevels.MemberAdvantageLevelId = dbo.MemberAdvantageLevels.Id
WHERE
dbo.tbh_OrderStatuses.OrderStatusID = 4 AND
(dbo.tbh_Orders.AddedDate BETWEEN dateadd(year,-1,getdate()) AND GETDATE()) and IsCurrent = 1
GROUP BY
dbo.UserProfile.UserId,
dbo.UserProfile.UserName,
dbo.UserProfile.Email,
dbo.MemberAdvantageLevels.Name,
dbo.MemberAdvantageLevels.MinAmount,
dbo.MemberAdvantageLevels.MaxAmount,
dbo.UserMemberAdvantageLevels.LevelAchievmentDate,
dbo.UserMemberAdvantageLevels.LevelAchiementAmount,
dbo.UserMemberAdvantageLevels.IsCurrent,
dbo.MemberAdvantageLevels.Id

因此,我需要检查 OrdersTotal,如果它超过当前级别阈值,我需要找到适合他们当前订单总额的级别,并使用他们的新级别创建新记录。

例如,假设 jon@doe.com 目前处于铜牌。青铜的 MinAmount 为 0,MaxAmount 为 999。目前他的年度订单为 $2500。我需要找到 2500 美元适合的水平并升级他的帐户。我还需要检查他们LevelAchievmentDate,如果它在当前年份之外,如果没有活动,我们可能需要将用户降级。

我在想我可以创建一个临时表来保存所有级别的结果,然后以某种方式在上面的查询中创建一个 CASE 语句来确定新级别。我不知道这是否可能。或者,迭代我的订单结果并执行其他查询会更好吗?如果我使用迭代模式,我知道我可以使用该When语句来迭代行。

更新

我更新了我的查询一点,到目前为止想出了这个,但我可能需要更多信息,而不仅仅是来自子查询的 ID

Select * into #memLevels from MemberAdvantageLevels


SELECT
Sum(dbo.tbh_Orders.SubTotal) AS OrderTotals,
Count(dbo.AZProfile.UserId) AS UserOrders,
dbo.AZProfile.UserId,
dbo.AZProfile.UserName,
dbo.AZProfile.Email,
dbo.MemberAdvantageLevels.Name,
dbo.MemberAdvantageLevels.MinAmount,
dbo.MemberAdvantageLevels.MaxAmount,
dbo.UserMemberAdvantageLevels.LevelAchievmentDate,
dbo.UserMemberAdvantageLevels.LevelAchiementAmount,
dbo.UserMemberAdvantageLevels.IsCurrent as IsCurrentLevel,
dbo.MemberAdvantageLevels.Id as MemberLevelId,

(Select Id from #memLevels where Sum(dbo.tbh_Orders.SubTotal) >= #memLevels.MinAmount and Sum(dbo.tbh_Orders.SubTotal) <= #memLevels.MaxAmount) as NewLevelId


FROM
dbo.tbh_Orders
INNER JOIN dbo.tbh_OrderStatuses ON dbo.tbh_Orders.StatusID = dbo.tbh_OrderStatuses.OrderStatusID
INNER JOIN dbo.AZProfile ON dbo.tbh_Orders.CustomerID = dbo.AZProfile.UserId
INNER JOIN dbo.UserMemberAdvantageLevels ON dbo.AZProfile.UserId = dbo.UserMemberAdvantageLevels.UserId
INNER JOIN dbo.MemberAdvantageLevels ON dbo.UserMemberAdvantageLevels.MemberAdvantageLevelId = dbo.MemberAdvantageLevels.Id
WHERE
dbo.tbh_OrderStatuses.OrderStatusID = 4 AND
(dbo.tbh_Orders.AddedDate BETWEEN dateadd(year,-1,getdate()) AND GETDATE()) and IsCurrent = 1
GROUP BY
dbo.AZProfile.UserId,
dbo.AZProfile.UserName,
dbo.AzProfile.Email,
dbo.MemberAdvantageLevels.Name,
dbo.MemberAdvantageLevels.MinAmount,
dbo.MemberAdvantageLevels.MaxAmount,
dbo.UserMemberAdvantageLevels.LevelAchievmentDate,
dbo.UserMemberAdvantageLevels.LevelAchiementAmount,
dbo.UserMemberAdvantageLevels.IsCurrent,
dbo.MemberAdvantageLevels.Id
4

2 回答 2

1

一种方法:

with cte as
(SELECT Sum(o.SubTotal) AS OrderTotals,
        Count(p.UserId) AS UserOrders,
        p.UserId,
        p.UserName,
        p.Email,
        l.Name,
        l.MinAmount,
        l.MaxAmount,
        ul.LevelAchievmentDate,
        ul.LevelAchiementAmount,
        ul.IsCurrent as IsCurrentLevel,
        l.Id as MemberLevelId
 FROM dbo.tbh_Orders o
 INNER JOIN dbo.UserProfile p ON o.CustomerID = p.UserId
 INNER JOIN dbo.UserMemberAdvantageLevels ul ON p.UserId = ul.UserId
 INNER JOIN dbo.MemberAdvantageLevels l ON ul.MemberAdvantageLevelId = l.Id
 WHERE o.StatusID = 4 AND
       o.AddedDate BETWEEN dateadd(year,-1,getdate()) AND GETDATE() and
       IsCurrent = 1
 GROUP BY
       p.UserId, p.UserName, p.Email, l.Name, l.MinAmount, l.MaxAmount,
       ul.LevelAchievmentDate, ul.LevelAchiementAmount, ul.IsCurrent, l.Id)
select cte.*, ml.*
from cte
join #memLevels ml 
  on cte.OrderTotals >= ml.MinAmount and cte.OrderTotals <= ml.MaxAmount
于 2013-03-20T18:45:35.037 回答
1

尚未经过语法检查或测试,但应该处理您描述的插入和更新。插入可以使用包含按计算的订单组的派生/虚拟表作为单个语句完成。请注意,插入和更新语句都在同一个事务中完成,以确保同一用户的两条记录不能以 IsCurrent = 1 结尾

INSERT UserMemberAdvantageLevels (UserId, MemberAdvantageLevelId, IsCurrent,
       LevelAchiementAmount, LevelAchievmentDate)
SELECT t.UserId, mal.Id, 1, t.OrderTotals, GETDATE()
FROM
    (SELECT ulp.UserId, SUM(ord.SubTotal) OrderTotals, COUNT(ulp.UserId) UserOrders
     FROM UserLevelProfile ulp
     INNER JOIN tbh_Orders ord ON (ord.CustomerId = ulp.UserId)
     WHERE ord.StatusID = 4
           AND ord.AddedDate BETWEEN DATEADD(year,-1,GETDATE()) AND GETDATE()
     GROUP BY ulp.UserId) AS t
INNER JOIN MemberAdvantageLevels mal
ON (t.OrderTotals BETWEEN mal.MinAmount AND mal.MaxAmount)
  -- Left join needed on next line in case user doesn't currently have a level
LEFT JOIN UserMemberAdvantageLevels umal ON (umal.UserId = t.UserId)
WHERE umal.MemberAdvantageLevelId IS NULL  -- First time user has been awarded a level
      OR (mal.Id <> umal.MemberAdvantageLevelId -- Level has changed 
           AND (t.OrderTotals > umal.LevelAchiementAmount -- Acheivement has increased (promotion)
                OR t.UserOrders = 0))                -- No. of orders placed is zero (de-motion)

/* Reset IsCurrent flag where new record has been added */
UPDATE UserMemberAdvantageLevels
SET umal1.IsCurrent=0
FROM UserMemberAdvantageLevels umal1
INNER JOIN UserMemberAdvantageLevels umal2 On (umal2.UserId = umal1.UserId)
WHERE umal1.IsCurrent = 1
AND umal2.IsCurrent = 2
AND umal1.LevelAchievmentDate < umal2.LevelAchievmentDate)
于 2013-03-20T19:02:24.273 回答