我的SQL非常生疏。我试图以嵌套查询的形式更新同一个表中COUNT(*)的表中一行的计数器。 SQL如下:
UPDATE DWInvoiceHeader AS A
SET A.InvCount = (Select Count(B.HIINV) From DWInvoiceHeader AS B
WHERE (B.HIVENT = '0')
Group By B.HIINV
Order By B.HIINV)
WHERE (A.HIVENT = '0');
行看起来像:
HIINV1.......Seq1.....InvCount - want InvCount to be 3
HIINV1.......Seq2.....InvCount - want InvCount to be 3
HIINV1.......Seq3.....InvCount - want InvCount to be 3
HIINV2.......Seq1.....InvCount - want InvCount to be 2
HIINV2.......Seq2.....Invcount - want InvCount to be 2
.
.
.
HIINVn.......Seq1.....InvCount - want InvCount to be 1
上面的SQL给出了消息“操作必须是可更新的查询”。
有什么想法吗?
答案 0 :(得分:0)
UPDATE DWInvoiceHeader AS A
SET A.InvCount =
(
Select Count(B.HIINV)
From DWInvoiceHeader AS B
WHERE (B.HIVENT = '0') AND (A.HIINV = B.HIINV)
Group By B.HIINV
)
WHERE (A.HIVENT = '0');
答案 1 :(得分:0)
您可以使用类似于:
的方式使用域功能DcountUPDATE test
SET InvCount = Dcount("HIVENT","test","[HIVENT]='0'")
如果您想使用通用方法,那么我建议将计数存储在工作表中,然后更新加入工作表的表。
Select B.HIINV,Count(B.HIINV) as InvCount
into WorkingTbl
From DWInvoiceHeader AS B
WHERE (B.HIVENT = '0') AND (A.HIINV = B.HIINV)
Group By B.HIINV
Update DWInvoiceHeader A
Inner Join WorkingTbl b
on a.HIINV=b.HIINV
Set A.InvCount=b.InvCount