如何根据给定的select语句更新列数据?

时间:2017-01-20 17:48:15

标签: oracle plsql

UPDATE SQ_DEAL_PARTNER SET PRIMARY_FL =PRIMARYFL WHERE  DPID =
    (SELECT DPID, MIN(CASE WHEN DPID IN (SELECT MIN(DPID) 
     FROM SQ_DEAL_PARTNER  WHERE DID=17664 )THEN  'Y' ELSE 'N' END)PRIMARYFL 
    FROM sq_deal_partner dl 
    WHERE DID=17664
     AND PARTNER_TY = 'D'
    GROUP BY DPID);

如何更新上述声明请帮助。

这是我的查询,我希望像这样输出

dpid    primary_fl
123      y
124      n
125      n

1 个答案:

答案 0 :(得分:0)

我建议使用半合并语句:

MERGE INTO SQ_DEAL_PARTNER dest
USING (SELECT DPID, 
              MIN(CASE 
                   WHEN DPID IN (SELECT MIN(DPID) 
                                   FROM SQ_DEAL_PARTNER  
                                  WHERE DID=17664 )
                   THEN 'Y' 
                   ELSE 'N' 
                  END) PRIMARYFL 
         FROM sq_deal_partner dl 
        WHERE DID=17664
          AND PARTNER_TY = 'D'
        GROUP BY DPID) src
ON (src.DPID = dest.DPID)
WHEN MATCHED THEN 
UPDATE SET
dest.PRIMARY_FL = src.PRIMARYFL;