2013-10-09 23 views
-1

我有一个表[报名]具有以下字段更新领域:SQL基于其他领域的计数行的表

1)MEMBERID 2)尝试

我的一些尝试是空值。

我想企图场更新为0的那些行,其中MEMBERID表中的计数为1

任何帮助表示赞赏。

回答

0
UPDATE applicants 
SET Attempts = 0 
WHERE MemberID IN 
    (SELECT MemberID FROM applicants GROUP BY MemberID HAVING COUNT(MemberID)=1) 
    AND Attempts IS NULL 
+0

感谢那些工作。但是,你为什么按MemberID分组? – vitalious

+0

'GROUP BY具有COUNT(MemberID)= 1'的GROUP BY允许获得在组中计数的'MemberID'-s等于1。 –

1

一般的做法是

update applicants set 
    Attempts = 0 
where 
    MemberID in (select t.MemberID from applicants as t group by t.MemberID having count(*) = 1) and 
    Attempts is null -- if you need it 
在SQL Server

,你可以这样做:

with cte as (
    select *, count(*) over(partition by MemberID) as cnt 
    from applicants 
) 
update cte set 
    Attempts = 0 
where cnt = 1 and Attempts is null