第一种:
select distinct 货物名单,count(货物名单) as 次数 from tb group by 货物名单

解决方案 »

  1.   

    有一个SQL语句实现有点难度```
      

  2.   

    yiyi_wx ,你的语句会把全部西瓜归为一类的,但是我这里要按照是否连续的状况把西瓜分为几类来统计
      

  3.   

    两个结果不一样吗?只是多了一列而已吧
    --建表
    create table test
    (
    时间 varchar(10),
    货物名单 varchar(10)
    )
    --插入数据
    insert into test select '8:00',     '西瓜' 
    insert into test select '8:01',     '西瓜'
    insert into test select '8:02',     '西瓜'
    insert into test select '8:03',     '西瓜'
    insert into test select '8:05',     '西瓜'
    insert into test select '8:55',     '西瓜'
    insert into test select '8:56',     '香蕉'
    insert into test select '8:57',     '香蕉'
    insert into test select '8:57',     '西瓜'
    insert into test select '8:58',     '苹果'
    insert into test select '8:59',     '苹果'
    insert into test select '9:58',     '西瓜'
    insert into test select '9:59',     '西瓜'
    --查询
    select 货物名单,cast(count(1) as varchar) + '次' as 次数 ,
    min(时间) + '  ' + max(时间) as 时间段
    from
    (
    select * ,
    ord = (select isnull(max(时间),0) from test where 时间 < a.时间 and 货物名单<> a.货物名单)
    from test a
    )b
    group by 货物名单,ord/*
    --结果
    货物名单       次数                               时间段                    
    ---------- -------------------------------- ---------------------- 
    西瓜         6次                               8:00  8:55
    香蕉         2次                               8:56  8:57
    西瓜         1次                               8:57  8:57
    苹果         2次                               8:58  8:59
    西瓜         2次                               9:58  9:59(所影响的行数为 5 行)
    */
      

  4.   

    --建立测试环境
    create table tb(id INT IDENTITY,时间 varchar(10),货物名单 varchar(10))
    insert tb(时间,货物名单)
    select '8:00','西瓜' union all
    select '8:01','西瓜' union all
    select '8:02','西瓜' union all
    select '8:03','西瓜' union all
    select '8:05','西瓜' union all
    select '8:55','西瓜' union all
    select '8:56','香蕉' union all
    select '8:57','香蕉' union all
    select '8:57','西瓜' union all
    select '8:58','苹果' union all
    select '8:59','苹果' union all
    select '9:58','西瓜' union all
    select '9:59','西瓜'
    go
    --执行测试语句
    select 货物名单,COUNT(1) AS 次数,min(时间) as 开始时间,max(时间) as 结束时间
    from(
    select a.id,a.时间,a.货物名单,count(1) as ii
    from tb a
    join tb b on b.货物名单=a.货物名单 and b.时间<=a.时间
    group by a.时间,a.id,a.货物名单
    )c
    group by 货物名单, id-c.ii
    go
    --删除测试环境
    drop table tb
    go
    /*--测试结果
    货物名单       次数          开始时间       结束时间
    ---------- ----------- ---------- ----------
    西瓜         6           8:00       8:55
    西瓜         1           8:57       8:57
    西瓜         2           9:58       9:59
    香蕉         2           8:56       8:57
    苹果         2           8:58       8:59(5 row(s) affected)
    */
      

  5.   


    create table #
    (时间     varchar(10),    货物名单 varchar(10))
    insert into #
    select '8:00',     '西瓜' union all 
    select '8:01',     '西瓜' union all 
    select '8:02',     '西瓜'  union all
    select '8:03',     '西瓜'  union all
    select '8:05',     '西瓜'  union all
    select '8:55',     '西瓜'  union all
    select '8:56',     '香蕉'  union all
    select '8:57',     '香蕉'  union all
    select '8:57',     '西瓜'  union all
    select '8:58',     '苹果'  union all
    select '8:59',     '苹果'  union all
    select '9:58',     '西瓜'  union all
    select '9:59',     '西瓜' 
    --第一种
    select 货物名单,count(col)as '次数'
    from
    (
        select 
      *,col=(select count(1) from # where 时间<a.时间 and 货物名单<>a.货物名单) 
        from # a
    )a
    group by col,货物名单/*
    货物名单       次数          
    ---------- ----------- 
    苹果         2
    西瓜         6
    西瓜         1
    西瓜         2
    香蕉         2(所影响的行数为 5 行)
    */
    --第二种select  货物名单,count(col)as '次数',min(时间)+'-'+max(时间)
    from
    (
        select 
       *,col=(select count(1) from # where 时间<a.时间 and 货物名单<>a.货物名单) 
        from # a
    )a
    group by col,货物名单/*
    货物名单       次数                                
    ---------- ----------- --------------------- 
    苹果         2           8:58-8:59
    西瓜         6           8:00-8:55
    西瓜         1           8:57-8:57
    西瓜         2           9:58-9:59
    香蕉         2           8:56-8:57(所影响的行数为 5 行)
    */
      

  6.   


    select  货物名单,count(col)as '次数',min(时间)+'-'+max(时间) as '时间段'
    from
    (
        select 
       *,col=(select count(1) from # where 时间<a.时间 and 货物名单<>a.货物名单) 
        from # a
    )a
    group by col,货物名单
    order by min(时间)+'-'+max(时间)/*
    货物名单       次数                                
    ---------- ----------- --------------------- 
    西瓜         6           8:00-8:55
    香蕉         2           8:56-8:57
    西瓜         1           8:57-8:57
    苹果         2           8:58-8:59
    西瓜         2           9:58-9:59(所影响的行数为 5 行)
    */
      

  7.   

    select  货物名单,count(col)as '次数',min(时间)+'-'+max(时间) as '时间段'
    from
    (
        select 
           *,col=(select count(1) from # where 时间<a.时间 and 货物名单<>a.货物名单) 
        from # a
    )a
    group by col,货物名单
    order by min(时间)+'-'+max(时间)  --鹤兄,这里可以直接: order by 时间段/*
    货物名单       次数                                
    ---------- ----------- --------------------- 
    西瓜         6           8:00-8:55
    香蕉         2           8:56-8:57
    西瓜         1           8:57-8:57
    苹果         2           8:58-8:59
    西瓜         2           9:58-9:59(所影响的行数为 5 行)
    */