在oracle / sql中有条件地更新重复值

时间:2012-02-02 20:04:06

标签: sql oracle plsql

如果任何子标记为已付款,我需要更新付费列中的重复值;然后用同一个客户更新另一个子

以下是示例数据:

DATE    Event_ID    EVENT   CUSTOMER    SUBSCRIBER  Paid
01/24/2011  3513709 Play 1  41285030    38803277    Y
01/24/2011  3513709 Play 1  41285030    45051523    N
01/24/2011  3513709 Play 1  41285030    45051523    Y
01/25/2011  3527194 Play 2  45568242    43673464    Y
01/25/2011  3527194 Play 2  45568242    45023437    N
02/03/2011  3622935 Play 3  19356414    43546915    Y
02/03/2011  3622935 Play 3  19356414    44981229    N
02/07/2011  3674946 Play 4  41240152    38744274    Y
02/07/2011  3674946 Play 4  41240152    44225340    Y
02/07/2011  3674946 Play 4  41240152    44820829    N
03/10/2011  4094832 Play 5  31485304    33260562    Y
03/10/2011  4094832 Play 5  31485304    43922788    Y
03/10/2011  4094832 Play 5  31485304    45022503    N
03/10/2011  4094832 Play 5  31485304    45022503    Y
03/10/2011  4094833 Play 6  31485304    33260562    N
03/10/2011  4094833 Play 6  31485304    43922788    N
03/10/2011  4094833 Play 6  31485304    45022503    N
03/16/2011  4167371 Play 7  19384295    32940366    Y
03/16/2011  4167371 Play 7  19384295    45002390    N
03/17/2011  4179070 Play 8  36882841    38554357    Y
03/17/2011  4179070 Play 8  36882841    44988521    N

所需的输出应该是这样的:

DATE    Event_ID    EVENT   CUSTOMER    SUBS    Paid
01/24/2011  3513709 Play 1  41285030    38803277    Y
01/24/2011  3513709 Play 1  41285030    45051523    Y
01/24/2011  3513709 Play 1  41285030    45051523    Y
01/25/2011  3527194 Play 2  45568242    43673464    Y
01/25/2011  3527194 Play 2  45568242    45023437    Y
02/03/2011  3622935 Play 3  19356414    43546915    Y
02/03/2011  3622935 Play 3  19356414    44981229    Y
02/07/2011  3674946 Play 4  41240152    38744274    Y
02/07/2011  3674946 Play 4  41240152    44225340    Y
02/07/2011  3674946 Play 4  41240152    44820829    Y
03/10/2011  4094832 Play 5  31485304    33260562    Y
03/10/2011  4094832 Play 5  31485304    43922788    Y
03/10/2011  4094832 Play 5  31485304    45022503    Y
03/10/2011  4094832 Play 5  31485304    45022503    Y
03/10/2011  4094833 Play 6  31485304    33260562    N
03/10/2011  4094833 Play 6  31485304    43922788    N
03/10/2011  4094833 Play 6  31485304    45022503    N
03/16/2011  4167371 Play 7  19384295    32940366    Y
03/16/2011  4167371 Play 7  19384295    45002390    Y
03/17/2011  4179070 Play 8  36882841    38554357    Y
03/17/2011  4179070 Play 8  36882841    44988521    Y

3 个答案:

答案 0 :(得分:1)

这要点是

  • 为所有拥有付费订阅的客户创建一个子选择
  • JOIN这个子选择回到您对实际客户的实际表格
  • UPDATE子句
  • 中使用此结果
  • 仅更新付费为N
  • 的位置

SQL声明

UPDATE  YourTable
SET     Paid = 'Y'
FROM    YourTable t
        INNER JOIN (
            SELECT  Customer
            FROM    YourTable
            WHERE   Paid = 'y'
        ) c ON c.Customer = t.Customer
WHERE   Paid = 'N'

答案 1 :(得分:0)

最简单的方法是使用一个天真的子查询:

update events set paid = 'Y' where event_id in (
    select event_id from events where paid = 'Y'
)

答案 2 :(得分:0)

WITH CTE AS (
    SELECT [Event], [Customer], SUM(CASE Paid WHEN 'Y' THEN 1 ELSE 0 END) [Sum]
    FROM tmpTbl
    GROUP BY [EVENT], Customer)

UPDATE tmpTbl SET [Paid] = 'Y'
FROM tmpTbl JOIN CTE AS tbl ON tmpTbl.[Event] = tbl.[Event] AND tmpTbl.[Customer] = tbl.[Customer] AND tbl.[Sum] > 0

示例数据脚本

SELECT * INTO tmpTbl FROM (
SELECT GETDATE() -1 [DATE] ,3513709 [Event_ID],'Play 1' [Event],41285030 [CUSTOMER],38803277 [SUBSCRIBER],'Y' [PAID] UNION
SELECT GETDATE() -1 [DATE] ,3513709 [Event_ID],'Play 1' [Event],41285030 [CUSTOMER],45051523 [SUBSCRIBER],'N' [PAID] UNION
SELECT GETDATE() -1 [DATE] ,3513709 [Event_ID],'Play 1' [Event],41285030 [CUSTOMER],45051523 [SUBSCRIBER],'Y' [PAID] UNION

SELECT GETDATE() -2 [DATE] ,3527194 [Event_ID],'Play 2' [Event],45568242 [CUSTOMER],43673464 [SUBSCRIBER],'Y' [PAID] UNION
SELECT GETDATE() -2 [DATE] ,3527194 [Event_ID],'Play 2' [Event],45568242 [CUSTOMER],45023437 [SUBSCRIBER],'N' [PAID] UNION

SELECT GETDATE() -3 [DATE] ,4094832 [Event_ID],'Play 5' [Event],31485304 [CUSTOMER],43922788 [SUBSCRIBER],'Y' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094832 [Event_ID],'Play 5' [Event],31485304 [CUSTOMER],45022503 [SUBSCRIBER],'Y' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094832 [Event_ID],'Play 5' [Event],31485304 [CUSTOMER],45022503 [SUBSCRIBER],'N' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094832 [Event_ID],'Play 5' [Event],31485304 [CUSTOMER],33260562 [SUBSCRIBER],'Y' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094833 [Event_ID],'Play 6' [Event],31485304 [CUSTOMER],43922788 [SUBSCRIBER],'N' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094833 [Event_ID],'Play 6' [Event],31485304 [CUSTOMER],33260562 [SUBSCRIBER],'N' [PAID] UNION
SELECT GETDATE() -3 [DATE] ,4094833 [Event_ID],'Play 6' [Event],31485304 [CUSTOMER],45022503 [SUBSCRIBER],'N' [PAID]) as tbl

输出

DATE    Event_ID    EVENT   CUSTOMER    SUBS    Paid
02/01/2012  3513709 Play 1  41285030    38803277    Y
02/01/2012  3513709 Play 1  41285030    45051523    Y
02/01/2012  3513709 Play 1  41285030    45051523    Y
01/31/2012  3527194 Play 2  45568242    43673464    Y
01/31/2012  3527194 Play 2  45568242    45023437    Y
01/30/2012  4094832 Play 5  31485304    33260562    Y
01/30/2012  4094832 Play 5  31485304    43922788    Y
01/30/2012  4094832 Play 5  31485304    45022503    Y
01/30/2012  4094832 Play 5  31485304    45022503    Y
01/30/2012  4094833 Play 6  31485304    33260562    N
01/30/2012  4094833 Play 6  31485304    43922788    N
01/30/2012  4094833 Play 6  31485304    45022503    N