yangpanwww 2009-09-15 17:00
浏览 140
已采纳

这个 SQL 语句怎么写啊?

写一SQL 语句!!!!

 

 

合同表:  ht_table

 

qiid(PK)

qysbbh 

 time htzlid
1 330151236521  2009-01-02 001
2 330215545245  2009-01-09 002
3 330215545245  2009-01-02 003
4 330215545245 2009-01-10 002

 

 目标:

 

从 ht_table 中查询出所有企业的合同信息(当 qysbbh 和 htzlid 相等就只查询出 合同时间(time) 最新的合同);

   

 

查询结果为:

 

qiid(PK)

qysbbh 

 time htzlid
1 330151236521  2009-01-02 001
3 330215545245  2009-01-02 003
4 330215545245 2009-01-10 002
  • 写回答

3条回答 默认 最新

  • icefishc 2009-09-15 17:52
    关注

    [quote]
    select qiid,qysbbh,max(time),htzlid from ht_table group by qiid,qysbbh,htzlid

    上面的写错了!!

    [/quote]
    这2个都错了。。。。
    select qiid,qysbbh,time,htzlid
    from ht_table h
    where not exist (
    select 1
    from ht_table
    where h.qysbbh = qysbbh
    and h.htzlid = htzlid
    and time > h.time)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?