要求显示纪录 当天纪录随机排序,非当天纪录按最近时间排序
-----------------------------------
例: 表A 
-----------------------------------
 ID  Name    pubtime
  1  赵大  2008-06-16
  2  钱二  2008-06-16
  3  张三  2008-06-16
  4  李四  2008-06-01
  5  王五  2008-06-02
  6  杨六  2008-05-16
...
-----------------------------------
查询结果就应该是:
  赵大,钱二,张三为当天纪录,随机排序;
  李四,王五,杨六非当天纪录,按时间排,应该是王五,李四,杨六
-----------------------------------
存储过程,视图都可以,查询语句都可以 实现就好~

解决方案 »

  1.   


    declare @table1 table
    (
    id INT IDENTITY,
    [name] varchar(50),
    pubtime varchar(50)
    )insert into @table1 ([name],pubtime)
    values('name1','2008-06-16 ')insert into @table1 ([name],pubtime)
    values('name2','2008-06-16 ')insert into @table1 ([name],pubtime)
    values('name3','2008-06-16 ')insert into @table1 ([name],pubtime)
    values('name4','2008-06-01 ')insert into @table1 ([name],pubtime)
    values('name5','2008-06-02 ')
    select * from @table1 order by pubtime desc,newid()
      

  2.   

    直接复制我的代码放到查询分析器中run一下
      

  3.   


      SELECT * FROM  表A ORDER BY pubtime DESC, NEWID()
      

  4.   

    select * from b where convert(varchar,pubtime,112)>=convert(varchar,getdate(),112)
    union
    select * from b where  convert(varchar,pubtime,112)<convert(varchar,getdate(),112) order by pubtime desc
      

  5.   

    lovehongyun
    能将下你那实现的原理吗。最后的查询!!!!谢谢
      

  6.   

    order by pubtime desc,newid()按你的日期desc排序,如果日期相同的话就按newid()排newid()生成的就是guidguid知道是什么吧?
      

  7.   

    我上边写的一大堆是为了方便测试declare @table1 table //定义一个表变量然后insert测试数据.然后select...
      

  8.   

    create  table table1
    (
    id INT IDENTITY,
    [name] varchar(50),
    pubtime varchar(50)
    )insert into table1 ([name],pubtime)
    values('name1','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name2','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name3','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name4','2008-06-01 ')insert into table1 ([name],pubtime)
    values('name5','2008-06-02 ')
    select [name],'今天' as dt from table1 where pubtime=convert(nvarchar(10),'2008-06-16')  group by [name] --将'2008-06-16'换成现在的时间
    union all
    select [name],'以前' as dt from table1 where pubtime!=convert(nvarchar(10),'2008-06-16')  group by [name] --将'2008-06-16'换成现在的时间
    drop table  table1
      

  9.   

    谢谢了。
     guid 就是说随即。!?
      

  10.   

    ate  table table1
    (
    id INT IDENTITY,
    [name] varchar(50),
    pubtime varchar(50)
    )insert into table1 ([name],pubtime)
    values('name1','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name2','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name3','2008-06-16 ')insert into table1 ([name],pubtime)
    values('name4','2008-06-01 ')insert into table1 ([name],pubtime)
    values('name5','2008-06-02 ')declare @today nvarchar(1000)
    declare @before nvarchar(1000)set @today=''
    set @before=''
    select @today=@today+[name]+',' from table1 where pubtime=convert(nvarchar(10),'2008-06-16') 
    print(@today)set @before=''
    select @before=@before+[name]+',' from table1 where pubtime!=convert(nvarchar(10),'2008-06-16') 
    print(@before)drop table  table1
      

  11.   

    =================
    我个人感觉,前辈您这用法是无法在实际SQL中使用的。因为数据库存储的不一定是LZ的那种时间格式。通常都是含有具体几分几秒的。前辈这用法用的太有局限性。
      

  12.   

    SELECT * FROM  表A ORDER BY pubtime DESC, NEWID()