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 |
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2010-08-18 : 11:47:55
|
| A Table has many line items to one, so I tightened up the criteria so it would only return one line item per id. Demo table is one line item per record.SELECT A.ID, A.TYPE, Max(A.DATE) AS MaxOfDATE, DEMO.EXPIREFROM A INNER JOIN DEMOON A.ID = DEMO.IDGROUP BY A.ID, A.TYPE, DEMO.EXPIREHAVING (((A.TYPE)='order') AND ((Max(A.DATE))<> DEMO.EXPIRE))ID TYPE MaxDATE EXPIRE13 ORDER 2010-08-16 00:00:00.000 2010-12-31 00:00:00.00015 ORDER 2011-08-25 00:00:00.000 2011-08-16 00:00:00.000I need to eb able to update the expire date with the Max Date, but the query below throws an error:"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression"update DEMOset DEMO.EXPIRE = (SELECT Max(A.DATE) FROM A INNER JOIN DEMOON A.ID = DEMO.IDGROUP BY A.ID, A.TYPE, DEMO.EXPIREHAVING ((A.TYPE)='order') AND (Max(A.DATE))<> DEMO.EXPIRE)Thanks!dz |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2010-08-18 : 12:35:14
|
| Primary keys:Table A = SEQNTable Demo = IDThanks! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-08-18 : 12:55:19
|
or maybe:UPDATE DSET EXPIRE = V.MaxDateFROM DEMO D JOIN ( SELECT A.ID, Max(A.DATE) AS MaxDate FROM A WHERE A.TYPE = 'order' GROUP BY A.ID ) V ON D.ID = V.IDWHERE D.EXPIRE <> V.MaxDate |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2010-08-18 : 13:52:40
|
| That worked, but what is the V table a temp table? Can you point me to any links/articles on how to do this so I can get a better understanding?Thanks,dz |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-18 : 13:59:42
|
quote: Originally posted by dzabor That worked, but what is the V table a temp table? Can you point me to any links/articles on how to do this so I can get a better understanding?Thanks,dz
Yes...and no...it's called a derived table..and like a temp, it's in tempdb..unless they changed it, then it's in memoryBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-08-18 : 14:31:29
|
| Small correction -- A derived table is stored in memory, not tempdb. |
 |
|
|
|
|
|
|
|