题目:门店  上传时间a    8:01:32
a    8:01:36
a    8:01:42
a    9:10:12
a    9:10:16
a    9:10:32结果门店 第一次上传  第二次上传
a    8:01:32    9:10:12
怎么写sql
select * ,id=identity(int,1,1) into #t from tb t
 where not exists(select * from tb where datediff(hour,t.[上传时间],[上传时间])=0 and [门店]=t.[门店] and [上传时间]<t.[上传时间])这个华夏小卒是如何构思的呢?
了不起的华夏小卒

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([门店] varchar(1),[上传时间] datetime)
    insert [tb]
    select 'a','8:01:32' union all
    select 'a','8:01:36' union all
    select 'a','8:01:42' union all
    select 'a','9:10:12' union all
    select 'a','9:10:16' union all
    select 'a','9:10:32'SELECT * FROM tbselect * ,id=identity(int,1,1) into #t from tb t
     WHERE NOT   exists(select * from tb where datediff(hour,t.[上传时间],[上传时间])=0 
    and [门店]=t.[门店] and [上传时间]<t.[上传时间]) 
    SELECT * FROM #t
     drop table #t
      

  2.   

    select * ,id=identity(int,1,1) into #t from tb t
     WHERE NOT   exists(select * from tb where datediff(hour,t.[上传时间],[上传时间])=0 
    and [门店]=t.[门店] and [上传时间]<t.[上传时间])这个语句的作用是输出同一小时(datediff(hour,t.[上传时间],[上传时间])=0 )、
    同一门店([门店]=t.[门店])的记录,如果不存在( NOT   exists)比它上传时间小的记录([上传时间]<t.[上传时间]),则输出到临时表(into #t ),并在临时表中增加一列名叫ID、从1开始的自增列(id=identity(int,1,1))还有什么不明白的