select lanmu_id,count(lanmu_id) as col1 from 表名 group by lanmu_id
select count(lanmu_id) from 表名 这是总的记录数 我也会的 我需要统计统计每个lanmu_id 的记录数占总量的多少?
SELECT SUM(HANGYE_ID) --统计总数 Declare @a real declare @b real set @a=(select hangye_id from table where lanmu_id) set @b=(select sum(hangye_id) from table where lanmu_id) select (@a/@b)*100 +'%'--统计总数
select a.lanmu_id,a.col1 as 记录数,100.0*col1/col2 as 占比 from (select lanmu_id,count(lanmu_id) as col1 from 表名 group by lanmu_id) a, (select count(*) as col2 from 表名 ) b
declare @mysum int select @mysum =sum(lanmu_id ) from yourtable select lanmu_id /@mysum as x from yourtable
select lanmu_id ,a.TotalRow,count(Test.lanmu_id),CAST(count(Test.Lanmu_Id) as real)/a.TotalRow from Test cross join (select count(*) AS TotalRow from test) a Group By lanmu_id,a.TotalRow 应该可以解决你的问题
这是总的记录数
我也会的
我需要统计统计每个lanmu_id 的记录数占总量的多少?
Declare @a real
declare @b real
set @a=(select hangye_id from table where lanmu_id)
set @b=(select sum(hangye_id) from table where lanmu_id)
select (@a/@b)*100 +'%'--统计总数
(select lanmu_id,count(lanmu_id) as col1 from 表名 group by lanmu_id) a,
(select count(*) as col2 from 表名 ) b
select @mysum =sum(lanmu_id ) from yourtable
select lanmu_id /@mysum as x from yourtable
应该可以解决你的问题