1  表A
   id                 name                销售额
    1                   1                  3000
    2                   2                   5000
    3                   3                   6000
    .
    .
给出销售额高于平均销售额的人员名单。。一句SQL完成。
2  表A
   id        name 
    1           1
    2            2
   表B
   id        num     add
   1          1       广东
   1          2         上海
   1          3       北京
   2          1       上海
一个人最多有三个地址:
给出这样的结果:
   id     name    addr1      add2     addr3
   1        1      广东        上海    北京
    2       2      上海         null   null
用一句SQL ,而且用外连接。。

解决方案 »

  1.   

    --1、select *
    from 表A
    where 销售额>(select avg(销售额) from 表A group by ID)
      

  2.   

    --2、select A.*,t1.[add] as addr1,t2.[add] as addr2,t3.[add] as addr3
    from A
        left join (select * from B where num=1) t1 on A.id=t1.id
        left join (select * from B where num=2) t2 on A.id=t2.id
        left join (select * from B where num=3) t3 on A.id=t3.id
      

  3.   

    1
    select id,name,销售额 from 表A where 销售额>(select ave(销售额) from 表A)
      

  4.   

    1、 select name from A  where 销售额>(select avg(销售额) from A)
      

  5.   

    我觉得wangtiecheng写的对,要用分组吧?
      

  6.   

    不知不为过,你好:
    我有个疑问:第2题中如果很多条数据,那不是要很多left join (select * from B where num=3) t3 on A.id=t3.id之类的语句?
    如果没有限制要用外连接,还可以用什么语句啊
      

  7.   

    用left join 是最简单的方法。还可以用存储过程,使用游标,但就不是一个语句了。
      

  8.   

    1  select * form 表A where 
      销售额>avg(销售额)  as 平均销售额 group by id
    2 left  outer join语句
      

  9.   

    --第一题:
    create table tb
    (
      id   int identity(1,1),
      name varchar(10),
      销售额 int
    )
    insert tb
    select '1',3000 union all
    select '2',5000 union all
    select '3',6000select * from tb where 销售额>(select avg(销售额) from tb)
    --result:
    id  name 销售额
    ---------------
    2 2 5000
    3 3 6000
    --第二题:
    create table 表A
    (
      id   int,
      name varchar(10)
    )
    insert 表A
    select 1,'1' union all
    select 2,'2'create table 表B
    (
      id   int,
      num  int,
      addr nvarchar(10)
    )
    insert 表B
    select 1,1,'广东' union all
    select 1,2,'上海' union all
    select 1,3,'北京' union all
    select 2,1,'上海'select a.*,b1.addr1,b2.addr2,b3.addr3 
    from 表A a
    left join (select id,addr as addr1 from 表B where num=1)b1 on b1.id=a.id
    left join (select id,addr as addr2 from 表B where num=2)b2 on b2.id=a.id
    left join (select id,addr as addr3 from 表B where num=3)b3 on b3.id=a.id   --result:
     id     name    addr1      add2     addr3
    1 1 广东 上海 北京
    2 2 上海 NULL NULL
      

  10.   

    1.select name from A group by name having xse>avg(xse) 
    2.select A.id,A.name,
    case when case num=1 then add else NULL
         when case num=2 then add else NULL
         when case num=3 then add else NULL
     from A left join B on A.id=B.id
    group by id
      

  11.   

    比如说你是.net程序员 再要求你会这样的还说的过去
    如果是招数据库专职的就........
      

  12.   

    不用编函数.一楼的代码是最灵活的统计方法.如果楼主非要只统计少数几项费用的话,可以这样试试:
    ----创建测试数据
    if object_id('tb') is not null
          drop table tb
    create table tb (ID int,Date Varchar(20),MoneyName Varchar(10),[Money] Int)
    INSERT INTO Tb SELECT 220701,'2006-07-01','生活费',300
    UNION ALL SELECT 220702,'2006-07-5','水费',50
    UNION ALL SELECT 220703,'2006-07-01','电费',43
    UNION ALL SELECT 220704,'2006-07-01','杂费',80
    ----方法一:直接统计
    select id,date,
    [生活费] = sum(case MoneyName when '生活费' then [Money] else 0 end),
    [水费] = sum(case MoneyName when '水费' then [Money] else 0 end),
    [电费] = sum(case MoneyName when '电费' then [Money] else 0 end),
    [杂费] = sum(case MoneyName when '杂费' then [Money] else 0 end)
    from tb group by id,date order by id
    ----方法二:动态统计
    Declare @sql varchar(8000)
    SET @sql='SELECT ID,Date'
    SELECT @Sql=@Sql+','+MoneyName+'=SUM(Case MoneyName WHEN '''+MoneyName+''' Then Money Else 0 END)' FROM Tb
    print @sql
    EXEC ( @sql + ' From Tb Where MoneyName in(''生活费'',''水费'',''电费'',''杂费'') Group By ID,Date Order by ID')
    ----清除测试环境
    drop table tb
      

  13.   

    第二题除了用好几个左联接 也可以用case语句
    2.select A.id,A.name,
    case b.num when 1 then b.add else NULL end as addr1,
    case b.num when 2 then b.add else NULL end as addr2,
    case b.num when 3 then b.add else NULL end as addr3
     from A left join B on A.id=B.id
    group by id
      

  14.   

    jiazheng(飞天)
    您的第二题的做法有错误.
      

  15.   

    CREATE TABLE CSDN_TEST
    (
    id int IDENTITY (1, 1) primary key,
    name varchar(100) not null,
    sale int not null
    )insert CSDN_TEST (name, sale) values (1, 3000)
    insert CSDN_TEST (name, sale) values (2, 5000)
    insert CSDN_TEST (name, sale) values (3, 6000)select name from CSDN_TEST where sale > (select avg(sale) from CSDN_TEST)
      

  16.   

    select test1.id, test1.name, test2.[add] as add1, test3.[add] as add2, test4.[add] as add3 from CSDN_TEST1 as test1 
    left outer join CSDN_TEST2 as test2 on test1.id=test2.id and test2.num=1
    left outer join CSDN_TEST2 as test3 on test1.id=test3.id and test3.num=2
    left outer join CSDN_TEST2 as test4 on test1.id=test4.id and test4.num=3
      

  17.   

    create table a (id int,name varchar(10),销售额 int)
    insert into a select 1,'1',3000
    union all select 2,'2',5000
    union all select 3,'3',6000--方法一:
    select * from a 
    where 销售额>(select avg(销售额) from a)--方法二:
    select a.* from a ,(select avg(销售额) as 销售额  from a ) as b
    where  a.销售额>b.销售额drop table aid          name       销售额         
    ----------- ---------- ----------- 
    2           2          5000
    3           3          6000(所影响的行数为 2 行)
      

  18.   


    create table a (id int ,name varchar(10))
    insert into a select 1,'1'
    union all select 1,'2'create table b(id int,num int ,[add] varchar(10))
    insert into b select 1,1,'广东'
    union all select 1,2,'上海'
    union all select 1,3,'北京'
    union all select 2,1,'上海'select * from a left join b
    on a.id=b.idselect * from a,b
    where a.id=b.iddeclare @sql varchar(1000)
    set @sql='select b.id,a.name'
    select @sql=@sql+','+' max( case when num ='''+rtrim(num)+''' then [add] else null end ) as  add'+ rtrim(num) 
    from  (select distinct num from  b) as ttset @sql=@sql+' from a  right join b on a.id=b.id group by b.id,a.name'
    exec(@sql)id          name       add1       add2       add3       
    ----------- ---------- ---------- ---------- ---------- 
    2           NULL       上海         NULL       NULL
    1           1          广东         上海         北京
    1           2          广东         上海         北京drop table a,b
      

  19.   

    case 语句都有问题.列 'B.addr' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'B.num' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'B.addr' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'B.num' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'B.addr' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'B.num' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
      

  20.   

    select A.id,A.name,
    case b.num when 1 then b.addr else NULL end as addr1,
    case b.num when 2 then b.addr else NULL end as addr2,
    case b.num when 3 then b.addr else NULL end as addr3
     from A left join B on A.id=B.id
    group by a.id,a.name
      

  21.   

    1. select * from  表A where [销售额]>(select avg([销售额] from 表A)
    2.
      

  22.   

    为什么要group by
    不懂。。