select a.水果,count(*) 数量 from a,b where a.代码 = b.水果 group by a.水果
表A: 水果 代码 苹果 1 梨子 2 桔子 3 表B: 姓名 水果 张三 1 李四 1 王五 3 小王 2 小李 1 --------------------- select 水果,count(case when 水果='苹果' then 1 else 0 end)as '苹果', count(case when 水果='梨子' then 1 else 0 end)as '梨子', count(case when 水果='桔子' then 1 else 0 end)as '桔子' from ( select a.水果,b.水果 as 代码 from 表A a left join 表B b on a.代码=b.水果 )a group by 水果
create table a(水果 nvarchar(10), 代码 int ) insert into a select '苹果', 1 union all select '梨子', 2 union all select '桔子', 3 create table b(姓名 nvarchar(10), 水果 int) insert into b select '张三', 1 union all select '李四', 1 union all select '王五', 3 union all select '小王', 2 union all select '小李', 1 select count(case when a.水果='苹果' then 1 else 0 end)as '苹果', count(case when a.水果='梨子' then 1 else 0 end)as '梨子', count(case when a.水果='桔子' then 1 else 0 end)as '桔子' from b left join a on b.水果=a.代码 group by a.水果苹果 梨子 桔子 ----------- ----------- ----------- 1 1 1 1 1 1 3 3 3(3 row(s) affected)
select 水果=t2.水果,t1.数量 from (select 水果,数量=count(姓名) from b group by 水果) t1 inner join a t2 on t2.代码=t1.水果-------------------- 执行结果水果 数量 苹果 3 梨子 1 桔子 1
对上次写的进行修改,可以实现要求:select '苹果'=max(case when tmp.水果='苹果' then tmp.数量 else null end), '梨子'=max(case when tmp.水果='梨子' then tmp.数量 else null end), '桔子'=max(case when tmp.水果='桔子' then tmp.数量 else null end) from ( select 水果=t2.水果, t1.数量 from (select 水果,数量=count(姓名) from b group by 水果) t1 inner join a t2 on t2.代码=t1.水果 ) as tmp-------------------------------- 执行结果 苹果 梨子 桔子 3 1 1
select max(d.苹果) 苹果, max(d.梨子) 梨子, max(d.桔子) 桔子 from (select '苹果'= case when c.水果 = '苹果' then c.数量 else NULL end, '梨子'= case when c.水果 = '梨子' then c.数量 else NULL end, '桔子'= case when c.水果 = '桔子' then c.数量 else NULL end from (select a.水果,count(*) 数量 from a,b where a.代码 = b.水果 group by a.水果) as c) as d
select [苹果] as 苹果, [梨子] as 梨子,[桔子] as 桔子 from (select a.水果 from a inner join b on a.代码=b. 水果) C pivot (COUNT(水果) for 水果 in ([苹果],[梨子],[桔子]) ) AS pvt
create table a(水果 nvarchar(10), 代码 int ) insert into a select '苹果', 1 union all select '梨子', 2 union all select '桔子', 3 create table b(姓名 nvarchar(10), 水果 int) insert into b select '张三', 1 union all select '李四', 1 union all select '王五', 3 union all select '小王', 2 union all select '小李', 1 select a.水果,b.dd from a,(select 水果,count(水果) dd from b group by 水果) b where a.代码=b.水果
select a.水果,count(*) 数量 from a,b where a.代码 = b.水果 group by a.水果 还是这样比较好
select a.水果,count(*) 数量 from a,b where a.代码=B.水果 group by a.水果
行列转换需用动态查询实现create table a (水果 char(10),代码 int) go create table b (姓名 char(10),水果 int) go insert into a values('苹果' , 1 ) insert into a values('梨子', 2 ) insert into a values('桔子', 3 ) insert into b values('张三', 1 ) insert into b values('李四', 1 ) insert into b values('王五', 3 ) insert into b values('小王', 2) insert into b values('小李', 1) go declare @str1 varchar(8000) set @str1='' select @str1=case when @str1='' then 'select '+rtrim(水果) +'=(select count(*) from b where 水果=' +convert(varchar,a.代码)+')' else @str1+', '+rtrim(水果) +'=(select count(*) from b where 水果=' +convert(varchar,a.代码)+')'
end from a exec( @str1)
create table a (水果 char(10),代码 int) go create table b (姓名 char(10),水果 int) go insert into a values('苹果' , 1 ) insert into a values('梨子', 2 ) insert into a values('桔子', 3 ) insert into b values('张三', 1 ) insert into b values('李四', 1 ) insert into b values('王五', 3 ) insert into b values('小王', 2) insert into b values('小李', 1) goselect a.水果,count(1)cnt from a,b where a.代码=b.水果 group by a.水果 declare @sql varchar(1000) select @sql=isnull(@sql+',','')+'['+水果+']=max(case 水果 when '''+水果 +''' then cnt else 0 end)' from a set @sql='select '+@sql+' from (select a.水果,count(1)cnt from a,b where a.代码=b.水果 group by a.水果)b' exec(@sql)
表A:
水果 代码
苹果 1
梨子 2
桔子 3 表B:
姓名 水果
张三 1
李四 1
王五 3
小王 2
小李 1
---------------------
select 水果,count(case when 水果='苹果' then 1 else 0 end)as '苹果',
count(case when 水果='梨子' then 1 else 0 end)as '梨子',
count(case when 水果='桔子' then 1 else 0 end)as '桔子'
from
(
select a.水果,b.水果 as 代码
from 表A a left join 表B b
on a.代码=b.水果
)a
group by 水果
insert into a
select '苹果', 1
union all select '梨子', 2
union all select '桔子', 3
create table b(姓名 nvarchar(10), 水果 int)
insert into b
select '张三', 1
union all select '李四', 1
union all select '王五', 3
union all select '小王', 2
union all select '小李', 1
select count(case when a.水果='苹果' then 1 else 0 end)as '苹果',
count(case when a.水果='梨子' then 1 else 0 end)as '梨子',
count(case when a.水果='桔子' then 1 else 0 end)as '桔子'
from b
left join a on b.水果=a.代码 group by a.水果苹果 梨子 桔子
----------- ----------- -----------
1 1 1
1 1 1
3 3 3(3 row(s) affected)
from (select 水果,数量=count(姓名) from b group by 水果) t1
inner join a t2 on t2.代码=t1.水果--------------------
执行结果水果 数量
苹果 3
梨子 1
桔子 1
'苹果'=max(case when tmp.水果='苹果' then tmp.数量 else null end),
'梨子'=max(case when tmp.水果='梨子' then tmp.数量 else null end),
'桔子'=max(case when tmp.水果='桔子' then tmp.数量 else null end)
from (
select 水果=t2.水果, t1.数量
from (select 水果,数量=count(姓名) from b group by 水果) t1
inner join a t2 on t2.代码=t1.水果
) as tmp--------------------------------
执行结果
苹果 梨子 桔子
3 1 1
from
(select
'苹果'= case when c.水果 = '苹果' then c.数量 else NULL end,
'梨子'= case when c.水果 = '梨子' then c.数量 else NULL end,
'桔子'= case when c.水果 = '桔子' then c.数量 else NULL end
from (select a.水果,count(*) 数量 from a,b where a.代码 = b.水果 group by a.水果) as c) as d
pivot
(COUNT(水果) for 水果 in ([苹果],[梨子],[桔子]) ) AS pvt
insert into a
select '苹果', 1
union all select '梨子', 2
union all select '桔子', 3
create table b(姓名 nvarchar(10), 水果 int)
insert into b
select '张三', 1
union all select '李四', 1
union all select '王五', 3
union all select '小王', 2
union all select '小李', 1 select a.水果,b.dd from a,(select 水果,count(水果) dd from b group by 水果) b where a.代码=b.水果
还是这样比较好
from a,b where a.代码=B.水果
group by a.水果
go
create table b (姓名 char(10),水果 int)
go
insert into a values('苹果' , 1 )
insert into a values('梨子', 2 )
insert into a values('桔子', 3 )
insert into b values('张三', 1 )
insert into b values('李四', 1 )
insert into b values('王五', 3 )
insert into b values('小王', 2)
insert into b values('小李', 1)
go
declare @str1 varchar(8000)
set @str1=''
select @str1=case when @str1='' then 'select '+rtrim(水果)
+'=(select count(*) from b where 水果='
+convert(varchar,a.代码)+')'
else @str1+', '+rtrim(水果)
+'=(select count(*) from b where 水果='
+convert(varchar,a.代码)+')'
end
from a
exec( @str1)
create table a (水果 char(10),代码 int)
go
create table b (姓名 char(10),水果 int)
go
insert into a values('苹果' , 1 )
insert into a values('梨子', 2 )
insert into a values('桔子', 3 )
insert into b values('张三', 1 )
insert into b values('李四', 1 )
insert into b values('王五', 3 )
insert into b values('小王', 2)
insert into b values('小李', 1)
goselect a.水果,count(1)cnt from a,b where a.代码=b.水果 group by a.水果
declare @sql varchar(1000)
select @sql=isnull(@sql+',','')+'['+水果+']=max(case 水果 when '''+水果 +''' then cnt else 0 end)' from a
set @sql='select '+@sql+' from (select a.水果,count(1)cnt from a,b where a.代码=b.水果 group by a.水果)b'
exec(@sql)