一个商品销售的统计
子类编号 所属大类    
1        1
2        1
3        2
4        2
-----具体销售信息-----------
销售编号   子类编号   属于那次销售
1             2            1
2             1            1
3             2            2
4             3            3
5             3            4
6             4            4      
7             1            4     
-------------------------------
我想统计 按 属于那次销售分组 属于大类的 数据次数(按分组不重复计算)
如想:统计属于1大类的数据次数
销售编号   子类编号   属于那次销售
1             2            1
2             1            15             3            4
6             4            4      
7             1            4  
--------按分组不重复计算 统计次数为2 
第一次销售中子类编号都属于1大类 但只计算一次
高手帮忙!

解决方案 »

  1.   

    设计到BOM的问题,去精华帖里面找找相关内容.
      

  2.   

    declare @category table (class int, category int) -- 一个商品销售的统计
    insert into @category 
    select 1, 1 union
    select 2, 1 union
    select 3, 2 union
    select 4, 2declare @sales table (id int, class int, times int) -----具体销售信息-----------
    insert into @sales
    select 1, 2, 1 union
    select 2, 1, 1 union
    select 3, 2, 2 union
    select 4, 3, 3 union
    select 5, 3, 4 union
    select 6, 4, 4 union
    select 7, 1, 4
    -------------------------------
    select *
    from @sales a
    where exists (select 1
        from @category b 
        where a.class=b.class
        and category = 1
        )
    -- id class times
    -- 1 2 1
    -- 2 1 1
    -- 3 2 2
    -- 7 1 4
      

  3.   

    想得到统计次数 
    tim_spac 已经给出了具体数据 修改了一下得到答案
    select count( distinct(times)) from (
    select  *
    from @sales a
    where exists (select 1
        from @category b 
        where a.class=b.class
        and category = 1
        )
    )t