My SQL is very rusty. Am trying to update a counter in a row in a table with a COUNT(*) from that same table in the form of a nested query. The SQL is below:
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');
The rows look like:
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
The SQL above gives me the message "Operation must be an updateable query".
Any ideas ?
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');
You could use the Domain Function Dcount in a way similar to:
UPDATE test
SET InvCount = Dcount("HIVENT","test","[HIVENT]='0'")
If you want to use a generic approach then I would suggest to store the counts in a working table and then update your table joining the working table.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.