create procedure spABC
asdeclare @countall float
select @countall = count(*) from table
select left(mid, 4) as 地区, count(*) as 数量, cast(count(*) * 100 / @countall as int) as 百分比 from table group by left(mid,4)
asdeclare @countall float
select @countall = count(*) from table
select left(mid, 4) as 地区, count(*) as 数量, cast(count(*) * 100 / @countall as int) as 百分比 from table group by left(mid,4)
count(*)*100.0/(Select count(*) from table) as 百分比
from table group by left(mid,4)
select left(mid,4),count(*) 次数, cast(count(*)*1.00/(select count(*) from table)*100 as varchar) +'%' as 百分比 from table group by left(mid,4)
as
declare @总数 decimal(20,2)
select @总数=count(*) from 表
select *,百分比=cast(cast(数量/@总数 * 100 as decimal(20,2) as varchar)+'%'
from(
select 地区=left(mid,4),数量=(count(*)
from 表 group by 地区) a
上面的 语法可以么?
as'
declare
countall ALIAS;
begin
select countall := count(*) from user_info
select substring(mid,1,4) as diqu, count(*) as num, cast(count(*) * 100 / @countall as int) as cent from user_info group by substring(mid,1,4)
end
end
' language ';总说没有结束符号
错在哪里
Create table mid(mid VarChar(6))
INSERT INTO mid(mid) values ('000111')
INSERT INTO mid(mid) values ('000122')
INSERT INTO mid(mid) values ('000133')
INSERT INTO mid(mid) values ('000211')
DECLARE @Num INT
SET @NUM=(SELECT COUNT(*) FROM mid)
SELECT SUBSTRING(mid,1,4) AS 地区,
@Num AS 总数,
百分比=cast(cast(COUNT(*)*100/@Num as decimal(20,2)) as varchar)+'%'
FROM mid
GROUP BY SUBSTRING(mid,1,4)
DROP TABLE mid
COMMIT TRAN TEST------
000111
000122
000133
000211
------
0001 4 75.00%
0002 4 25.00%
as
declare @总数 decimal(20,2)
select @总数=count(*) from 表
select *,百分比=cast(cast(数量/@总数 * 100 as decimal(20,2) as varchar)+'%'
from(
select 地区=left(mid,4),数量=(count(*)
from 表 group by 地区) a
select left(mid,4),count(*) 次数, cast(count(*)*1.00/(select count(*) from table)*100 as varchar) +'%' as 百分比 from table group by left(mid,4)