Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Query with an Aggregate Function ???

Status
Not open for further replies.

harleyhead

IS-IT--Management
Apr 19, 2006
17
US
I have 2 tables: Header and Line Items - a one to many, respectively. I want to set a flag on the Header to False when there are no Line Items associated with the Header. However, that would require either a Count of Line Items or some other aggregate function - but can't have that in an update query.

How can I accomplish this?
 
Use the IN statement and a sub query OR an outer join to the line items table and is null criteria on the FK in Line items. The former is more SQL server friendly for converting later... I have no Idea which is faster.

First method select statement (you cab convert it to an update in QBE):

Select *
From Header
Where header.key In (Select [Line Items].Key From [Line Items] Where Line Items].Key = header.key Group By [Line Items].Key Having Count(AnyField) = 0)
 
Oops, Not In is more appropriate...

Select *
From Header
Where header.key Not In (select Key From [Line Items])
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top