各位师兄 我想建个view 但不知道怎么写sql 麻烦你们指点下 大恩不言谢
 字段1 字段2
 2 2013-08-01
 2 2013-01-01
 2 2012-05-01
 1 2013-06-26
 1 2013-02-01
 3 2012-12-12
 我要可以根据日期筛选出每个人的第一条数据
 比如我输入日期 2013-07-01 结果是
2 2013-01-01
 1 2013-06-26
 3 2012-12-12

解决方案 »

  1.   

    select a.*
    from tb a
    inner join (select id,max([date]) as [date] from tb group by id)b
    on a.id=b.id and a.[date]=b.[date]
      

  2.   


     create table test(字段1 int ,字段2 datetime)
     insert into test values (2,'2013-08-01')
     insert into test values (2,'2013-01-01')
     insert into test values (2,'2012-05-01')
     insert into test values (1,'2013-06-26')
     insert into test values (1,'2013-02-01')
     insert into test values (3,'2012-12-12')
     select  字段1,字段2=CONVERT(varchar, MAX(字段2), 120 )
     from test
     where 字段2<'2013-07-01'
     group by 字段1
     
    /*
    字段1 字段2
    1 2013-06-26 00:00:00.000
    2 2013-01-01 00:00:00.000
    3 2012-12-12 00:00:00.000
     */
      

  3.   

    create table #tb(id int,[date] datetime)
    insert into #tb
    select 2,'2013-08-01'
    union all select 2,'2013-01-01'
    union all select 2,'2012-05-01'
    union all select 1,'2013-06-26'
    union all select 1,'2013-02-01'
    union all select 3,'2012-12-12'select a.*
    from #tb a
    inner join (select id,max([date]) as [date] from #tb where [date]<'2013-07-01' group by id)b
    on a.id=b.id and a.[date]=b.[date] 
    order by id/*
    1 2013-06-26 00:00:00.000
    2 2013-01-01 00:00:00.000
    3 2012-12-12 00:00:00.000
    */
      

  4.   


    --建临时表
    if object_id('Tempdb..#t') is not null drop table #t
    create table #t(
    id int identity(1,1) not null,
    UserId int null,
    LoginTime datetime null
    )
    --建示例数据
    Insert Into #t
    select 2,'2013-08-01' union all
    select 2, '2013-01-01' union all
    select 2, '2012-05-01' union all
    select 1, '2013-06-26' union all
    select 1, '2013-02-01' union all
    select 3, '2012-12-12'  
    --查询日期
    declare @time datetime
    set @time='2013-07-01 '
    --查询
    select UserId,max(LoginTime) as LoginTime 
    from #t 
    where  LoginTime <@time
    group by UserId --结果
    (6 行受影响)
    UserId      LoginTime
    ----------- -----------------------
    1           2013-06-26 00:00:00.000
    2           2013-01-01 00:00:00.000
    3           2012-12-12 00:00:00.000(3 行受影响)
      

  5.   

    感谢各位大侠的热心帮助  实在惭愧 耽误了大家的时间 我在这里补充下  条件是不固定的 我在代码里面进行增加条件 然后按照查临时表就不知道怎么传条件进去了 因为view的条件都是添加到后面去的
      

  6.   

    先建VIEW,再从前台传条件,是行不通的。那就不要建VIEW了,建立个存储过程,直接传条件后,查询出结果吧。如果查询出的结果要被用到下一步操作,就先缓存到临时表。CREATE TABLE #TEMP... INSERT INTO #TEMP EXEC 存储过程名称 @日期参数
      

  7.   

    先建VIEW,再从前台传条件,是行不通的。那就不要建VIEW了,建立个存储过程,直接传条件后,查询出结果吧。如果查询出的结果要被用到下一步操作,就先缓存到临时表。CREATE TABLE #TEMP... INSERT INTO #TEMP EXEC 存储过程名称 @日期参数
    好的 谢谢你  哈哈 我公司领导们自己开发底层的东西 不支持存储过程 现在只能写sql
      

  8.   

    先建VIEW,再从前台传条件,是行不通的。那就不要建VIEW了,建立个存储过程,直接传条件后,查询出结果吧。如果查询出的结果要被用到下一步操作,就先缓存到临时表。CREATE TABLE #TEMP... INSERT INTO #TEMP EXEC 存储过程名称 @日期参数
    好的 谢谢你  哈哈 我公司领导们自己开发底层的东西 不支持存储过程 现在只能写sql
    可以写在函数里面,就能传参了,
    表值函数支持么?
      

  9.   

    先建VIEW,再从前台传条件,是行不通的。那就不要建VIEW了,建立个存储过程,直接传条件后,查询出结果吧。如果查询出的结果要被用到下一步操作,就先缓存到临时表。CREATE TABLE #TEMP... INSERT INTO #TEMP EXEC 存储过程名称 @日期参数
    好的 谢谢你  哈哈 我公司领导们自己开发底层的东西 不支持存储过程 现在只能写sql
    可以写在函数里面,就能传参了,
    表值函数支持么?
    貌似只支持view跟table 因为他们会把这些转化为实体类 我们只要写where后面的语句
      

  10.   

    居然会能支持 view ,不支持函数
    ----查询view 的sql ,select *  from view
    ---查询表值函数的sql,select * from functionName('参数')
      

  11.   

    select * from tb 是他们固定的 我们只需要写 where 1=1
      

  12.   

    select * from tb 是他们固定的
    我们只需要写('参数')  .是会执行成的