首先谢谢各位刚才的踊跃指导,刚刚的帖子小弟已经结贴给分了,
是我自己的原因,刚刚的帖子抽象出来的实例有点不太对头,以至于我都没法套用各位给的语句来测试一下,下面重新整理,请各位继续指教A 表 (客户信息表)
id,  name
1    张三
2    李四B 表 (客户车辆保险续费表)
id   aid  time  money
1    1    2008  1000
2    2    2008  1200
3    1    2009  1500
4    2    2009  1600C 表 (客户购车信息)
id  aid  carNumber
1   1    冀C9987
2   2    京P4489其中B,C表里面的aid对应A表id现在我想在不输入任何查询条件时示所有客户最后一次缴纳车辆保费的信息
name carNumber time money
张三 冀C9987   2009 1500
李四 京P4489   2009 1600也可以输入客户名称或车牌号进行查询具体的某一条记录,比如输入“张三”可查询出
name carNumber time money
张三 冀C9987   2009 1500谢谢各位大侠!

解决方案 »

  1.   

    本帖最后由 roy_88 于 2011-07-19 16:55:01 编辑
      

  2.   

    select a.name,c.carNumber,b.time,b.money
    from a,b b1,c
    where a.id = b1.aid
    and a.id = c.aid
    and not exists (
      select 1 from b
      where aid = b1.aid
      and time > b1.time 
      )
    -- and a.name ='张三'    --这个条件在查询张三数据时使用
      

  3.   

    select name,carNumber,time,money
    from a,b,c
    where a.id=b.aid and a.id=c.aid
    and not exists 
    (select 1 from b as tb where id=b.id and time>b.time)
    --and name='张三'
      

  4.   


    select a.name,c.carNumber ,b.time ,b.money
    from A a left join B b on a.id=b.aid
             left join C c on a.id=c.aid
    where not exists(select 1 from B bb where b.aid=bb.aid and b.time<bb.time)
      

  5.   

    你那个维护表里怎么没车辆ID
    SELECT A.NAME,C.CARNUMBER,B.TIME,B.MONEY 
    FROM A
    LEFT JOIN (
    SELECT TIME,MONEY FROM (
    SELECT TIME,MONEY,ROW_NUMBER() OVER(PARTITION BY AID ORDER BY TIME DESC,ID DESC) NID FROM B 
    ) T WHERE NID=1
    ) B ON A.ID=B.AID
    LEFT JOIN C ON A.ID=C.AID
    WHERE 1=1--条件自己爱加什么加什么了
      

  6.   

    drop table a,b,ccreate table a(id int, name varchar(8))
    insert a
    select 1 ,'张三' union all
    select 2 ,'李四'create table  b(id int,aid int ,time varchar(4), money int)
    insert b
    select 1 ,1 ,'2008', 1000 union all
    select 2 ,2 ,'2008', 1200 union all
    select 3 ,1 ,'2009', 1500 union all
    select 4 ,2 ,'2009', 1600create table c(id int,aid int,carNumber varchar(20))
    insert c
    select 1 ,1 ,'冀C9987' union all
    select 2 ,2 ,'京P4489' select name,carNumber,time,money
    from a,b,c
    where a.id=b.aid and a.id=c.aid
    and not exists 
    (select 1 from b as tb where aid=b.aid and time>b.time)
    --and name='张三'/*
    name     carNumber            time money       
    -------- -------------------- ---- ----------- 
    张三       冀C9987               2009 1500
    李四       京P4489               2009 1600(所影响的行数为 2 行)
    */
      

  7.   

    请教高手上面出现频率比较多的是这句语句 
    not exists (select 1 from b as tb where id=b.id and time>b.time)
    我查了下 not exists 好像是如果后面这个查询语句不成立的时候的值返回来
    但是“select 1 from b as tb where id=b.id and time>b.time” 这句中的 “time>b.time” 我始终不明白是怎么个意思? 这个是循环比对时间吗?我上面的“B 表 (客户车辆保险续费表)”虽然列的是一个客户有2条续保记录,但是也有可能是多条比如20条, 这样的话“time>b.time” 这个语句还好用吗?
     
      

  8.   

    那句的意思就是“不存在更大的”,也就是找最大的。但如果按你那样20082009的,最大的TIME可能有多条,就会都取出来的
      

  9.   


    create table #A --客户信息表
    (id int, [name] nvarchar(10))
    insert #A
    select 1,'张三' union all
    select 2,'李四'create table #B --客户保险续费表
    (id int,aid int,[time] int, money int)
    insert #B
    select 1 ,1, 2008 ,1000 union all
    select 2 ,2, 2008 ,1200 union all
    select 3 ,1, 2009 ,1500 union all
    select 4 ,2, 2009 ,1600create table #C --客户购车信息
    (id int, aid int, carNumber nvarchar(20))
    insert #c
    select 1 ,1, '冀C9987' union all
    select 2 ,2, '京P4489'declare @name nvarchar(10)
    declare @carNumber nvarchar(20)
    set @name=''  --改这里,传 '' 就全部查出来..传'张三'就查张三的
    set @carNumber=''  --改这里
    select isnull(a.[name],'') as [name],isnull(c.carNumber,'') as carNumber,b.[time],b.money from #B as b 
    Left join #A as a on a.id=b.aid
    Left join #C as c on a.id=c.aid
    where not exists(select 1 from #B as b1 where b1.aid=b.aid and b1.[time]>b.[time]) 
    and (@name='' or  (@name<>'' and a.[name]=@name))
    and (@carNumber='' or  (@carNumber<>'' and c.carNumber=@carNumber))--name       carNumber            time        money
    ------------ -------------------- ----------- -----------
    --张三         冀C9987               2009        1500
    --李四         京P4489               2009        1600
    --
    --(2 row(s) affected)
      

  10.   

    还是不太明白啊,我就是想把所有客户列出来,顺便客户后面把他最后的续保的信息列出来,有的客户有可能续过1次保,有的可能续过20次保,我不管他许多是,总之所有的客户名称列表都列出来,每个客户名称后面跟的是该客户最后一次续保的信息,其实我实际的是要取最后续保到期时间,好提前有个提醒,是不是我这个要求的话用“not exists (select 1 from b as tb where id=b.id and time>b.time)” 这种方式就实现不了了?还有 "select 1"就是“select *” 的意思吗? 还有如果按时间取最大的用 order by time desc 行吗?只是我不知道该放到哪里?小弟技术水平有限,请多多指教。谢谢!
      

  11.   

    哈哈,非常感谢,刚刚把“cd731107” 的代码直接粘进SQLSERVER的查询器里运行好用,就是我要的结果非常感谢各位!现在马上结贴,分数有点少,可能每人分不到多少分,也可能分的不是很均匀,所以实在不好意思啊!再次感谢各位热心回答!结贴