简体   繁体   English

使用子查询更新列

[英]Update column using Subquery

I am trying to update eid of the pay table with eid of payc table by joining payid .In the PayC table there are duplicate eid s' for matched payid s',Need to get only one EID for every matched PAYID 我正在尝试通过加入payid用payc表的eid更新pay表的eid。在PayC表中,匹配的payid s有重复的eid s',需要为每个匹配的PAYID仅获得一个EID

update PAY
SET eid = (select t.EID from (select a.EID,a.PAYID, count(a.EID) as cnt 
                                                       from PAYC a,PAYC b
                                                            where a.payid = b.payid
                                                            group by a.PAYID,a.eid
                                                            having count( a.eid) > 1) t
                                                            where  T.payid = PAYID)
WHERE EXISTS (select 'X' from (select a.EID,a.PAYID, count(a.EID) as cnt 
                                                       from PAYC a,PAYC b
                                                            where a.payid = b.payid
                                                            group by a.PAYID,a.eid
                                                            having count( a.eid) > 1) t
                                                            where  T.payid = PAYID)

Error:Subquery returned more than 1 value. 错误:子查询返回了多个值。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

we can find duplicate EID by this query BUT NEED TO FETCH ONLY ONE EID 我们可以通过此查询找到重复的EID,但只需要获取一个EID

select a.EID,COUNT(a.PAYID), count(a.EID) as cnt 
 from PAYC a,PAYC b
 where a.payid = b.payid
 group by a.PAYID,a.eid
 having count( a.eid) > 1 AND 
 COUNT(a.PAYID) >1 

sample data 样本数据

paycid payid  eid  amount year  quarter
1       101  1000   2000  2001   1
2       101  1000   3000  2001   2

Need to fetch 需要获取

eid
1000

for the matched payid 匹配的Payid

select top 1 * from stuff

可能会。

Something like this might help? 这样的事情可能会有所帮助?

WITH Duplicates AS (
    SELECT
        a.EID,
        a.PAYID,
        ROW_NUMBER() OVER (PARTITION BY a.PAYID ORDER BY a.EID) AS ROWID
    FROM
        PAYC a
        INNER JOIN PAYC b ON b.PAYID = a.PAYID
    GROUP BY
        a.EID,
        a.PAYID
    HAVING
        COUNT(a.EID) > 1)       
UPDATE
    p
SET
    EID = d.EID
FROM
    PAY p
    INNER JOIN Duplicates d ON d.PAYID = p.PAYID AND d.ROWID = 1;

Although I have to admit that I don't understand the logic of your self-join from PAYC to PAYC. 尽管我不得不承认,我不理解您从PAYC到PAYC的自我加入的逻辑。 Surely you don't need to do this to find cases where there are multiple EIDs per PAYID? 您肯定不需要这样做来查找每个PAYID有多个EID的情况吗? In which case you could just remove the INNER JOIN PAYC b line entirely? 在那种情况下,您可以完全删除INNER JOIN PAYC b行?

After thinking about this some more, could you not simply do this? 再多考虑一下之后,您能不能简单地做到这一点?

WITH Duplicates AS (
    SELECT
        PAYID,
        MAX(EID) AS EID
    FROM
        PAYC
    GROUP BY
        PAYID
    HAVING
        COUNT(DISTINCT EID) > 1)        
UPDATE
    p
SET
    EID = d.EID
FROM
    PAY p
    INNER JOIN Duplicates d ON d.PAYID = p.PAYID;

Then use MAX, MIN or whatever aggregation your require to pick the EID you want to use to update your PAY table (my example uses MAX). 然后使用MAX,MIN或您需要的任何聚合来选择要用于更新PAY表的EID(我的示例使用MAX)。 This is why your original query didn't work, you didn't tell it which EID to use, and it was returning multiple values. 这就是为什么您的原始查询不起作用,您没有告诉它要使用哪个EID以及它返回多个值的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM