将一个表中的值与查询的结果进行比较?

时间:2013-03-20 17:33:18

标签: sql sql-server-2008

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

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语句迭代行。

更新

我更新了我的查询A,到目前为止提出了这个,但我可能需要更多的信息,而不仅仅是来自SubQuery的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

2 个答案:

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

答案 1 :(得分:1)

尚未经过语法检查或测试,但应处理您描述的插入和更新。插入可以使用派生/虚拟表作为单个语句完成,该表包含通过计算的订单组。请注意,insert和update语句都在同一事务中完成,以确保同一用户的两条记录不能以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)