Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-09-15 : 09:49:29
|
I have two tables:Client-----clientID FirstPaymentDate1 null2 null3 null4 null5 nullPayments--------clientID PaymentDate2 02/07/20081 03/07/20082 07/08/2008 How can I update the Client table so thatthe FirstPaymentDate is the oldest PaymentDate for that ClientID if the FirstPaymentDate field is null in the Client table please?So I would get the result:Client-----clientID FirstPaymentDate1 03/07/20082 02/07/20093 null4 null5 null *UK format dates |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-15 : 09:55:30
|
| Update cset c.FirstPaymentDate=p.PaymentDate from Client as c inner join (select clientID, min(PaymentDate) as PaymentDate from payments group by Clientid) as pon c.Clientid=p.ClientidMadhivananFailing to plan is Planning to fail |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-15 : 10:11:20
|
[code]DECLARE @Client TABLE(clientID INT, FirstPaymentDate DATETIME)INSERT INTO @Client SELECT 1, null UNION SELECT2, null UNION SELECT3, null UNION SELECT4, null UNION SELECT5, nullDECLARE @Payments TABLE(clientID INT, PaymentDate DATETIME)INSERT INTO @Payments SELECT2, '02/07/2008' UNION SELECT1, '03/07/2008' UNION SELECT2, '07/08/2008'SELECT c.clientID, p.PaymentDateFROM @Client c LEFT JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY clientID ORDER BY PaymentDate DESC)x, PaymentDate, clientID FROM @Payments)p on c.clientID=p.clientIDWHERE x=1 OR p.PaymentDate IS NULL[/code]not sure correct...but hope can help you Hope can help...but advise to wait pros with confirmation... |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-09-15 : 10:12:12
|
OMG Hope can help...but advise to wait pros with confirmation... |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2009-09-15 : 10:21:49
|
| Many thanks guys. |
 |
|
|
|
|
|
|
|