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 |
|
asifbhura
Posting Yak Master
165 Posts |
Posted - 2010-03-26 : 11:10:34
|
| Hi,I want query which should not display first records BY CreatedDateit means i want to query which must display all records without last inserted records.Regards,ASIF |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-26 : 11:17:28
|
| Table(s) Structure(s0?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-03-26 : 11:27:01
|
| [code]CREATE TABLE #tmp (ID INT, CreatedDate DATETIME)GOINSERT INTO #tmpSELECT 1, '2000-01-01'UNION ALL SELECT 2, '2000-01-02'UNION ALL SELECT 3, '2000-01-03'UNION ALL SELECT 4, '2000-01-04'UNION ALL SELECT 5, '2000-01-05'UNION ALL SELECT 6, '2000-01-06'UNION ALL SELECT 7, '2000-01-07'GOSELECT ID, CreatedDateFROM #tmp WHERE CreatedDate != (SELECT MAX(CreatedDate) FROM #tmp)GODROP TABLE #tmpGO[/code]Although, it might be better to do this:[code]DECLARE @MaxDate DATETIMESELECT @MaxDate = MAX(CreatedDate) FROM #tmpSELECT ID, CreatedDateFROM #tmp WHERE CreatedDate != @MaxDate [/code]Using the second method, you guarantee that the MAX(CreatedDate) is only executed once.There are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-27 : 09:12:15
|
| [code]SELECT ID, CreatedDateFROM(SELECT ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) AS Seq,ID, CreatedDateFROM #Tmp)tWHERE Seq>1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-27 : 09:13:19
|
| And if you've multiple records with max date value (batch inserts) and want to exclude all of them ,replace ROW_NUMBER by DENSE_RANK in above query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|