本帖最后由 qq_19651241 于 2014-08-30 11:37:30 编辑

解决方案 »

  1.   

    select  客户,商品,sum(金额)
    from test 
    where  客户 in 
    (select  客户 
    from test 
    where  DATE >=  '2011-06-30'
    group by 客户
    having (count (distinct 商品)between 2 and 6)
    )
    group by 客户,商品试一下呢
      

  2.   


    select * from
    (
    select [客户名称],COUNT(1) as [商品种类],SUM([金额]) as [金额] from
    (
    select [客户名称],[商品],SUM([金额]) as [金额] from TEST group by [客户名称],[商品]
    ) a group by [客户名称]
    )b having [商品种类]>1 and [商品种类]<7
      

  3.   

    select 客户,商品,sum(金额)
    from test 
    where  客户 in 
    (select  客户  
    from test 
    where  [DATE] >=  '2011-06-30'
    group by 客户
    having (count (distinct 商品)between 2 and 6))
    and [DATE] >=  '2011-06-30'
    group by 客户,商品
    order by 客户,商品
    1、要删除in里面的select列表里的count计算表达式
      

  4.   

    2、最外面的查询还是要加一个时间限制  and [DATE] >=  '2011-06-30'
      

  5.   


    select  a.客户,a.商品,sum(a.金额) as 金额
    from test  a join
    (
    select  客户 ,count (distinct 商品) as 商品类别 
    from test 
    where  DATE >=  '2011-06-30'
    group by 客户
    having count (distinct 商品) between 2 and 6
    ) b on a.客户=b.客户
    group by a.客户,a.商品
      

  6.   

    可不可以讲解一下,为什么?是不是因为嵌套解开的顺序是从内而外的?所以,括号里的条件不作用于外层?但是这样对查询结果有什么影响么?为什么用IN,我用EXISTS有什么区别么?
      

  7.   

    select 客户,商品,sum(金额)
    from test 
    where  客户 in 
    (select  客户  
    from test 
    where  [DATE] >=  '2011-06-30'
    group by 客户
    having (count (distinct 商品)between 2 and 6))
    and [DATE] >=  '2011-06-30'
    group by 客户,商品
    order by 客户,商品
      

  8.   

    1,关于 IN EXISTS 你可以看一下联机帮助 ,  简单点你的where条件IN前是一列,后面两列肯定有问题吧
    2,SQL执行的逻辑你可以看下: microsoft sql server 2005 技术内幕 这本书  里面有一个章节:查询的逻辑执行过程