日期:2014-05-18  浏览次数:20627 次

一个简单的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