difficult SQL statement that Im trying but no luck.
Discussion
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?
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?
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.
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
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
or something like it.
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.
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
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.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.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff



