试下这个:
select a.公司,a.名称,值=case b.值 when 0 then a.值 else isnull(a.值,0)/b.值 from (select * from 表 where 名称 in ('产值','销售')) a,(select * from 表 where 名称 in ('件数')) b where a.公司= b.公司
select a.公司,a.名称,值=case b.值 when 0 then a.值 else isnull(a.值,0)/b.值 from (select * from 表 where 名称 in ('产值','销售')) a,(select * from 表 where 名称 in ('件数')) b where a.公司= b.公司
解决方案 »
- 简单需求,火速来救我
- 数据库如何执行嵌套 的LIKE 查询啊
- collection国际标准是什么?
- 两表之间的查询,急! 在线等解决的办法!
- SQL SERVER问题请教会的老手们!程序出错 麻烦纠错指点!
- sql server 2008启动不了服务,麻烦帮忙下!谢谢
- 关于SQL2000的一个糊涂的问题???
- bcp批量导入文本数据出错,请各位指点
- []__[]__辭了,散分__[]__[]
- SQL语句 where in(张三,张三,李四,张三,李四,张三)。怎么使记录集的顺序和()里的条件顺序一致。包括重复的要一样
- 取不重复值的问题及表关联
- SOS!如何由sql server2000建立sybase11的链接服务器
select a.公司,a.名称,值=case b.值 when 0 then a.值 else isnull(a.值,0)/b.值 end from (select * from 表 where 名称 in ('产值','销售')) a,(select * from 表 where 名称 in ('件数')) b where a.公司= b.公司
from tableName a, tableName b
where a.公司=b.公司 and b.名称='件数' and a.名称<>'件数'
create table mm(公司 varchar(10),名称 varchar(20),值 int)
insert mm select 'A','产值',1200
union all select 'A','销售',800
union all select 'A','件数',10
select 公司,名称, 值/(select 值 from mm where 名称='件数') as 值
from mm where 名称='产值'
union
select 公司,名称,值/(select 值 from mm where 名称='件数') as 值
from mm where 名称='销售'
不知道有没有更好的方法
insert mm select 'A','产值',1200
union all select 'A','销售',800
union all select 'A','件数',10
select 公司,名称, 值/(select 值 from mm where 名称='件数') as 值
from mm where 名称='产值'
union
select 公司,名称,值/(select 值 from mm where 名称='件数') as 值
from mm where 名称='销售'这个当公司不只一个公司时呢?
,值/isnull((select sum(值) from 表 where 名称 = '件数' and 公司 = a.公司),1)
from 表 a
where 名称 in ('产值','销售')
order by 公司,名称
select a.公司,a.名称,(case b.值 when 0 then 0 else a.值/b.值 end)as 值 from
(
select 公司,名称,值 from #mm where 名称<>'件数'
)a
left join
(
select 公司,名称,isnull(值 ,0) as 值 from #mm where 名称='件数'
)b
on a.公司=b.公司