首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-03-31 
一个简单的sql语句把declare@cintselect@ccount(1)fromtselectbb,convert(decimal(10,3),count(1))/conve

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

热点排行
Bad Request.