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 ,而且用外连接。。
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 ,而且用外连接。。
from 表A
where 销售额>(select avg(销售额) from 表A group by ID)
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
select id,name,销售额 from 表A where 销售额>(select ave(销售额) from 表A)
我有个疑问:第2题中如果很多条数据,那不是要很多left join (select * from B where num=3) t3 on A.id=t3.id之类的语句?
如果没有限制要用外连接,还可以用什么语句啊
?
销售额>avg(销售额) as 平均销售额 group by id
2 left outer join语句
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
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
如果是招数据库专职的就........
----创建测试数据
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
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
您的第二题的做法有错误.
(
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)
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
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 行)
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
服务器: 消息 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 子句中。
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
2.
不懂。。