现在表的结构如下:
khh        bz
0001      人民币
0002      人民币
0003      人民币
0003       港币
0004       美金
0004       港币
0005      人民币
0005       美金
0006      人民币
0006       美金
0006       港币
0007       美金
0008       港币
请教大家如何用sql语句查询只有人民币的客户、只有美金的客户、只有港币的客户、有美金港币的客户、有人民币美金港币的客户,谢谢

解决方案 »

  1.   

    create table tb(khh nvarchar(10),bz nvarchar(10))
    insert into tb select '0001','人民币'
    insert into tb select '0002','人民币'
    insert into tb select '0003','人民币'
    insert into tb select '0003','港币'
    insert into tb select '0004','美金'
    insert into tb select '0004','港币'
    insert into tb select '0005','人民币'
    insert into tb select '0005','美金'
    insert into tb select '0006','人民币'
    insert into tb select '0006','美金'
    insert into tb select '0006','港币'
    insert into tb select '0007','美金'
    insert into tb select '0008','港币'
    go
    --只有人民币
    select khh from tb a where bz='人民币' and exists(select 1 from tb where khh=a.khh group by khh having count(*)=1)
    /*
    khh
    ----------
    0001
    0002(2 行受影响)
    */
    --只有美金
    select khh from tb a where bz='美金' and exists(select 1 from tb where khh=a.khh group by khh having count(*)=1)
    /*
    khh
    ----------
    0007(1 行受影响)
    */
    --只有港币
    select khh from tb a where bz='港币' and exists(select 1 from tb where khh=a.khh group by khh having count(*)=1)
    /*
    khh
    ----------
    0008(1 行受影响)*/
    --有美金港币的客户
    select distinct khh from tb a where 
    exists(select 1 from tb where khh=a.khh and bz='美金')
    and exists(select 1 from tb where khh=a.khh and bz='港币')
    /*
    khh
    ----------
    0004
    0006(2 行受影响)
    */
    --有人民币美金港币的客户
    select distinct khh from tb a where 
    exists(select 1 from tb where khh=a.khh and bz='美金')
    and exists(select 1 from tb where khh=a.khh and bz='港币')
    and exists(select 1 from tb where khh=a.khh and bz='人民币')
    /*
    khh
    ----------
    0006(1 行受影响)
    */
    go
    drop table tb
      

  2.   

    只有人民币的客户
    select khh  from RMB where bz="人民币" and khh in (SELECT khh FROM RMB group by khh having count(*)=1)只有美金的客户
    select khh  from RMB where bz="美金" and khh in (SELECT khh FROM RMB group by khh having count(*)=1)只有港币的客户
    select khh  from RMB where bz="港币" and khh in (SELECT khh FROM RMB group by khh having count(*)=1)有美金港币的客户
    select khh from RMB where bz="美金" and khh in (select khh  from RMB where bz="港币" and khh in (SELECT khh FROM RMB group by khh having count(*)=2))有人民币美金港币的客户
    select khh from RMB where bz="人民币" and khh in (select khh from RMB where bz="美金" and khh in (select khh  from RMB where bz="港币" and khh in (SELECT khh FROM RMB group by khh having count(*)=3)))如果表中一定只有三种币种,那么最后的
    and khh in (SELECT khh FROM RMB group by khh having count(*)=3)))
    可以不要了只有40分,不够下注呀
      

  3.   

    create table tb(khh nvarchar(10),bz nvarchar(10))
    insert into tb select '0001','人民币'
    insert into tb select '0002','人民币'
    insert into tb select '0003','人民币'
    insert into tb select '0003','港币'
    insert into tb select '0004','美金'
    insert into tb select '0004','港币'
    insert into tb select '0005','人民币'
    insert into tb select '0005','美金'
    insert into tb select '0006','人民币'
    insert into tb select '0006','美金'
    insert into tb select '0006','港币'
    insert into tb select '0007','美金'
    insert into tb select '0008','港币'
    go--只有人民币的客户
    select distinct khh from tb where khh not in (select distinct khh from tb where bz <> '人民币')
    /*
    khh        
    ---------- 
    0001
    0002(所影响的行数为 2 行)
    */--只有美金的客户
    select distinct khh from tb where khh not in (select distinct khh from tb where bz <> '美金')
    /*
    khh        
    ---------- 
    0007(所影响的行数为 1 行)
    */--只有港币的客户
    select distinct khh from tb where khh not in (select distinct khh from tb where bz <> '港币')
    /*
    khh        
    ---------- 
    0008(所影响的行数为 1 行)
    */--有美金港币的客户,应该是只有也必须有美金港币的客户吧?
    select khh from tb where khh in
    (
    select khh from
    (
    select distinct khh from tb where bz = '美金' 
    union all
    select distinct khh from tb where bz = '港币' 
    ) t group by khh having count(1) = 2

    group by khh having count(1) = 2
    /*
    khh        
    ---------- 
    0004(所影响的行数为 1 行)
    */--有人民币美金港币的客户,应该是只有也必须人民币美金港币的客户吧?
    select khh from tb where khh in
    (
    select khh from
    (
    select distinct khh from tb where bz = '美金' 
    union all
    select distinct khh from tb where bz = '港币' 
    union all
    select distinct khh from tb where bz = '人民币' 
    ) t group by khh having count(1) = 3

    group by khh having count(1) = 3
    /*
    khh        
    ---------- 
    0006(所影响的行数为 1 行)
    */drop table tb
      

  4.   

    1楼的exist用的要比2楼的in的效率要高吧
      

  5.   

    http://blog.csdn.net/jianghao616/archive/2010/12/13/6074069.aspx