从数据库中选择不同的前2个日期 [英] select distinct top 2 dates from database
问题描述
你好
我在不同的日期在我的表中插入一些数据,但是特定日期的记录超过1条现在我想要获取前2个不同的约会...
我的字段就像是
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
我想拿29和1,但输出是28,29
任何人都可以帮我
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
推荐答案
这是一个例子,希望这能满足你的需求
Here is an example, hopefully this satisfy what you are looking for
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
日期列数据类型应该在表
datetime />
从日期排序中选择不同的前2个日期desc
你需要使用此
按日期desc从表顺序中选择不同的前2个日期
hi you need to use this
Select distinct top 2 date from table order by date desc
这篇关于从数据库中选择不同的前2个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!