difficult SQL statement that Im trying but no luck.
difficult SQL statement that Im trying but no luck.
Author
Discussion

Dupont666

Original Poster:

22,946 posts

223 months

Friday 16th April 2010
quotequote all
UPDATE TableA
SET TradeDate = min(b.TradeDate)
FROM TABLEA a, TABLEB b
WHERE a.DealId = b.DealId
and a.TransactionId = b.TransactionId
and b.Sendfile = 1
and min(b.TradeDate) < a.TradeDate

Comes back with the aggregate error, but Im not wanting to use a temp table as it needs to be done as the code goes along before moving to the next deal.

Any bright ideas?

Size Nine Elm

5,167 posts

315 months

Friday 16th April 2010
quotequote all
Try 'Select min(b.TradeDate)' instead fo just min...

ewenm

28,506 posts

276 months

Friday 16th April 2010
quotequote all
UPDATE tableA a
SET a.tradeDate = (Select min(b.tradeDate)
from tableb b
where a.dealID = b.dealID
and a.transactionID = b.transactionID
and b.sendfile = 1
and b.tradeDate < a.tradeDate)

Try that. You might need to put another where clause at the end to specify the tableA record you want to update.

Edited by ewenm on Friday 16th April 18:00

otolith

69,479 posts

235 months

Friday 16th April 2010
quotequote all
Not clear what you are trying to do - is there meant to be some aggregation or not?

Are you trying to set TradeDate in TableA to the minimum value of TradeDate in all of TableB or to the minimum value of TradeDate in TableB with the same deal and/or transaction ID?

If it's the latter, a subquery containing the aggregate is how I would do it - something like


UPDATE
TableA
SET
TradeDate=X.TradeDate
FROM
TableA A
join
(
SELECT
TransactionID,
DealID,
min(TradeDate) as TradeDate
FROM
TableB
GROUP BY
TransactionID,
DealID
) X on X.TransactionID=A.TransactionID and X.DealID=A.DealID
WHERE
X.TradeDate < A.TradeDate


or something like it.

Dupont666

Original Poster:

22,946 posts

223 months

Friday 16th April 2010
quotequote all
otolith said:
Not clear what you are trying to do - is there meant to be some aggregation or not?

Are you trying to set TradeDate in TableA to the minimum value of TradeDate in all of TableB or to the minimum value of TradeDate in TableB with the same deal and/or transaction ID?

If it's the latter, a subquery containing the aggregate is how I would do it - something like


UPDATE
TableA
SET
TradeDate=X.TradeDate
FROM
TableA A
join
(
SELECT
TransactionID,
DealID,
min(TradeDate) as TradeDate
FROM
TableB
GROUP BY
TransactionID,
DealID
) X on X.TransactionID=A.TransactionID and X.DealID=A.DealID
WHERE
X.TradeDate < A.TradeDate


or something like it.
you got the winner, effectively I put a temp table in the middle of the query in the form of a derived table.... it was a bugger to work out.

ewenm

28,506 posts

276 months

Saturday 17th April 2010
quotequote all
You might find that inefficient as you group all the records in table B each time you call it, rather than just looking at the records you're interested in.

Dupont666

Original Poster:

22,946 posts

223 months

Saturday 17th April 2010
quotequote all
ewenm said:
You might find that inefficient as you group all the records in table B each time you call it, rather than just looking at the records you're interested in.
in my case its one record in the derived table at a time smile