读书人

一个简单的sql语句解决方法

发布时间: 2012-03-31 13:13:26 作者: rapoo

一个简单的sql语句

declare @c int
select @c=count(1) from t
select bb,convert(decimal(10,3),count(1))/convert(decimal(10,3),@c) as 出现次数 from t group by bb
用一个sql语句写出来。
下面是测试数据:


create table t(aa int,bb int)
insert into t(aa,bb)
select 1,1 union all
select 2,1 union all
select 3,2 union all
select 4,2 union all
select 5,1 union all
select 6,2 union all
select 7,1 union all
select 8,3 union all
select 9,1 union all
select 12,3

/* 求SQL语句,把下面三行转换为1行
declare @c int
select @c=count(1) from t
select bb,convert(decimal(10,3),count(1))/convert(decimal(10,3),@c) as 出现次数 from t group by bb

*/


drop table t

[解决办法]
select bb,convert(decimal(10,3),count(1))/convert(decimal(10,3),(select col = count(1) from t)) as 出现次数 from t group by bb

读书人网 >SQL Server

热点推荐