Author |
Topic |
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-21 : 10:18:55
|
hi,I have a table like the followingDeclare Table sample(ID int identity,col1 varchar(50),col2 ntext,entered_date datetime)data in the table like1,Sample,Null,6/21/20072,test,null,6/22/2007i want the last entered date ID=2 and entered_date= 6/22/2007,how can i take the last entered date,when i trying to use the MAX(entered_date) in group by column it dose in allow the ntext datatype, is there any way so that i can get the last entered date. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-21 : 10:27:15
|
[code]SELECT *FROM sample sWHERE s.entered_date = (SELECT MAX(entered_date) FROM sample)[/code] KH |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-21 : 10:40:31
|
If datecol is unique, thenSelect top 1 columns from tableOrder by datecol descMadhivananFailing to plan is Planning to fail |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-21 : 10:45:39
|
Actually the data is something like thisDeclare Table sample(ID int,col1 varchar(50),col2 ntext,entered_date datetime)1,Sample,Null,6/21/20071,Sample,Null,6/22/20071,Sample,Null,6/23/20072,test,null,6/22/20072,test,null,6/24/20072,test,null,6/25/2007i want only the last entered value like this.1,Sample,Null,6/23/20072,test,null,6/25/2007so what should i do.., |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-21 : 10:50:50
|
[code]SELECT s.*FROM [sample] s INNER JOIN ( SELECT col1, entered_date = MAX(entered_date) FROM [sample] GROUP BY col1 ) m ON s.col1 = m.col1 AND s.entered_date = m.entered_date[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-21 : 10:54:41
|
Select columns from table where datecol in(select max(datecol) from table group by id)MadhivananFailing to plan is Planning to fail |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 05:26:37
|
Hi,i want only the last entered record all other should be deletedDeclare Table sample(ID int,col1 varchar(50),col2 ntext,entered_date datetime)1,Sample,Null,6/21/20071,Sample,Null,6/22/20071,Sample,Null,6/23/20072,test,null,6/22/20072,test,null,6/24/20072,test,null,6/25/2007i want only the last entered value like this.1,Sample,Null,6/23/20072,test,null,6/25/2007and all other record should be deletedso what should i do.., |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 05:34:33
|
[code]DECLARE @sample TABLE( ID int, col1 varchar(50), col2 ntext, entered_date datetime)INSERT INTO @sampleSELECT 1, 'Sample', NULL, '6/21/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/22/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/23/2007' UNION ALLSELECT 2, 'test', NULL, '6/22/2007' UNION ALLSELECT 2, 'test', NULL, '6/24/2007' UNION ALLSELECT 2, 'test', NULL, '6/25/2007'DELETE sFROM @sample s LEFT JOIN ( SELECT col1, entered_date = MAX(entered_date) FROM @sample GROUP BY col1 ) m ON s.col1 = m.col1 AND s.entered_date = m.entered_dateWHERE m.col1 IS NULLSELECT * FROM @sample/*ID col1 col2 entered_date----------- -------- ---- ------------ 1 Sample NULL 2007-06-23 2 test NULL 2007-06-25 */[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 05:51:10
|
In the delete statement why u have given the where col1 is nullDELETE sFROM @sample s LEFT JOIN ( SELECT col1, entered_date = MAX(entered_date) FROM @sample GROUP BY col1 ) m ON s.col1 = m.col1 AND s.entered_date = m.entered_dateWHERE m.col1 IS NULL |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 05:59:18
|
Sorry sorry... i made mistake, i havent seen the code clearly.., thanks its working In the delete statement why u have given thewhere col1 is nullDELETE sFROM @sample s LEFT JOIN(SELECT col1, entered_date = MAX(entered_date)FROM @sampleGROUP BY col1) mON s.col1 = m.col1AND s.entered_date = m.entered_dateWHERE m.col1 IS NULL |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 06:08:14
|
this will give you the rows that you want to keep.SELECT col1, entered_date = MAX(entered_date)FROM @sampleGROUP BY col1 to remove the rest of records, you left join to the above and check for NULL KH[spoiler]Time is always against us[/spoiler] |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 06:53:34
|
if we have same record then the values are repeatingDECLARE @sample TABLE( ID int, col1 varchar(50), col2 ntext, entered_date datetime)INSERT INTO @sampleSELECT 1, 'Sample', NULL, '6/21/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/22/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/23/2007' UNION ALLSELECT 2, 'test', NULL, '6/22/2007' UNION ALLSELECT 2, 'test', NULL, '6/22/2007' UNION ALLSELECT 2, 'test', NULL, '6/24/2007' UNION ALLSELECT 2, 'test', NULL, '6/25/2007'DELETE sFROM @sample s LEFT JOIN ( SELECT col1, entered_date = MAX(entered_date) FROM @sample GROUP BY col1 ) m ON s.col1 = m.col1 AND s.entered_date = m.entered_dateWHERE m.col1 IS NULLSELECT * FROM @sample/*ID col1 col2 entered_date----------- -------- ---- ------------ 1 Sample NULL 2007-06-23 2 test NULL 2007-06-25 */ |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 07:24:49
|
if you have 2 rows ofSELECT 2, 'test', NULL, '6/25/2007' these 2 rows will be retains KH[spoiler]Time is always against us[/spoiler] |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 07:29:09
|
No way to remove those redundant records kya.. ?? |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 07:35:50
|
do you have a Primary Key in the table ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 07:48:54
|
i dont have the primary key in the table.., actually those table are temporary table.., when ever a record is inserted or updated in the main main the row will be inserted in the temporary table these temporary table have same structure as main table only thing is primary key is removed.. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 07:55:08
|
quote: actually those table are temporary table.
Since it is a temp table, surely you can add a indentity column right ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
rauof_thameem
Starting Member
31 Posts |
Posted - 2007-06-22 : 08:00:04
|
mmmmm Ya.... i can add..., so with primary key how i can remove the redundant data?? |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-06-22 : 08:23:55
|
[code]DECLARE @sample TABLE( pk int identity(1,1), ID int, col1 varchar(50), col2 ntext, entered_date datetime)INSERT INTO @sample ([ID], col1, col2, entered_date)SELECT 1, 'Sample', NULL, '6/21/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/22/2007' UNION ALLSELECT 1, 'Sample', NULL, '6/23/2007' UNION ALLSELECT 2, 'test', NULL, '6/22/2007' UNION ALLSELECT 2, 'test', NULL, '6/24/2007' UNION ALLSELECT 2, 'test', NULL, '6/25/2007' UNION ALLSELECT 2, 'test', NULL, '6/25/2007'DELETE sFROM @sample s LEFT JOIN ( SELECT col1, pk = MIN(pk) FROM @sample x WHERE x.entered_date = (SELECT MAX(entered_date) FROM @sample y WHERE y.col1 = x.col1) GROUP BY col1 ) m ON s.pk = m.pkWHERE m.col1 IS NULLSELECT * FROM @sample[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|