一个表:storeKuang
有下面字段:id,sendId,basketNum,productId,lookStates数据示例如下:
【id】,【sendid】,【basketNum】,【productId】,【lookStates】1,001,0001,345,1
2,001,0001,346,1
3,001,0001,347,0
4,001,0002,346,0
5,001,0001,348,1
6,001,0003,346,1
7,001,0004,346,1
8,001,0003,347,0
9,001,0005,348,1
我现在要求下面条件的basketNum的记录数
第一:就是不重复的basketNum的数目(答案:5)
第二:为了大家的理解,我定义:basketNum相等的一系列记录中当所有的lookStates=1,我们定义这个basketNum是一个完成取货的状态,例如0004,0005;basketNum相等的一系列记录中当有部分或者全部lookStates=0,我们定义这个basketNum是一个正在取货的状态,例如0001,0002,0003。现在我要统计完成取货的basketNum的数目和正在取货的basketNum的数目。(答案是:2和3)那个5我是这么查出来的,感觉方法有点笨,不知道是否有其他的好办法?
"select count(*) as allBasket from (select  basketNum from storeKuang where sendid='0001' group by basketNum) sk "2,3这两个数值要如何查询出来

解决方案 »

  1.   

    第一select distinct basketNum from storeKuang
      

  2.   

    create table storeKuang(id int,sendId varchar(10),basketNum varchar(10),productId int,lookStates int)
    insert into storeKuang select
    1,'001','0001',345,1 union all select
    2,'001','0001',346,1 union all select
    3,'001','0001',347,0 union all select
    4,'001','0002',346,0 union all select
    5,'001','0001',348,1 union all select
    6,'001','0003',346,1 union all select
    7,'001','0004',346,1 union all select
    8,'001','0003',347,0 union all select
    9,'001','0005',348,1
    go
    select count(*) from (
    select distinct basketnum from storeKuang
    )t
    gogo
    drop table storeKuang
      

  3.   

    现在我要统计完成取货的basketNum的数目和正在取货的basketNum的数目
    这个才是我要的答案!
      

  4.   

    第二:
    查询正在取货select distinct  basketNum  from storeKuang where lookStates=0查询取完货select distinct basketNum from storeKuang
    except
    select distinct  basketNum  from storeKuang where lookStates=0
      

  5.   

    --> 测试数据: #storeKuang
    if object_id('tempdb.dbo.#storeKuang') is not null drop table #storeKuang
    go
    create table #storeKuang (id int,sendId varchar(3),basketNum varchar(4),productId int,lookStates int)
    insert into #storeKuang
    select 1,'001','0001',345,1 union all
    select 2,'001','0001',346,1 union all
    select 3,'001','0001',347,0 union all
    select 4,'001','0002',346,0 union all
    select 5,'001','0001',348,1 union all
    select 6,'001','0003',346,1 union all
    select 7,'001','0004',346,1 union all
    select 8,'001','0003',347,0 union all
    select 9,'001','0005',348,1-- 完成
    select count(distinct basketNum) 
    from #storeKuang t
    where not exists(select 1 from #storeKuang where basketNum=t.basketNum and lookStates=0)-- 正在取
    select count(distinct basketNum) 
    from #storeKuang 
    where lookStates=0-----------
    2(1 行受影响)
    -----------
    3(1 行受影响)
      

  6.   

    create table storeKuang(id int,sendId varchar(10),basketNum varchar(10),productId int,lookStates int)
    insert into storeKuang select
    1,'001','0001',345,1 union all select
    2,'001','0001',346,1 union all select
    3,'001','0001',347,0 union all select
    4,'001','0002',346,0 union all select
    5,'001','0001',348,1 union all select
    6,'001','0003',346,1 union all select
    7,'001','0004',346,1 union all select
    8,'001','0003',347,0 union all select
    9,'001','0005',348,1
    go
    select count(*)as 不重复 from (
    select distinct basketnum from storeKuang
    )t
    go
    select count(*)as 已完成 from (
    select count(*)a from storeKuang group by basketNum having avg(lookstates)=1
    )t
    go
    select count(*)as 未完成 from (
    select count(*)a from storeKuang group by basketNum having avg(lookstates)<1
    )t
    go
    drop table storeKuang
    /*
    不重复
    -----------
    5(1 行受影响)已完成
    -----------
    2(1 行受影响)未完成
    -----------
    3(1 行受影响)*/
      

  7.   

    declare @a table(id INT,sendId CHAR(3),basketNum CHAR(4),productId int,lookStates INT)
    insert @a select 1,'001','0001',345,1
    union all select 2,'001','0001',346,1
    union all select 3,'001','0001',347,0
    union all select 4,'001','0002',346,0
    union all select 5,'001','0001',348,1
    union all select 6,'001','0003',346,1
    union all select 7,'001','0004',346,1
    union all select 8,'001','0003',347,0
    union all select 9,'001','0005',348,1SELECT sendId,COUNT(DISTINCT basketNum) basketNum FROM @a GROUP BY sendId--result
    /*sendId basketNum   
    ------ ----------- 
    001    5(所影响的行数为 1 行)
    */
    SELECT sendId,yy,COUNT(distinct basketNum) basketNum FROM 
    (
    SELECT *,(CASE WHEN EXISTS(SELECT 1 FROM @a WHERE sendId=a.sendId AND basketNum=a.basketNum and lookStates=0) THEN 0 ELSE 1 end) yy 
    FROM @a a)aa
    GROUP BY sendId,yy 
    --result
    /*sendId yy          basketNum   
    ------ ----------- ----------- 
    001    0           3
    001    1           2(所影响的行数为 2 行)
    */