Update table using MIN() Function

I am trying to update a date field using the MIN() function. when I run the code below, I just get one date  result, instead of a result for each appropriate row in the temp table.  Any one any ideas on what I am doing wrong here. All date fields are SMALLDATETIME.

SET datefst = (SELECT MIN(ar.sales_date)
                        FROM prodSales ar
                        JOIN #TCI i
                        ON i.ProdCode = ar.Prodcode
                        AND i.catl = ar.catl
                        WHERE ar.sales_date BETWEEN i.fdate AND i.End_Date)
JOIN prodSales ar
ON i.ProdCode = ar.ProdCode
AND i.catl = ar.catl
AND i.datefst IS NULL;

Many thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Morpheus7.

Right off the bat, you have too many references to the two tables. How about trying something like this:

update i
	set	datefst = (
		select min(ar.sales_date)
		from prodSales ar
		where ar.sales_date between i.fdate and i.End_Date
			and i.ProdCode = ar.Prodcode
			and i.catl = ar.catl
	from #TCI i
	where i.datefst is null;

Open in new window

Let us know if that works better.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you explain the logic of what you trying to do?
Morpheus7Author Commented:
That works Great, many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.