有表A(aa,bb)如下:
aa bb
1 a
1 a
1 a
2 b
2 b
2 b
3 c
3 c
3 c
3 d
4 d
4 d
4 e
5 f
5 f
5 f也就是aa,bb均不是唯一.
现需以下结果:
如果aa相同,则计算bb:当bb相同就只计算一次,bb不同就分开计算
比如:
1 a
1 a
1 a
2 b
2 b
2 b
结果应该是:
1 a
2 b
但是,当aa相同,计算bb时,bb不同,则要分开计算
比如:
3 c
3 c
3 c
3 d
结果应该是:
3 c
3 d
又当aa不同时,bb的值相互不影响
比如
3 d
4 d
结果应该是:
3 d
4 d最终结果应该是:
1 a
2 b
3 c
3 d
4 d
4 e
5 f
可能没表述清楚...囧...

解决方案 »

  1.   

    SELECT DISTINCT * FROM TB?
      

  2.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-10 10:58:02
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([aa] int,[bb] varchar(1))
    insert [tb]
    select 1,'a' union all
    select 1,'a' union all
    select 1,'a' union all
    select 2,'b' union all
    select 2,'b' union all
    select 2,'b' union all
    select 3,'c' union all
    select 3,'c' union all
    select 3,'c' union all
    select 3,'d' union all
    select 4,'d' union all
    select 4,'d' union all
    select 4,'e' union all
    select 5,'f' union all
    select 5,'f' union all
    select 5,'f'
    --------------开始查询--------------------------select distinct aa,bb from [tb] 
    ----------------结果----------------------------
    /*
    aa          bb   
    ----------- ---- 
    1           a
    2           b
    3           c
    3           d
    4           d
    4           e
    5           f(所影响的行数为 7 行)
    */
      

  3.   

    Select aa,bb  from 表A group by aa,bb
      

  4.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-10 10:58:02
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([aa] int,[bb] varchar(1))
    insert [tb]
    select 1,'a' union all
    select 1,'a' union all
    select 1,'a' union all
    select 2,'b' union all
    select 2,'b' union all
    select 2,'b' union all
    select 3,'c' union all
    select 3,'c' union all
    select 3,'c' union all
    select 3,'d' union all
    select 4,'d' union all
    select 4,'d' union all
    select 4,'e' union all
    select 5,'f' union all
    select 5,'f' union all
    select 5,'f'
    --------------开始查询--------------------------select  aa,bb from [tb] group by aa,bb
    ----------------结果----------------------------
    /*
    aa          bb   
    ----------- ---- 
    1           a
    2           b
    3           c
    3           d
    4           d
    4           e
    5           f(所影响的行数为 7 行)
    */
      

  5.   

    直接用distinct结果会比需要的少.因为distinct aa之后,aa对应的bb可能有2个
      

  6.   

    重新描述一下例子
    有表A(aa,bb)如下: 
    aa bb 
    1 a 
    1 a 
    1 a
    1 b
    2 b 
    2 b 
    结果应该是: 
    1 a
    1 b
    2 b
      

  7.   

    就用distinct
    不信你自己去测试下
      

  8.   

    你试试呀,正确的
    SELECT DISTINCT AA,BB FROM TB
      

  9.   

    select distinct * from A有问题吗?
      

  10.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-10 11:10:05
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([aa] int,[bb] varchar(1))
    insert [tb]
    select 1,'a' union all
    select 1,'a' union all
    select 1,'a' union all
    select 1,'b' union all
    select 2,'b' union all
    select 2,'b'
    --------------开始查询--------------------------
    select distinct aa,bb from [tb]
    ----------------结果----------------------------
    /*aa          bb   
    ----------- ---- 
    1           a
    1           b
    2           b(所影响的行数为 3 行)*/
      

  11.   

    还有一点.我是根据一组aa的数组来查询的也就是我现在有张表,只保存了aa,比如table1我是
    select distinct table1.aa,表.bb
    from table1 join 表
    on table1.aa= 表.aa
      

  12.   

    我最后得到的结果aa,bb始终没有直接查table1的aa的结果数多.table1有160+条联合查只有140+条
      

  13.   

    select distinct aa,bb 
    from tb 
      

  14.   

    我是结果偏少啊...left jion之后结果还是一样...
      

  15.   

    我查询出来的结果aa就没有重复了...就算bb不同...也只显示一条PS:  SQL2000
      

  16.   

    FULL JOIN ???LEFT JOIN 应该可以了
      

  17.   


    select  table1.aa,k.bb 
    from table1 left join (select distinct * from 表) k 
    on table1.aa= k.aa
      

  18.   

    if object_id('[tb]') is not null 
    drop table [tb]
    go
    create table [tb]([aa] int,[bb] varchar(1))
    insert [tb]
    select 1,'a' union all
    select 1,'a' union all
    select 1,'a' union all
    select 1,'b' union all
    select 2,'b' union all
    select 2,'b'
    go
    if object_id('table1') is not null 
    drop table table1
    go
    create table table1([aa] int)
    insert table1
    select 1 union all select 2 union all select 3
    go
    select  table1.aa,k.bb 
    from table1 left join (select distinct * from tb) k 
    on table1.aa= k.aa
    /*
    aa          bb
    ----------- ----
    1           a
    1           b
    2           b
    3           NULL
    */
    --下面正常的链接
    select  table1.aa,k.bb 
    from table1  join (select distinct * from tb) k 
    on table1.aa= k.aa/*\
    aa          bb
    ----------- ----
    1           a
    1           b
    2           b
    */