Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hello

I am inserting some data in my table on different dates but there are more than 1 record for a particular date now i want to fetch top 2 distinct dates...


my fields are like


Sr.No.______CNAME_____PNAME____DATE

1__________RAM________abc______28/12/2012
2__________MOHAN____XYZ______28/12/2012
3__________SHYAM_____CDF______01/01/2013
4__________FFFF________GHH______01/01/2013
5__________lljl___________err______29/01/2013
6__________dfr__________dsf_____29/01/2013


I want to fetch 29 and 1 but the output is 28,29


can any one help me
Posted
Comments
CHill60 4-Dec-12 9:25am    
Can you post the code you used to get that output

Here is an example, hopefully this satisfy what you are looking for

SQL
SET DATEFORMAT DMY

create table test
(
    id int,
    cName varchar(50),
    PName Varchar(50),
    myDate Datetime
)
go

insert into test values (1,'RAM','abc', cast('28/12/2012' as datetime))
insert into test values (2,'MOHAN','XYZ', cast('28/12/2012' as datetime))
insert into test values (3,'SHYAM','CDF', cast('01/01/2013' as datetime))
insert into test values (4,'FFFF','GHH', cast('01/01/2013' as datetime))
insert into test values (5,'lljl','err', cast('29/01/2013' as datetime))
insert into test values (6,'dfr','dsf', cast('29/01/2013' as datetime))

select top 2 mydate
from test
group by mydate
order by mydate desc


drop table test
 
Share this answer
 
Hi,

Date column datatype should be datetime in table

select distinct top 2 dates from order by date desc
 
Share this answer
 
Comments
Pradeep_kaushik 4-Dec-12 9:39am    
i use this query but the output is 28 and 29 it compair only date not month and year
hi you need to use this
Select distinct top 2 date from table order by date desc
 
Share this answer
 
Comments
Pradeep_kaushik 4-Dec-12 9:38am    
i use this query but the output is 28 and 29 it compair only date not month and year
ShivKrSingh 5-Dec-12 4:14am    
Send your table Design. I think you had taken wrong datatype.
Depending on your database, use TOP or LIMIT clause, e.g. with MySQL:
SQL
select distinct DATE from MYTABLE limit 2
 
Share this answer
 
SQL
first set your datatype datetime

Date column datatype should be datetime in table

select distinct top 2 dates from order by date desc
 
Share this answer
 
Comments
Pradeep_kaushik 4-Dec-12 11:49am    
ok but now it takes time also how can i insert only date
bhavesh002 5-Dec-12 1:53am    
use ToShortDateString() method for get only date
try this

select top 2 yourdatecolumnname,count(yourdatecolumnname)
from yourtablename group by yourdatecolumnname

you can also user order by for your according.
 
Share this answer
 
I'm assuming you want the record id's 3-6 as those four records have the top two dates. Try this:

SQL
Select myTable.* from MyTable inner join
(select distinct top 2 Convert(DateTime,[Date],102) as [Date] order by Convert(datetime,[Date],102) desc) TopDates on Convert(datetime,MyTable.[Date],102) = TopDates.[Date]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900