1
ID ProductId MemberId SessionId CreatedOn 数量
62 1 2 2012-09-11 1
63 2 2 2012-09-11 1
64 1 0 agzdeoqubwokfme05wwmne55 2012-09-11 1
65 3 0 agzdeoqubwokfme05wwmne55 2012-09-11 1

我有上表,我需要做一个程序。我的场景是没有登录将商品添加到购物车,当您登录时合并所有商品。

我将在程序@MemberId(登录会员ID)中传递两个参数,@SessionID

我想要如下所示的表格。

ID ProductId MemberId SessionId CreatedOn 数量
62 1 2 2012-09-11 2
63 2 2 2012-09-11 1
65 3 2 2012-09-11 1

一种产品与另一种产品合并,数量为 2。那一行应该消失了。

4

3 回答 3

2

假设 SQL SERVER 2005 或以上

CREATE TABLE #T (ID INT , ProductId INT,  MemberId INT,   SessionId VARCHAR(100),  CreatedOn DATETIME,   Quantity INT)
INSERT INTO #T SELECT 62,1 ,2 ,' ','2012-09-11',1
INSERT INTO #T SELECT 63,  2,2, ' ','2012-09-11',1
INSERT INTO #T SELECT 64,  1,0,'agzdeoqubwokfme05wwmne55','2012-09-11',1
INSERT INTO #T SELECT 65,  3,0,'agzdeoqubwokfme05wwmne55','2012-09-11',1

;With CTE AS 
(SELECT 
    ID
    ,ProductId
    ,MemberId = MAX(MemberId) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,SessionID
    ,CreatedOn = CONVERT(VARCHAR(10),MAX(CreatedOn) OVER(PARTITION BY ProductID ORDER bY(SELECT 1)),20)
    ,Quantity = SUM(Quantity) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,Rn = ROW_NUMBER() OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
FROM #T)

SELECT ID,ProductId,MemberId,SessionID,CreatedOn,Quantity FROM CTE WHERE Rn = 1

DROP TABLE #T

将给出结果为

ID  ProductId   MemberId    SessionID              CreatedOn    Quantity
62  1            2                                  2012-09-11    2
63  2            2                                  2012-09-11    1
65  3            0          agzdeoqubwokfme05wwmne55 2012-09-11   1

最后,如果您想更新表格,请点击此处

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = c.CreatedOn
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN CTE c
ON #T.ID = c.ID
WHERE c.Rn = 1

结果是

ID  ProductId   MemberId    SessionId               CreatedOn   Quantity
62  1              2                                 2012-09-11   2
63  2              2                                 2012-09-11   1
64  1              0    agzdeoqubwokfme05wwmne5      2012-09-11   1
65  3              0    agzdeoqubwokfme05wwmne55     2012-09-11   1

完整的查询是

;With CTE AS 
(SELECT 
    ID
    ,ProductId
    ,MemberId = MAX(MemberId) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,SessionID
    ,CreatedOn = CONVERT(VARCHAR(10),MAX(CreatedOn) OVER(PARTITION BY ProductID ORDER bY(SELECT 1)),20)
    ,Quantity = SUM(Quantity) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,Rn = ROW_NUMBER() OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
FROM #T)

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = CONVERT(VARCHAR(10),c.CreatedOn,20)
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN CTE c
ON #T.ID = c.ID
WHERE c.Rn = 1

SELECT * FROM #T

DROP TABLE #T

对于任何版本的 SQL

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = c.CreatedOn
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN (SELECT t.ID,t.ProductID,t.MemberId,t.SessionId,t.CreatedOn,x.Quantity
            FROM #T t 
            JOIN (
                    SELECT
                            ID = MIN(ID)
                            ,ProductID 
                            ,MemberId = MAX(MemberId)       
                            ,CreatedOn = MAX(CreatedOn)
                            ,Quantity = SUM(Quantity) 
                    FROM #T 
                    GROUP BY ProductID
                    )X
            ON t.ID =X.ID) c
ON #T.ID = c.ID


SELECT * FROM #T
DROP TABLE #T
于 2012-09-12T03:54:35.933 回答
1
SELECT ProductID, 
       max(MemberId), 
       max(CreatedOn),
       sum(Quantity) 
FROM theTable 
WHERE SessionID=<id> or MemberID=<id>
GROUP BY ProductID
于 2012-09-12T02:45:01.573 回答
1

更新可能不是最佳解决方案,最好删除所有记录并插入新记录。

无论如何,如果你想更新然后删除额外的,你需要做这样的事情。

假设ID是这里的唯一键。这是完整的逻辑,请仔细阅读

DECLARE @Temp1 TABLE 
(
--datafields same AS your table

)

DECLARE @Temp2 TABLE 
(
--datafields same AS your table
)

INSERT INTO @Temp1
SELECT ID,
       ProductID, 
       MemberId,
       SessionId, 
       CreatedOn,
       Quantity 
FROM theTable 
WHERE SessionID= @SessionID  or MemberID= @MemberID 

UPDATE a
SET SessionID =  @SessionID  , MemberID= @MemberID 
FROM @Temp1 AS a
--WHERE  SessionID= @SessionID  OR MemberID= @MemberID 

INSERT INTO @Temp2
SELECT MAX(ID),
       ProductID, 
       max(MemberId),
       MAX(SessionId),
       max(CreatedOn),
       sum(Quantity) 
FROM @Temp1
GROUP BY ProductID 

UPDATE a
SET a.MemberId = b.MemberId ,a.SessionId=b.SessionId ,a.CreatedOn=b.CreatedOn,a.Quantity=b.Quantity 
FROM YourTable AS a
INNER JOIN @Temp2 AS b
ON a.ID=b.ID      

DELETE  FROM YourTable
WHERE   ID IN ( SELECT  ID
                    FROM    @Temp1 AS ta
                    WHERE   NOT EXISTS ( SELECT 1
                                         FROM   @Temp2 AS tb
                                         WHERE  ta.ID = tb.ID ) ) 
于 2012-09-12T04:04:33.803 回答