Steve
Steve

Reputation: 3

SQL Take a Min of a sub-query with a constraint

I have two Tables A and B, and columns a (in A) and b (in B). These columns have the same values but are different names. Column c in table A has int values. I want to update column d in table B such that:

Each entry in column D is the min of the entries in C that have matching values in a and b.

I wrote this query:

Update B
Set d = min(select A.c From A where A.a = B.b) 
From A, B

But this is an error because I can't run aggregate functions in sub queries. I've looked up how to do this but I can't understand it. Can someone please guide me in the right direction?

Upvotes: 0

Views: 40

Answers (1)

ngrashia
ngrashia

Reputation: 9894

min cannot be used in where clause. However, it can be used in subquery used in where clause. Try the below query. It should do the trick for you.

Update B
Set d = (select min(A.c) From A where A.a = B.b) 

Upvotes: 1

Related Questions